Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[DependencyInjection] Anonymous services in PHP DSL#24632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
[DependencyInjection] Anonymous services in PHP DSL#24632
Uh oh!
There was an error while loading.Please reload this page.
Conversation
unkind commentedOct 19, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I didn't fix all the tests yet (is there any script to update all autogenerated DI-fixtures?), need feedback. ping@nicolas-grekas UPD: target branch is incorrect. Anyway, any chance to merge it in 3.4? If so, I'll reopen PR. |
55997aa to02bf763Compare
nicolas-grekas left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 I'm 👎: this will introduce a non trivial amount of complexity (it's broken already), when the current way just works (just give that decorator a name.)
| */ | ||
| finalpublicfunctionargs(array$arguments) | ||
| { | ||
| $arguments =array_map( |
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.
Ihe inner can be injected anywhere, not only in the constructor, and can be nested also. The logic should be in AbstractConfigurator::processValue() (but it's not trivial to do so).
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.
and can be nested also
What do you mean?
The logic should be in AbstractConfigurator::processValue() (but it's not trivial to do so)
It is still in dev branch, later will be much harder to fix it. Probably, it's not the only case when we needDefinition object there.
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.
the inner service doesn't have to be injected as an argument: it can be nested in an array, which itself is passed as argument (constructor, or setter, or property in fact.)
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.
the inner service doesn't have to be injected as an argument: it can be nested in an array, which itself is passed as argument (constructor, or setter, or property in fact.)
It just requires to tweakAbstractConfigurator::processValue() to make it work, doesn't it?
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 should (but I'm still not sure it's worth the added complexity)
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 there any reason why it is static? As I can see,AbstractConfigurator contains link to thedefinition. I'm not big fan of it, but it's cheap solution.
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's used infunction iterator(array $values)
| ); | ||
| } | ||
| returnnewReferenceConfigurator($decorated[1] ?$decorated[1] :$this->id .'.inner'); |
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.
".inner" is not hardcoded when decorating services, see "renamedId".
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 don't get it.$decorated[1] contains value ofrenamedId. I use.inner suffix when user didn't specifyrenamedId.
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.
oh ok!
unkind commentedOct 19, 2017
What about just anonymous services without decorators? |
nicolas-grekas commentedOct 19, 2017
I would do it without the "anonymous" helper: |
unkind commentedOct 19, 2017
Well, a lot (probably, most) of my services are anonymous: listeners, command handlers, API controllers, console commands, cache/monitoring/logging decorators. I can use |
nicolas-grekas commentedOct 19, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
My fear is that ppl will get confused when reading And I'm also fearing that writing actually might become more confusing: "anonymous" would become a new item on the autocompletion list, which ppl might have a hard time figure out what it means (because it's not that common) - and even scarier: ppl doing the mistake of selecting the wrong set/anonymous choice might have an even harder time figuring out their mistake. |
unkind commentedOct 19, 2017
Let them learn the difference. They won't hurt themselves, it's not a gun.
I think most of us have such services in our codebases, we probably just don't realize it. It's yet another reason to make it explicit in my opinion. |
f304759 to766ceb3Compareunkind commentedOct 20, 2017
I've removed decorators, tests are OK except some unrelated ones. Let's focus on the |
unkind commentedOct 20, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Offtopic:
This annoys in the same way as requirement to specify name for "anonymous" services: I don't need it. Considering we have name convention for service names like |
nicolas-grekas commentedNov 26, 2017
I'm still really unsold on the topic, if anything, |
unkind commentedNov 26, 2017
@nicolas-grekas it looks like astraw man for me: I didn't say that serviceshave to be anonymous. Just many of themcould be. Giving name is fine, but sometimes is totally pointless like rituals.
Giving a name for this construction is fine, isn't it? The whole discussion is about explicit vs implicit syntax for the same concept. I don't understand your point why this kind of syntax should be implicit. |
nicolas-grekas commentedNov 26, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Oh! :) |
unkind commentedDec 18, 2017
What do you think of decorators for @@ -23,6 +25,16 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigura */ class PhpFileLoader extends FileLoader {+ private $configuratorDecorator;++ public static function withConfiguratorDecorator(ContainerBuilder $container, FileLocatorInterface $locator, callable $configuratorDecorator): PhpFileLoader+ {+ $loader = new self($container, $locator);+ $loader->configuratorDecorator = $configuratorDecorator;++ return $loader;+ }+ /** * {@inheritdoc} */@@ -44,7 +56,13 @@ class PhpFileLoader extends FileLoader $callback = $load($path); if ($callback instanceof \Closure) {- $callback(new ContainerConfigurator($this->container, $this, $this->instanceof, $path, $resource), $this->container, $this);+ $configurator = new ContainerConfigurator($this->container, $this, $this->instanceof, $path, $resource);++ if ($this->configuratorDecorator) {+ $configurator = call_user_func($this->configuratorDecorator, $configurator);+ }++ $callback($configurator, $this->container, $this); } |
ro0NL commentedDec 28, 2017
Anyway, just walked into this trying to register a event listener twice. Now in a codebase full of |
unkind commentedDec 28, 2017
Especially when you don't need a name (anonymous/lambda functions is a very close example). |
nicolas-grekas commentedDec 29, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
which is not an issue since the method is final, so can be changed without any BC break |
ro0NL commentedDec 29, 2017
Hm looking at failing tests locally that's bad right? :) |
unkind commentedDec 29, 2017
@ro0NL if you mean my branch, it's possible if you merged with 3.4/master because Do I understand correctly that in order to merge it I have to replace |
ro0NL commentedDec 29, 2017
right. I tested on 3.4 :) i thought it worked today already. If we can settle with |
766ceb3 tod709412Compareunkind commentedJan 4, 2018
@nicolas-grekas can you change base branch on master? |
curry684 commentedJan 4, 2018
You can do it yourself with the |
unkind commentedJan 4, 2018
@curry684 thanks, didn't know it. |
d709412 to8b6945dCompare8b6945d to2348c11Compare
nicolas-grekas left a comment
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.
(@unlinkd FYI, I pushed some changes on your fork, was quicker for me this way. Ready on my side thank you.)
unkind commentedJan 22, 2018
OK, looks good. |
fabpot commentedJan 23, 2018
Cannot be merged as tests are broken. |
2348c11 toee42276Comparenicolas-grekas commentedJan 23, 2018
Tests fixed, should be green in a few minutes. |
fabpot commentedJan 23, 2018
Thank you@unkind. |
…nkind)This PR was merged into the 4.1-dev branch.Discussion----------[DependencyInjection] Anonymous services in PHP DSL| Q | A| ------------- | ---| Branch? | 4.1| Bug fix? | no| New feature? | enhancement for fluent PHP configs| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets |#24493| License | MIT| Doc PR | not yetSee#24493:```php$s->anonymous(SendEmailForRegisteredUser::class)->tag('listener');```Commits-------ee42276 [DI] Add way to register service with implicit name using the PHP DSL
ro0NL commentedJan 23, 2018
Nice one@unkind. Thx super useful :) |
Uh oh!
There was an error while loading.Please reload this page.
See#24493: