-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Enums doesn't works when they are binded #44834
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
Actually enums are not supported properly in the DI parameters (yet?), while being supported in injection: # services.yaml
services:
_defaults:
autowire: true
autoconfigure: true
bind:
$myParam: !php/const App\Status::Deleted works, right? Moreover, the symfony/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBagInterface.php Line 48 in 6d9991c
symfony/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBagInterface.php Line 60 in 6d9991c
|
However the following fails: # services.yaml
services:
_defaults:
autowire: true
autoconfigure: true
bind:
- $myParam: !php/const App\Status::Deleted
+ App\Status $myParam: !php/const App\Status::Deleted ->
I tried #44838 on a 5.4 app with success. Perhaps you could confirm this patch is working as well on 4.4 @ismail1432? 🙏🏻 (appart from the intermediate parameter which is the root of this issue) |
…(ogizanagi) This PR was merged into the 4.4 branch. Discussion ---------- [DependencyInjection][HttpKernel] Fix enum typed bindings | Q | A | ------------- | --- | Branch? | 4.4 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | N/A <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Relates to #44834 While: ```yml # services.yaml services: _defaults: autowire: true autoconfigure: true bind: $myParam: !php/const App\Status::Deleted ``` is working for me, the following isn't: ```diff # services.yaml services: _defaults: autowire: true autoconfigure: true bind: - $myParam: !php/const App\Status::Deleted + App\Status $myParam: !php/const App\Status::Deleted ``` -> > Invalid value for binding key "App\Status $myParam" for service […]: expected "Symfony\Component\DependencyInjection\Reference", "Symfony\Component\DependencyInjection\Definition", "Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument", "Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument" or null, "App\Status" given. This attempts to fix it by accounting for `\UnitEnum` in the `ResolveBindingPass`, as well as re-allowing enum types already present in bindings after #44826. Commits ------- c32d749 [DependencyInjection][HttpKernel] Fix enum typed bindings
…num as parameters (ogizanagi) This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [DependencyInjection][FrameworkBundle] Fix using PHP 8.1 enum as parameters | Q | A | ------------- | --- | Branch? | 4.4 <!-- see below --> | Bug fix? | yesish | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #44834 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | N/A While #40857 allowed using enums in DI, it does not allow using these in parameters. This would be the actual fix for #44834, which shows the error you'll get trying to use enum as DI parameters (appart from the binding issue fixed in #44838):  given: ```php namespace App; enum Status { case Draft; case Deleted; case Published; } ``` ```yaml # services.yaml parameters: default_status: !php/const App\Status::Draft ``` The exception happens because the `PhpDumper` misses the leading slash: ```diff protected function getDefaultParameters(): array { return [ - 'default_status' => App\Status::Draft, + 'default_status' => \App\Status::Draft, ]; } ``` While this would fix using enums as DI parameters as of Symfony < 6, the `ParameterBagInterface::get/set` and `ContainerInterface::setParameter/getParameter` are not allowing `\UnitEnum` and adding these in phpdoc is an issue since actual typehints and return types were added as of Symfony 6. => So this PR is a BC break, but hopefully nobody will be hit by it 🤞🏻 (poke `@ismail1432` - https://twitter.com/SmaineDev/status/1476237072826613763?s=20) Commits ------- ac36617 [DependencyInjection][FrameworkBundle] Fix using PHP 8.1 enum as parameters
Symfony version(s) affected
Any version from 4.4 and PHP 8.1
Description
Use syntax
!php/const
with an enum as parameter doesn't work properly when you want to bind the parameter.How to reproduce
but
output

Possible Solution
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered: