-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Added a shortcut method for autowired definitions #20648
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
*/ | ||
public function autowire($id, $class = null) | ||
{ | ||
return $this->setDefinition((new Definition($class))->setAutowired(true)); |
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.
$id
forgotten ;)
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.
Thanks, updated it and added a simple test.
We should plan for #20167 also |
@nicolas-grekas I agree. What about introducing a third (optional) type parameter? |
I like the shortcut but the name made me think the method allows to autowire an existing service more than registering a new autowired one (i.e replace |
|
I love it 👍 |
Thank you @wouterj. |
…wired definitions (wouterj) This PR was merged into the 3.3-dev branch. Discussion ---------- [DependencyInjection] Added a shortcut method for autowired definitions | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | todo This is a simple proposal to make adding autowired definitions to the `ContainerBuilder` a little easier. Registering autowired services in PHP code was quite verbose at the moment, while the whole point of autowiring is quick service registration. **Before** ```php $container->register('app.twig_extension', AppExtension::class) ->setAutowired(true) ->addTag('twig.extension') ; ``` **After** ```php $container->autowire('app.twig_extension', AppExtension::class) ->addTag('twig.extension') ; ``` With #20264, this will be even nicer: ```php $container->autowire(AppExtension::class) ->addTag('twig.extension') ; ``` Commits ------- 6ef4ce8 Added a shortcut method for autowired definitions
This is a simple proposal to make adding autowired definitions to the
ContainerBuilder
a little easier. Registering autowired services in PHP code was quite verbose at the moment, while the whole point of autowiring is quick service registration.Before
After
With #20264, this will be even nicer: