-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] Deprecate autowiring-types in favor of aliases #21494
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
4366566
to
c3e73db
Compare
@nicolas-grekas Haha... I really like this simplification... but I don't understand how it works! :) Is there some significance to the service id matching the autowired interface? Otherwise... I would still expect that if I had 10 services for an interface (e.g. LoggerInterface)... that now it would simply appear that I would have 11 services for that interface. Clearly, I'm missing something ;) |
It is very simple: if there is a service named "Foo\BarInterface", then that's the one, when looking to wire a "Foo\BarInterface" type hint. |
UPGRADE-3.3.md
Outdated
@@ -14,6 +14,8 @@ Debug | |||
DependencyInjection | |||
------------------- | |||
|
|||
* Autoriwing-types have been deprecated, use aliases instead. |
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.
Shouldn't we add an example 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.
indeed, the example from the pr desc fi
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.
example added
this is a very elegant solution to the problem, allowing to use third-party services to autowire a type (as your alias can point to any service), and forbidding to explicitly configure the same autowired type twice for different services (as service ids are unique). So 👍 for me. |
c3e73db
to
78c9141
Compare
Far simpler 👍 |
Great! 👍 |
Ha, I missed the change to 👍 |
$definition = $container->findDefinition('templating'); | ||
$definition->setAutowiringTypes(array(ComponentEngineInterface::class, FrameworkBundleEngineInterface::class)); | ||
$container->setAlias(ComponentEngineInterface::class, 'templating'); | ||
$container->setAlias(FrameworkBundleEngineInterface::class, 'templating'); |
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 should be private aliases. We don't want to keep these service names in the compiled container
</service> | ||
<service id="Symfony\Component\EventDispatcher\EventDispatcherInterface" alias="event_dispatcher" /> | ||
<service id="Symfony\Component\EventDispatcher\EventDispatcher" alias="event_dispatcher" /> |
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.
these should be private aliases
<autowiring-type>Symfony\Component\DependencyInjection\ContainerInterface</autowiring-type> | ||
<autowiring-type>Symfony\Component\DependencyInjection\Container</autowiring-type> | ||
</service> | ||
<service id="service_container" synthetic="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.
shouldn't this one be removed entirely ? The component already handles it
</service> | ||
<service id="service_container" synthetic="true" /> | ||
<service id="Symfony\Component\DependencyInjection\ContainerInterface" alias="service_container" /> | ||
<service id="Symfony\Component\DependencyInjection\Container" alias="service_container" /> |
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.
these aliases should be private
</service> | ||
<service id="Symfony\Component\Translation\TranslatorInterface" alias="translator.default" /> |
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 alias should be translator
IMO instead, so that autowiring injects the real translator when using decoration. Otherwise, we loose the profiler integration when using autowiring and someone decorates the main translator
alias rather than the private service.
And this alias should also be private (as any alias created purely for autowiring purpose anyway)
UPGRADE-3.3.md
Outdated
@@ -14,6 +14,23 @@ Debug | |||
DependencyInjection | |||
------------------- | |||
|
|||
* Autoriwing-types have been deprecated, use aliases instead. |
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.
typo here
UPGRADE-4.0.md
Outdated
@@ -24,6 +24,23 @@ Debug | |||
DependencyInjection | |||
------------------- | |||
|
|||
* Autoriwing-types have been removed, use aliases instead. |
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.
typo here
78c9141
to
b11d391
Compare
@stof comments addressed, thanks |
btw, my comment about using private aliases for this is also true for the documentation when writing it |
Docs issue created - I added a note on there about your (Stof) "private aliases" comment. |
Thank you @nicolas-grekas. |
…icolas-grekas) This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Deprecate autowiring-types in favor of aliases | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #21351, #19970, ~~#18040~~, ~~#17783~~ | License | MIT | Doc PR | symfony/symfony-docs#7445 https://github.com/symfony/symfony/pull/21494/files?w=1 This PR deprecates autowiring-types and replaces them by plain aliases. ping @dunglas @weaverryan Eg instead of ```xml <service id="annotations.reader" class="Doctrine\Common\Annotations\AnnotationReader" public="false"> <autowiring-type>Doctrine\Common\Annotations\Reader</autowiring-type> </service> ``` just do: ```xml <service id="annotations.reader" class="Doctrine\Common\Annotations\AnnotationReader" public="false" /> <service id="Doctrine\Common\Annotations\Reader" alias="annotations.reader" public="false" /> ``` Commits ------- b11d391 [DI] Deprecate autowiring-types in favor of aliases
@nicolas-grekas shouldn't this be back-ported to 2.8? Autowiring has been introduced there and IIRC there is |
@theofidry from semver:
|
@theofidry I don't think so. People using 2.8 LTS should not be annoyed with a deprecation in a patch version. It works well as is. This improvement is really a new feature. |
fair enough |
|
@sstok Using public aliases works fine on my side. The exception message make me think it's unrelated. Maybe open an issue showing the failing service definition (from |
I used
Should be
|
#18040,#17783https://github.com/symfony/symfony/pull/21494/files?w=1
This PR deprecates autowiring-types and replaces them by plain aliases.
ping @dunglas @weaverryan
Eg instead of
just do: