Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add note about not using autowire with ServiceSubscriber#20961

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

Open
Neirda24 wants to merge4 commits intosymfony:7.2
base:7.2
Choose a base branch
Loading
fromNeirda24:update-service-subscriber

Conversation

Neirda24
Copy link

Resolve#20959

@Neirda24Neirda24 changed the titleAdd note about not using autoconfigureAdd note about not using autoconfigure with ServiceSubscriberMay 14, 2025
@nicolas-grekas
Copy link
Member

nicolas-grekas commentedMay 15, 2025
edited
Loading

I'm sorry I don't understand what this is trying to solve.
Having an injection point is always required, using autoconfiguration or not.
If you don't want to use autoconfiguration, you should declare the locator using DI tags, as already explained in the doc.
If I missed your point, please expand a bit.

@Neirda24
Copy link
Author

sure thing@nicolas-grekas . In a bundle we were using the ServiceSubscriberInterface. We couldn't find how to apply the setContainer manually. Because there was a#[Required] on thesetContainer which implied to us that it requires autowire. So we tagged withcontainer.service_subscriber but couldn't find a way to specify the method call ourselves. So even though autowire is not required it automagically binds to whatever ContainerInterface but we were planning on doing it manually (as it is often the case in bundles). Whereas in this cas you have to define the setContainer call method ahead in your service declaration instead of manually through the Extension or a compiler pass. Let me know if it is not clear enough.

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedMay 15, 2025
edited
Loading

I guess you're trying to document the logic inhttps://github.com/symfony/symfony/blob/dff2ff8bae203d46a44d3fd1f8d7adfa457c8d67/src/Symfony/Component/DependencyInjection/Compiler/ResolveServiceSubscribersPass.php#L32-L34, right? (we should add ServiceCollectionInterface there BTW)

Then this relates to autowiring, not autoconfiguration: ifnot autowiring, those references that point to ContainerInterface/ServiceProviderInterface should be defined explictly.

An example in the doc would be nice indeed.

@Neirda24
Copy link
Author

yes my bad I thought autowire but wrote autoconfigure. You are right indeed. With the PR linked I was trying to solve a bit more than the documentation. Because if you'd like to map several ContainerInterface (never had the case so this is why I won't push more) then you'd be stuck with this auto-binding.

If I add examples like

$services->set('.sensiolabs_gotenberg.abstract_builder', AbstractBuilder::class)        ->abstract()        ->call('setContainer', [service(ContainerInterface::class)])        ->tag('container.service_subscriber')    ;

then we should keep it as headline instead of note. Or I am wrong ?

@Neirda24Neirda24 changed the titleAdd note about not using autoconfigure with ServiceSubscriberAdd note about not using autowire with ServiceSubscriberMay 15, 2025
Without autowire
~~~~~~~~~~~~~~~~

If you are not using autowire you need to require either ``Psr\Container\ContainerInterface`` or ``Symfony\Contracts\Service\ServiceProviderInterface`` as a ``__construct`` argument or as a method call like ``setContainer(Psr\Container\ContainerInterface $container)``.

Choose a reason for hiding this comment

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

Suggested change
If you are not usingautowire you need torequire either``Psr\Container\ContainerInterface``or ``Symfony\Contracts\Service\ServiceProviderInterface`` as a ``__construct`` argument oras amethod call like ``setContainer(Psr\Container\ContainerInterface $container)``.
If you are not usingautowiring, you need toreference the special``Psr\Container\ContainerInterface``service where the locator should be inject (typicallyas aconstructor argument.)


If you are not using autowire you need to require either ``Psr\Container\ContainerInterface`` or ``Symfony\Contracts\Service\ServiceProviderInterface`` as a ``__construct`` argument or as a method call like ``setContainer(Psr\Container\ContainerInterface $container)``.

.. code-block:: diff

Choose a reason for hiding this comment

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

I guess a.. configuration-block:: would be preferable, since this diff applies to nothing currently?

@@ -140,6 +140,18 @@ count and iterate over the services of the locator::
The :class:`Symfony\\Contracts\\Service\\ServiceCollectionInterface` was
introduced in Symfony 7.1.

Without autowire

Choose a reason for hiding this comment

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

maybe this?

Suggested change
Without autowire
Manually wiring a locator

Choose a reason for hiding this comment

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

or a.. note:: indeed as suggested by Oskar

Copy link
Author

Choose a reason for hiding this comment

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

is it possible to add code block in a note ?

@nicolas-grekas
Copy link
Member

What happens if you don't do anything? No explicit service I mean?
symfony/symfony#34998 makes me think it might work out of the box even if autowiring is disabled.

@Neirda24
Copy link
Author

@nicolas-grekas : if you don't add thesetContainer call for example : nothing. and no easy way to convert the ServicesubscriberInterface onto a ServiceLocator (hence the PRsymfony/symfony#60272)

@nicolas-grekas
Copy link
Member

if you don't add the setContainer call for example : nothing

can you try declaring it with no arguments?

@Neirda24
Copy link
Author

👍 it works indeed.

@nicolas-grekas
Copy link
Member

OK, so basically this note can be reduced to the case were the injection point is not the constructor + no need to tell about which service to wire, binding does the job

@Neirda24
Copy link
Author

will update accordingly

@stof
Copy link
Member

no need to tell about which service to wire, binding does the job

We still need to document the valid targets for the explicit wiring. A common mistake I saw in support request is inject the mainservice_container instead of one of the supported bindings (which does not trigger a compile-time error, but will likely tell you that you cannot access a private service from the container during runtime)

nicolas-grekas reacted with thumbs up emoji

nicolas-grekas added a commit to symfony/symfony that referenced this pull requestMay 16, 2025
…ctionInterface when declaring a service subscriber (nicolas-grekas)This PR was merged into the 7.2 branch.Discussion----------[DependencyInjection] Fix missing binding for ServiceCollectionInterface when declaring a service subscriber| Q             | A| ------------- | ---| Branch?       | 7.2| Bug fix?      | yes| New feature?  | no| Deprecations? | no| Issues        | -| License       | MITLooks like we forgot this in#53163I figured it out when reviewingsymfony/symfony-docs#20961Commits-------6174d09 [DependencyInjection] Fix missing binding for ServiceCollectionInterface when declaring a service subscriber
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

@OskarStarkOskarStarkOskarStark left review comments

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

5 participants
@Neirda24@nicolas-grekas@stof@OskarStark@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp