Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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
merged 1 commit into from
Jan 1, 2022

Conversation

ogizanagi
Copy link
Contributor

@ogizanagi ogizanagi commented Dec 28, 2021

Q A
Branch? 6.1
Bug fix? no
New feature? yes
Deprecations? no
Tickets N/A
License MIT
Doc PR Todo

Given:

namespace App\Model;

enum Suit: string 
{
    case Hearts = 'H';
    case Diamonds = 'D';
    case Clubs = 'C';
    case Spades = 'S';
}

and the controller:

class CardController
{
    #[Route('/cards/{suit}')]
    public function list(Suit $suit): Response
    {
        // [...]
    }
}

A request to /cards/H would inject the Suit::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.

@ogizanagi ogizanagi force-pushed the backed-enum-resolver branch from f5c19a9 to a92f56f Compare December 29, 2021 08:09
@ogizanagi ogizanagi force-pushed the backed-enum-resolver branch from c5eeca7 to a92f56f Compare December 29, 2021 08:47
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
@ogizanagi ogizanagi force-pushed the backed-enum-resolver branch from a92f56f to 48a9d02 Compare December 30, 2021 09:24
@fabpot fabpot force-pushed the backed-enum-resolver branch from 48a9d02 to 8466cfe Compare January 1, 2022 14:51
@fabpot
Copy link
Member

fabpot commented Jan 1, 2022

Thank you @ogizanagi.

@fabpot fabpot merged commit a0eecdf into symfony:6.1 Jan 1, 2022
@ogizanagi ogizanagi deleted the backed-enum-resolver branch January 1, 2022 17:08
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
@fabpot fabpot mentioned this pull request Apr 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants