-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
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
to
1ec04e4
Compare
acd888b
to
ee790c4
Compare
derrabus
reviewed
Dec 28, 2021
src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/BackedEnumValueResolver.php
Outdated
Show resolved
Hide resolved
f5c19a9
to
a92f56f
Compare
ogizanagi
commented
Dec 29, 2021
src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/BackedEnumValueResolver.php
Show resolved
Hide resolved
c5eeca7
to
a92f56f
Compare
nicolas-grekas
added a commit
that referenced
this pull request
Dec 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/A Same as #43050 for inlined docblock comments (mainly `class-string<FQCN>`) Commits ------- dbc3eea Suppress psalm error for UndefinedDocblockClass for PHP 8.1 classes
a92f56f
to
48a9d02
Compare
chalasr
approved these changes
Dec 31, 2021
fabpot
approved these changes
Jan 1, 2022
48a9d02
to
8466cfe
Compare
Thank you @ogizanagi. |
24 tasks
fabpot
added a commit
that referenced
this pull request
Apr 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 #44831 I'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 @ogizanagi Commits ------- ce87606 [Routing] Add EnumRequirement to help generate route requirements from a \BackedEnum
symfony-splitter
pushed a commit
to symfony/routing
that referenced
this pull request
Apr 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 symfony/symfony#44831 I'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 @ogizanagi Commits ------- ce876065ff [Routing] Add EnumRequirement to help generate route requirements from a \BackedEnum
Merged
Sign up for free
to 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.
Given:
and the controller:
A request to
/cards/H
would inject theSuit::Hearts
enum case into the controller$suit
argument.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.