-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Unable to inject parameters containing array of enums into service #49505
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
Thanks for the great report. |
Challenge accepted: I asked to PHP core devs, and they confirmed listing enums in array prevents opcache from putting said array in shared memory. I still recommend splitting as advised on that comment. |
The issue is that #48045 added support for adding enums in parameters in loaders and configurators, without adding tests to ensure they work everywhere else in the component. So this was not caught when doing other refactorings. |
Should we reuse |
…s (fancyweb) This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Fix dumping array of enums parameters | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #49505 | License | MIT | Doc PR | - Commits ------- 97c5874 [DependencyInjection] Fix dumping array of enums parameters
Symfony version(s) affected
^6.2.0
Description
Symfony 6.2 introduced enums as service parameters: https://symfony.com/blog/new-in-symfony-6-2-improved-enum-support
One of the examples includes defining an array of enums as a parameter:
Although, when you try to inject said parameter into a service, the container produces a warning when it's asked to instantiate the service:
How to reproduce
DemoService
.app.some_parameter
can be resolved, the container will warn about the absence ofapp.another_parameter
.I created a repo demonstrating this issue: See https://github.com/imba28/symfony-enum-array-parameters-bug for a complete example
Possible Solution
I think this is due to a bug in the class
PhpDumper
. Whenever a parameter contains an enum, it is marked as a dynamic parameter that must be resolved using$container->getParameter()
:symfony/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Lines 1516 to 1522 in 1dcb0f9
However, the routine dumping the parameters lacks an equivalent check:
symfony/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Lines 1918 to 1934 in 1dcb0f9
For that reason, the generated file
var/cache/dev/ContainerXYZ/getDemoServiceService.php
includesinstead of
which eventually fails, since
app.another_parameter
has been previously marked as a dynamic parameter and thus is not part of$container->parameters
.Possible fix
Recursively traverse
$value
and determine if there's an enum hidden somewhere and place the check in this condition: https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php#L1928If it does, dump
$this->getParameter(%s)
instead of$this->parameters[%s]
.Additional Context
No response
The text was updated successfully, but these errors were encountered: