auraphp / Aura.Di

Dependency Injection System
MIT License
349 stars 63 forks source link

Problem with the handling of namespaces #100

Closed cziegenberg closed 9 years ago

cziegenberg commented 9 years ago

I did a simple test and found a problem with the handling of namespaces. The optional leading namespace separator "\" can result in Aura\Di\Resolver\UnresolvedParam exceptions when used, because the (parent) class parameters cannot be found.

As you always have to use full class names, I'd suggest to explicitly define in the docs, that you MUST NOT leading namespace separators, and I also suggest to remove them automatically from given class name arguments (e.g. in newInstance).

TEST 1: Result: Works

$di->params['RunTimeException']['message'] = 'test';
$di->params['RunTimeException']['code'] = 0;
$di->params['RunTimeException']['previous'] = null;
$di->newInstance('RunTimeException');

TEST 2: Result: Fails (Aura\Di\Resolver\UnresolvedParam)

$di->params['RunTimeException']['message'] = 'test';
$di->params['RunTimeException']['code'] = 0;
$di->params['RunTimeException']['previous'] = null;
$di->newInstance('\RunTimeException');

TEST 3: Result: Works

$di->params['Exception']['message'] = 'test';
$di->params['Exception']['code'] = 0;
$di->params['Exception']['previous'] = null;
$di->newInstance('RunTimeException');

TEST 4: // Result: Fails (Aura\Di\Resolver\UnresolvedParam), because get_parent_class() return class names without leading "\"

$di->params['\Exception']['message'] = 'test';
$di->params['\Exception']['code'] = 0;
$di->params['\Exception']['previous'] = null;
$di->newInstance('RunTimeException');

TEST 5: Result: Works

$di->params['Exception']['message'] = 'test';
$di->params['Exception']['code'] = 0;
$di->params['Exception']['previous'] = null;
$di->newInstance('\RunTimeException');

TEST 6: // Result: Fails (Aura\Di\Resolver\UnresolvedParam), because get_parent_class() return class names without leading "\"

$di->params['\Exception']['message'] = 'test';
$di->params['\Exception']['code'] = 0;
$di->params['\Exception']['previous'] = null;
$di->newInstance('\RunTimeException');

TEST 7: Result: Works

$di->params['\RunTimeException']['message'] = 'test';
$di->params['\RunTimeException']['code'] = 0;
$di->params['\RunTimeException']['previous'] = null;
$di->newInstance('\RunTimeException');

TEST 8: Result: Fails (Aura\Di\Resolver\UnresolvedParam)

$di->params['\RunTimeException']['message'] = 'test';
$di->params['\RunTimeException']['code'] = 0;
$di->params['\RunTimeException']['previous'] = null;
$di->newInstance('RunTimeException');
harikt commented 9 years ago

Thank you for the report.

Lazy me : v2 or v3 ? PR on tests would also be helpful in case you love. Thank you.

cziegenberg commented 9 years ago

Tested with v3. If I have some time, I'll try to add tests (currently working on bugs of my own projects :)).

pmjones commented 9 years ago

I'd suggest to explicitly define in the docs, that you MUST NOT leading namespace separators

Added a note in the docs as part of c8a6957.

I also suggest to remove them automatically from given class name arguments

Easier to do in the methods than in the arrays. PR welcome on that one, if you like.

acim commented 9 years ago

Class name resolution via ::class is perfect to be used here, but just if you run >=5.5.

http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.class.class