Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Routing] Add EnumRequirement to help generate route requirements from a \BackedEnum#45803

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

Conversation

@fancyweb
Copy link
Contributor

@fancywebfancyweb commentedMar 21, 2022
edited by nicolas-grekas
Loading

QA
Branch?6.1
Bug fix?no
New feature?yes
Deprecations?no
Tickets-
LicenseMIT
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):

#[Route(path:'/foo/{bar}', requirements: ['bar' => FooEnum::AAA.'|'.FooEnum::BBB])]

Allow all enum cases:

#[Route(path:'/foo/{bar}', requirements: ['bar' =>newEnumRequirement(Foo::class)])]

Allow a subset:

#[Route(path:'/foo/{bar}', requirements: ['bar' =>newEnumRequirement(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

@carsonbot
Copy link

Hey!

I think@usu has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@ogizanagi
Copy link
Contributor

Note: the alternative mentioned on Slack was to use a PHP 8 attribute to configure the\BackedEnum argument resolver instead, but perhaps not the best idea, since it'll be slightly less performant and wouldn't allow multiple routes with different requirements on contrary of this suggestion.

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedMar 22, 2022
edited
Loading

If we really want to support subsets, we should give the subset of "what".
Wouldn't this be a cleaner signature?
BackedEnumRequirement(class-string<T> $enum, ...BackedEnum<T> $cases)

Honestly, I find this quite ugly... but 🤷

@nicolas-grekas
Copy link
Member

What about renaming BackedEnumRequirement to EnumRequirement? I feel like adding "Backed" doesn't provide anything.

GwendolenLynch reacted with thumbs up emoji

@fancyweb
Copy link
ContributorAuthor

\UnitEnum enums are not supported.EnumRequirement could imply the class supports both\BackedEnum and\UnitEnum.

I'll change the signature toBackedEnumRequirement(class-string<T> $enum, ...BackedEnum<T> $cases) and throw if a $case is not part of $enum.

@nicolas-grekas
Copy link
Member

EnumRequirement could imply the class supports both \BackedEnum and \UnitEnum

It cannot since it will throw once ppl give it a try - and it's a logical error to expect that a pure enum could work as a part of matching a URL, so ppl that could have this expectation are wrong anyway. "BackedEnum" just adds to the ugliness of using enums to me...

@fancywebfancywebforce-pushed therouting/backed-enum-requirement branch 2 times, most recently fromcb68e74 to275d111CompareMarch 31, 2022 13:04
@fancywebfancywebforce-pushed therouting/backed-enum-requirement branch from275d111 toce87606CompareMarch 31, 2022 13:04
@fancywebfancyweb changed the title[Routing] Add BackedEnumRequirement to help generate route requirements from a \BackedEnum[Routing] Add EnumRequirement to help generate route requirements from a \BackedEnumMar 31, 2022
@fabpot
Copy link
Member

Thank you@fancyweb.

@fabpotfabpot merged commit9cbc853 intosymfony:6.1Apr 12, 2022
@fancywebfancyweb deleted the routing/backed-enum-requirement branchApril 12, 2022 07:49
@fabpotfabpot mentioned this pull requestApr 15, 2022
@tarlepp
Copy link
Contributor

Similar solution for#[IsGranted(SomeEnum::FOO)] would also be great.

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedJun 21, 2022
edited
Loading

#[Route(path: '/foo/{bar}', requirements: ['bar' => FooEnum::AAA.'|'.FooEnum::BBB])]

Similar solution for #[IsGranted(SomeEnum::FOO)] would also be great.

Both ways could be supported natively ifphp/php-src#8825 were available, reducing the boilerplate needed for common simple cases.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@stofstofstof approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

6.1

Development

Successfully merging this pull request may close these issues.

7 participants

@fancyweb@carsonbot@ogizanagi@nicolas-grekas@fabpot@tarlepp@stof

[8]ページ先頭

©2009-2025 Movatter.jp