electricjones / data-manager

Simple data manager for nested data and dot notation access, plus optional extras
MIT License
12 stars 2 forks source link

Namespacing data loaded from files #36

Closed electricjones closed 9 years ago

electricjones commented 9 years ago

There is a potential flaw in the way the FileLoader constructs its array data atm. If two files are loaded and share some key, whichever is loaded last overwrites the first. There should be a way around this, and it should probably not do this by default.

To be clear, this is not the same as #34.

This is what I mean. hello.json

{
  "one": 1
}

world.php

return [
  'one' => 'one'
]

In this case, world.php overwrites hello.json.

Possible Fixes

There are a couple options to correct this. 1 Auto namespace items based on (sanitized) filename

hello.json

{
  "one": 1
}

world.php

return [
  'one' => 'one'
]
$manager->getAll();
/*
[
   'hello' => [
      'one' => 1
   ],
   'world' => [
      'one' => 'one'
   ]
]
*/

2 Give the user the ability to decide a namespace

$manager->addFiles([
   [SplFileInfo, 'someCustomNamespace'],
   [SplFileInfo, 'someOtherNamespace'],
]);

Which would namespace each file under that particular namespace.

3 A combination of both would probably be best

hello.json

{
  "one": 1
}

world.php

return [
  'one' => 'one'
]
$manager->addFiles([
   [SplFileInfo('hello.json'), 'myNs'],
   SplFileInfo('world.php'),
]);

$manager->getAll();
/*
[
   'myNs' => [
      'one' => 1
   ],
   'world' => [
      'one' => 'one'
   ]
]
*/

Things to be aware of

I'd like to do some research into Laravel, Symfony, and Yii. I know they all do this and have used all three, but never noticed how they handle this little issue. Any other thoughts are welcome.

electricjones commented 9 years ago

Added to develop branch

smolinari commented 9 years ago

I am not sure how I missed this issue. But I did. Does something still need to be done? Sort of confused by the "Added to develop branch" comment.

Scott

electricjones commented 9 years ago

No it was super simple. Only took me a few minutes. Thanks though :)