-
Notifications
You must be signed in to change notification settings - Fork 29
Feature: generated injector delegator #51
Feature: generated injector delegator #51
Conversation
As it could be used comonnly would be nice to have it out of the box.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense. Thanks a lot for your effort. Just a view nitpicks that needs to be fixed before a merge.
I also think it makes sense to add it to the ConfigProvider
I am not sure about it, what if someone already has it added there (the custom one) or if someone what to use another? |
Good point. Could you update the AoT guide? |
Docs is updated now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found some additional minor things that we may need to address.
Thanks for your effort to improve this component 👍
This namespace is default for generated resources.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @webimpress
sorry for the delay on this. Just one thing with the exception you throw. After that I think we're good to go.
This will improve readability and makes the unit easier to test. It will also offer a typed information to consumers to get rid it.
@michalbundyra As discussed in slack, I've addressed this one. Can you pleas re-check? |
{ | ||
$config = $container->has('config') ? $container->get('config') : []; | ||
$aotConfig = $config['dependencies']['auto']['aot'] ?? []; | ||
$namespace = empty($aotConfig['namespace']) ? 'Zend\Di\Generated' : $aotConfig['namespace']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe better "empty or non-string" here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$namespace = empty($aotConfig['namespace']) ? 'Zend\Di\Generated' : $aotConfig['namespace']; | |
$namespace = empty($aotConfig['namespace']) || ! is_string($aotConfig['namespace']) | |
? 'Zend\Di\Generated' | |
: $aotConfig['namespace']; |
Like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, please
test would be with an array, for example, as it can't be casted to string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just going to make this change and saw it would render the next 3 lines useless:
$config = $container->has('config') ? $container->get('config') : [];
$aotConfig = $config['dependencies']['auto']['aot'] ?? [];
$namespace = empty($aotConfig['namespace']) ? 'Zend\Di\Generated' : $aotConfig['namespace'];
if (! is_string($namespace)) {
throw new InvalidServiceConfigException('Provided namespace is not a string.');
}
I'd argue to leave it as is. This will be explicit to the user that there is an unusable misconfiguration instead of hiding it behind a default value that may cause failures which may become hard to debug. At least it's not obvious what's the issue that cause incorrect behavior.
I'll change the test from an int fixture to an array though.
|
||
use Psr\Container\ContainerExceptionInterface; | ||
|
||
class InvalidServiceConfigException extends LogicException implements ContainerExceptionInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if LogicException as a parent is the correct one
Maybe better InvalidArgumentException?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
InvalidArgumentException actually is a LogicException.
But in this case I'd argue that this is not cased by a function argument but a (service)configuration value. This could also be seen a RuntimeException
.
I chose LogicException since it's the developer's job to configure the services correctly, and if they don't provide a namespace-string, they made a logical mistake.
https://www.php.net/manual/en/class.logicexception.php
Exception that represents error in the program logic. This kind of exception should lead directly to a fix in your code.
But I'm happy if you provide a better fit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, sounds good to me.
I just wonder if we should have then another exception before if the namespace is provided but is not null and not a string? Maybe we shouldn't ignore invalid value. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this already the case or am I missing something? 🤔
@@ -27,7 +28,7 @@ public function testProvidedNamespaceIsNotAString() | |||
|
|||
$delegator = new GeneratedInjectorDelegator(); | |||
|
|||
$this->expectException(ContainerExceptionInterface::class); | |||
$this->expectException(InvalidServiceConfigException::class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it was quite important check, as container (per specification) can throw only ContainerExceptionInterface
instances.
would be nice to have it somewhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is a unit test really the correct scope? We could assert that InvalidServiceConfigException
implements ContainerExceptionInterface
. So that whenever this changes this test fails:
$this->expectException(InvalidServiceConfigException::class); | |
self::assertInstanceOf(ContainerInterface::class, new InvalidServiceConfigException()); | |
$this->expectException(InvalidServiceConfigException::class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, probably it should be separate test case, but I am happy to have it here for time being.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess a separate test would be out of scope for now. I'm not entirely sure if this is the correct place or way to cover this. Maybe we find something more appropriate in the future. 😉
Until then I added the assert as suggested.
``` | ||
|
||
After this we need to add configuration to the `ConfigProvider` class we created | ||
First, we'll use a delegator `Zend\Di\GeneratedInjectorDelegator` to decorate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to note that GeneratedInjectorDelegator
is available since version X.Y.Z.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs are updated. If somebody has a more elegant phrasing or suggesstion, I'd appreciate to hear it.
Static analysis is failing: https://travis-ci.org/zendframework/zend-di/jobs/625613032#L425 |
In documentation we are suggesting to create in manually. I think we can have it in the library as it's generic code.
See: https://docs.zendframework.com/zend-di/cookbook/aot-guide/#5-add-aot-to-the-service-manager
Documentations needs update if it is going to be accepted.
develop
branch, and submit against that branch.CHANGELOG.md
entry for the new feature.