-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Autowiring confuses argument order in some cases #17724
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
Comments
Might be a weird brainfart, but what about allowing a map of |
@iltar So first, there is still a bug here - I want to make sure we keep that focus :). But, your idea is interesting. Well actually, if it weren't for this bug, you can already do what you're saying, just under the services:
product_repository:
class: AppBundle\Repository\ProductRepository
autowire:
logger: '@logger' In theory, this could also still just be done under |
@weaverryan this use case is already supported (https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php#L75) The following snippet should work: services:
product_repository:
class: AppBundle\Repository\ProductRepository
autowire: true
arguments: [ '', '@logger' ] |
It looks weird in YAML (but works), it's far better in XML: <service id="product_repository" class="AppBundle\Repository\ProductRepository" autowire="true">
<argument />
<argument type="service" id="logger" />
</service> |
You're right - but it's odd: if I use your same syntax but with services:
product_repository:
class: AppBundle\Repository\ProductRepository
autowire: true
arguments:
- ~
- '@logger' Anyways, with your working example, it just doesn't feel right. One day, autowiring might work great. Then suddenly I add a 4th constructor argument that can't be autowired. Suddenly I need to add 5 lines ( services:
product_repository:
class: AppBundle\Repository\ProductRepository
autowire: true
arguments:
- ''
- ''
- ''
- ''
- '%kernel.root_dir%' And if we change the order of the args (something autowiring should allow), it'll break. What do you think about my previous suggestion? #17724 (comment) |
@weaverryan it makes a lot of sense to enhance this feature. I personally prefer you're proposal of using: services:
product_repository:
class: AppBundle\Repository\ProductRepository
autowire: true
arguments:
1: '@logger' Because it couples less the code with the configuration. With named parameters, if you rename a constructor parameter, it breaks the app. With numbers as keys, the only way to break the app is to change the order of parameters (and it's already considered a BC break). |
👍 for argument indexes. That's already how we manage arguments internally by the way ( |
Hence my original suggestion, as it's simply mapped and you can use |
Ok then with arguments by index! Actually, I thought that this would already work - I tried that originally and then opened this "bug" (see my original code block, it matches the last one from @dunglas) exactly. So, this is "actionable" - someone just needs to make wiring work with a specific index. Thanks! |
… ones are put in the wrong spot
PR at #17876 |
… ones are put in the wrong spot
… ones are put in the wrong spot
… are set (weaverryan) This PR was merged into the 2.8 branch. Discussion ---------- [DependencyInjection] Fixing autowiring bug when some args are set | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17724, #17878 | License | MIT | Doc PR | todo This fixes #17724 & #17878. **#17724** I've set this against the 2.8 branch because imo it's a bug fix. The [test](https://github.com/symfony/symfony/compare/2.8...weaverryan:auto-wiring-individuals?expand=1#diff-d124c3d39cd5f7c732fb3d3be7a8cb42R298) illustrates the bug - having *some* arguments set beforehand caused auto-wired arguments to be set on the wrong index. **#17878** I've also included this fix just to get all the weird ordering problems taken care of at once. I don't think this is a behavior change - autowiring with scalars only worked previously if the argument was optional (still works now) or if you specified that argument explicitly (still works). Otherwise, your argument ordering would have gotten messed up. Commits ------- 260731b minor changes 865f202 [#17878] Fixing a bug where scalar values caused invalid ordering cf692a6 [#17724] Fixing autowiring bug where if some args are set, new ones are put in the wrong spot
* 2.8: fixed issue with PHP 5.3 The WebProcessor now forwards the client IP minor changes [#17878] Fixing a bug where scalar values caused invalid ordering [#17724] Fixing autowiring bug where if some args are set, new ones are put in the wrong spot bumped Symfony version to 2.3.39 updated VERSION for 2.3.38 update CONTRIBUTORS for 2.3.38 updated CHANGELOG for 2.3.38
* 3.0: fixed issue with PHP 5.3 The WebProcessor now forwards the client IP minor changes [#17878] Fixing a bug where scalar values caused invalid ordering [#17724] Fixing autowiring bug where if some args are set, new ones are put in the wrong spot bumped Symfony version to 2.3.39 updated VERSION for 2.3.38 update CONTRIBUTORS for 2.3.38 updated CHANGELOG for 2.3.38
Hi guys!
Take the following class:
Since
LoggerInterface
can't be injected yet (we're waiting on a MonologBundle tag), I wired it like this:Sure, it's weird to have the "1" in there, but I thought it would work. It almost did, but it reversed the arguments. In the cached container, this becomes:
In YAML, I believe this is the only way to set one specific argument, without specifying all of them, and it looks like a bug :).
Especially because non-services can't be autowired yet, this probably the biggest issue left with autowiring (along with not being able to globally map a class/interface to a specific service id).
ping @dunglas
Thanks!
The text was updated successfully, but these errors were encountered: