Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[HttpKernel] Add a controller argument resolver for backed enums#44831
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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
8b96d07 to1ec04e4Compareacd888b toee790c4Comparesrc/Symfony/Component/HttpKernel/Controller/ArgumentResolver/BackedEnumValueResolver.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
f5c19a9 toa92f56fComparesrc/Symfony/Component/HttpKernel/Controller/ArgumentResolver/BackedEnumValueResolver.phpShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
c5eeca7 toa92f56fComparenicolas-grekas added a commit that referenced this pull requestDec 29, 2021
…8.1 classes (ogizanagi)This PR was merged into the 4.4 branch.Discussion----------Suppress psalm error for UndefinedDocblockClass for PHP 8.1 classes| Q | A| ------------- | ---| Branch? | 4.4 <!-- see below -->| Bug fix? | no| New feature? | no <!-- please update src/**/CHANGELOG.md files -->| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->| Tickets |#44831 (comment) <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->| License | MIT| Doc PR | N/ASame as#43050 for inlined docblock comments (mainly `class-string<FQCN>`)Commits-------dbc3eea Suppress psalm error for UndefinedDocblockClass for PHP 8.1 classes
a92f56f to48a9d02Comparechalasr approved these changesDec 31, 2021
fabpot approved these changesJan 1, 2022
Member
fabpot commentedJan 1, 2022
Thank you@ogizanagi. |
24 tasks
fabpot added a commit that referenced this pull requestApr 12, 2022
…equirements from a \BackedEnum (fancyweb)This PR was merged into the 6.1 branch.Discussion----------[Routing] Add EnumRequirement to help generate route requirements from a \BackedEnum| Q | A| ------------- | ---| Branch? | 6.1| Bug fix? | no| New feature? | yes| Deprecations? | no| Tickets | -| License | MIT| Doc PR | -Ref#44831I'd like to limit a route parameter allowed values to the backed values of an enum to use it in conjunction with the new `\BackedEnum` argument resolver (ie fail from the start).Also, sometimes, I'd like to limit it only to a subset of the backed values.I couldn't find a way to do that because enums can't implement `__toString()` and accessing `->value` is not considered a constant operation.We can leverage the fact that route requirements can be a `\Stringable`.Before (no enum):```php#[Route(path: '/foo/{bar}', requirements: ['bar' => FooEnum::AAA.'|'.FooEnum::BBB])]```Allow all enum cases:```php#[Route(path: '/foo/{bar}', requirements: ['bar' => new EnumRequirement(Foo::class)])]```Allow a subset:```php#[Route(path: '/foo/{bar}', requirements: ['bar' => new EnumRequirement(Foo::class, Foo::Aaa, Foo::Bbb)])]```Probably not the best solution but I hope we can find something for that use case for 6.1 😄cc@ogizanagiCommits-------ce87606 [Routing] Add EnumRequirement to help generate route requirements from a \BackedEnum
symfony-splitter pushed a commit to symfony/routing that referenced this pull requestApr 12, 2022
…equirements from a \BackedEnum (fancyweb)This PR was merged into the 6.1 branch.Discussion----------[Routing] Add EnumRequirement to help generate route requirements from a \BackedEnum| Q | A| ------------- | ---| Branch? | 6.1| Bug fix? | no| New feature? | yes| Deprecations? | no| Tickets | -| License | MIT| Doc PR | -Refsymfony/symfony#44831I'd like to limit a route parameter allowed values to the backed values of an enum to use it in conjunction with the new `\BackedEnum` argument resolver (ie fail from the start).Also, sometimes, I'd like to limit it only to a subset of the backed values.I couldn't find a way to do that because enums can't implement `__toString()` and accessing `->value` is not considered a constant operation.We can leverage the fact that route requirements can be a `\Stringable`.Before (no enum):```php#[Route(path: '/foo/{bar}', requirements: ['bar' => FooEnum::AAA.'|'.FooEnum::BBB])]```Allow all enum cases:```php#[Route(path: '/foo/{bar}', requirements: ['bar' => new EnumRequirement(Foo::class)])]```Allow a subset:```php#[Route(path: '/foo/{bar}', requirements: ['bar' => new EnumRequirement(Foo::class, Foo::Aaa, Foo::Bbb)])]```Probably not the best solution but I hope we can find something for that use case for 6.1 😄cc@ogizanagiCommits-------ce876065ff [Routing] Add EnumRequirement to help generate route requirements from a \BackedEnum
Merged
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading.Please reload this page.
Given:
and the controller:
A request to
/cards/Hwould inject theSuit::Heartsenum case into the controller$suitargument.This core resolver aims to resolve backed enum from route path parameters, so we assume a 404 Not Found should be thrown on an invalid backing value provided.