8000 [DI] fix definition and usage of AbstractArgument by nicolas-grekas · Pull Request #36545 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] fix definition and usage of AbstractArgument #36545

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
Apr 24, 2020

Conversation

nicolas-grekas
Copy link
Member
Q A
Branch? master
Bug fix? yes
New feature? no
Deprecations? no
Tickets -
License MIT
Doc PR -

Reading https://symfony.com/blog/new-in-symfony-5-1-abstract-service-arguments and the comments there made me realize that the current implementation is not generic enough. Abstract arguments can be found anywhere, not only as service arguments. Also, AbstractArgument instances should not convey the key/id since that makes them harder to use in the PHP-DSL.

@javiereguiluz
Copy link
Member
javiereguiluz commented Apr 23, 2020

Thanks for simplifying this feature. This is the before:

<service id="maker.generator" class="Symfony\Bundle\MakerBundle\Generator">
    <argument type="service" id="maker.file_manager" />
    <argument type="abstract" key="$rootNamespace">defined in MakerPass</argument>
</service>
maker.generator:
    class: Symfony\Bundle\MakerBundle\Generator
    arguments:
        $rootNamespace: !abstract defined in MakerPass
$builder->register('maker.generator', Generator::class)
    ->addArgument(new AbstractArgument('maker.generator', '$rootNamespace', 'defined in MakerPass'));

Could you please show the after config? Thanks!

@nicolas-grekas
Copy link
Member Author

before:
->addArgument(new AbstractArgument('foo', '$baz', 'should be defined by Pass'));

after:
->addArgument(new AbstractArgument('should be defined by Pass'));

the rest is untouched.

But the example in PHP should be this actually if we want the equivalent of yaml/xml:
->setArgument('$rootNamespace', new AbstractArgument('should be defined by Pass'));

@wouterj
Copy link
Member
wouterj commented Apr 23, 2020

So I used this feature in the Security PR (see e.g. https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/SecurityBundle/Resources/config/security_authenticator.xml#L17) as <argument type="abstract">provider key</argument>.
Should we replace it with <argument type="abstract" key="$providerKey"/>? And then, if I'm correct we can no longer use replaceArgument(1, $providerKey), but need to do replaceArgument('$providerKey', $providerKey)?

@nicolas-grekas
Copy link
Member Author
nicolas-grekas commented Apr 23, 2020

Should we replace it with <argument type="abstract" key="$providerKey"/>?

nope: we don't use named arguments in core

And then, if I'm correct we can no longer use replaceArgument(1, $providerKey), but need to do replaceArgument('$providerKey', $providerKey)?

nothing changes here (but this is argument 3, not 1)

@wouterj
Copy link
Member
wouterj commented Apr 23, 2020

Hmm, but then I think this feature might still not be generic enough (or at least, it doesn't fit Symfony's usage). If I read the code correctly, the XML currently used in Security will result in this error message:
Argument "3" of service "security.authenticator.manager" is abstract. provider key.

This is imho weird in 2 ways:

  1. provider key. is a bit lost. The solution would be to only use <argument type="abstract"/>, but that removes the usefull explanation of what the argument will contain.
  2. It's not actually the 3rd argument that is missing, it's the 4th argument (but arguments are zero-indexed in the DI component).

(Btw, I agree with the changes in this PR, but I feel like the feature itself is not 100% correct yet)

@nicolas-grekas
Copy link
Member Author

The message would be:
Argument "3" of service "security.authenticator.manager" is abstract: provider key.

I get what you mean @wouterj, but I don't have any better proposal.

@nicolas-grekas
Copy link
Member Author

Note that what matters is that the message is actionable. In this regard, I think it is.

@nicolas-grekas
Copy link
Member Author

Argument "3" of service "security.authenticator.manager" is abstract: provider key.

Updated to use positional numbers: Argument 4 of service "security.authenticator.manager" is abstract: provider key.

@fabpot
Copy link
Member
fabpot commented Apr 24, 2020

@javiereguiluz Can you update the blog post?

@fabpot
Copy link
Member
fabpot commented Apr 24, 2020

Thank you @nicolas-grekas.

@javiereguiluz
Copy link
Member

Yes! I've just updated the blog post 👍

@nicolas-grekas nicolas-grekas modified the milestones: next, 5.1 May 4, 2020
@fabpot fabpot mentioned this pull request May 5, 2020
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.

5 participants
0