Skip to content

[*Bundle] Add autowiring aliases for common services #22098

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

Merged
merged 1 commit into from
Mar 21, 2017

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Mar 21, 2017

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

As spotted while working on #22060, we're missing many aliases to prevent any autowiring ambiguities.
I also removed the "Symfony\Component\EventDispatcher\EventDispatcher" and "Symfony\Component\DependencyInjection\Container" aliases: we'd better encourage using the corresponding interfaces instead.
On ControllerTrait, we need to type hint against SessionInterface, because otherwise, when session support is disabled, autowiring still auto-registers an "autowired.Session" service, which defeats the purpose of being able to enable/disable it.

@@ -7,6 +7,7 @@
<services>
<!-- ResolvedFormTypeFactory -->
<service id="form.resolved_type_factory" class="Symfony\Component\Form\ResolvedFormTypeFactory" />
<service id="Symfony\Component\Form\ResolvedFormTypeFactoryInterface" alias="form.resolved_type_factory" public="false" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it actually needed ? I never had a need to inject it directly. This service should probably only be used by the form registry and the form factory, not by userland code

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dunno, but it's public and has an interface, so does no harm at least

@@ -80,6 +80,9 @@
</service>

<service id="router" alias="router.default" />
<service id="Symfony\Component\Routing\RouterInterface" alias="router.default" public="false" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the alias should point to router, not to router.default, to inject the right service when overwriting or decorating the router

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated, I also added aliases for session.storage and session.handler

@@ -27,6 +28,7 @@
<!-- Run after all custom normalizers -->
<tag name="serializer.normalizer" priority="-1000" />
</service>
<service id="Symfony\Component\Serializer\Normalizer\ObjectNormalizer" alias="serializer.normalizer.object" public="false" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it necessary ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @lyrixx @dunglas wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that there is a good use case to inject this normalizer using autowiring. I would not recommend to do it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably a better idea to alias Symfony\Component\Serializer\Normalizer\NormalizerInterface and Symfony\Component\Serializer\Normalizer\DenormalizerInterface to the serializer service in case someone want to autowire a normalizer.

@stof
Copy link
Member

stof commented Mar 21, 2017

I agree about encouraging interfaces rather than implementations (especially for the translator and dispatcher, as we have decorator implementations)

* @required
*/
protected function getSession(): Session
protected function getSession(): SessionInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change violates the Liskov substitution principle, getFlashBag() is not part of this interface.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See PR description for why we need to do this.
The addFlash() method now throws a LogicException when needed.

@@ -14,6 +14,7 @@
<argument type="collection" />
<argument type="collection" />
</service>
<service id="Symfony\Component\Serializer\SerializerInterface" alias="serializer" public="false" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also mark Symfony\Component\Serializer\NormalizerInterface, Symfony\Component\Serializer\DenormalizerInterface, Symfony\Component\Serializer\EncoderInterface and Symfony\Component\Serializer\DecoderInterface as aliases of the serializer.

@nicolas-grekas nicolas-grekas force-pushed the autow-aliases branch 2 times, most recently from a1b115a to b1f1b12 Compare March 21, 2017 20:49
Copy link
Member

@dunglas dunglas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@fabpot
Copy link
Member

fabpot commented Mar 21, 2017

Thank you @nicolas-grekas.

@fabpot fabpot merged commit 08c2ee3 into symfony:master Mar 21, 2017
fabpot added a commit that referenced this pull request Mar 21, 2017
…nicolas-grekas)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[*Bundle] Add autowiring aliases for common services

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

As spotted while working on #22060, we're missing many aliases to prevent any autowiring ambiguities.
I also removed the "Symfony\Component\EventDispatcher\EventDispatcher" and "Symfony\Component\DependencyInjection\Container" aliases: we'd better encourage using the corresponding interfaces instead.
On ControllerTrait, we need to type hint against SessionInterface, because otherwise, when session support is disabled, autowiring still auto-registers an "autowired.Session" service, which defeats the purpose of being able to enable/disable it.

Commits
-------

08c2ee3 [*Bundle] Add autowiring aliases for common services
@nicolas-grekas nicolas-grekas deleted the autow-aliases branch March 21, 2017 22:00
fabpot added a commit that referenced this pull request Apr 5, 2017
…g reflection against all existing services (nicolas-grekas)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[BC BREAK][DI] Always autowire "by id" instead of using reflection against all existing services

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | yes - compile time only
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

(patch best reviewed [ignoring whitespaces](https://github.com/symfony/symfony/pull/22295/files?w=1).)

"By-id" autowiring, as introduced in #22060 is free from all the issues that "by-type" autowiring has:
- it has no magic and requires explicit type<>id matching (*vs* using reflection on all services to cherry-pick *the* one that matches some type-hint *at that time*, which is fragile)
- it is free from any ambiguities (*vs* the Damocles' sword of breaking config just by enabling some unrelated bundle)
- it is easily introspected: just look at DI config files (*vs* inspecting the type-hierarchy of all services + their type-hints)
- ~~it is side-effect free, thus plain predictable (*vs* auto-registration of discovered types as services)~~
- it plays nice with deprecated services or classes (see #22282)
- *etc.*

Another consideration is that any "by-type" autowired configuration is either broken (because of future ambiguities) - or equivalent to a "by-id" configuration (because resolving ambiguities *means* adding explicit type<>id mappings.) For theoreticians, we could say that "by-id" autowiring is the asymptotic limit of "by-type" autowiring :-)

For all these reasons - and also because it reduces the complexity of the code base - I propose to change the behavior and only support "by-id" autowiring in 3.3. This will break some configurations relying on "by-type" autowiring. Yet the break will only happen at compile-time, which means this won't *silently* break any apps. For all core Symfony services, they will work out of the box thanks to #22098 *et al.* For the remaining services, fixing ones config should be pretty straightforward: just follow the suggestions provided by the exception messages. If they are fine to you, you'll end up with the exact same config in the end. And maybe you'll spot issues that were hidden previously.

Commits
-------

cc5e582 [BC BREAK][DI] Always autowire "by id" instead of using reflection against all existing services
fabpot added a commit that referenced this pull request Apr 9, 2017
…Scott)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[FrameworkBundle] Add autowiring alias for Stopwatch

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Following in the footsteps of #22098 to add aliases for various services, this adds an alias for `Symfony\Component\Stopwatch\Stopwatch`.

Commits
-------

707f74b [FrameworkBundle] Add autowiring alias for Stopwatch
fabpot added a commit that referenced this pull request Apr 12, 2017
This PR was merged into the 3.3-dev branch.

Discussion
----------

[FrameworkBundle] Fix serializer aliases

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT
| Doc PR        |

Fix serializer aliases introduced in #22098

Commits
-------

22e72d0 Fix serializer aliases
@fabpot fabpot mentioned this pull request May 1, 2017
ogizanagi added a commit that referenced this pull request Jun 16, 2022
…concrete normalizers (chalasr)

This PR was merged into the 6.2 branch.

Discussion
----------

[Serializer] Deprecate autowiring aliases pointing to concrete normalizers

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | no
| Deprecations? | yes
| Tickets       | -
| License       | MIT
| Doc PR        | -

They should've never been added. See #22098 (comment)

Commits
-------

c1680be [Serializer] Deprecate autowiring aliases pointing to concrete normalizers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants