Skip to content
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

[DependencyInjection] Add support of PHP enumerations #40857

Conversation

alexandre-daubois
Copy link
Contributor

@alexandre-daubois alexandre-daubois commented Apr 18, 2021

Q A
Branch? 4.4
Bug fix? yes (new PHP version compatibility)
New feature? no
Deprecations? no
Tickets Fix #40233
License MIT
Doc PR (see below)

Added support of enums using !php/const tag, as they work the same way.

@carsonbot
Copy link

Hey!

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

Cheers!

Carsonbot

@alexandre-daubois alexandre-daubois force-pushed the feat-dependency-injection-enum-support branch 2 times, most recently from 2ff81ae to d3e9060 Compare April 19, 2021 11:16
@derrabus derrabus added this to the 5.x milestone Apr 19, 2021
@alexandre-daubois alexandre-daubois force-pushed the feat-dependency-injection-enum-support branch from d3e9060 to c4adfe1 Compare April 19, 2021 14:30
@alexandre-daubois alexandre-daubois changed the base branch from 5.4 to 5.3 June 4, 2021 06:34
@alexandre-daubois alexandre-daubois changed the title [DependencyInjection] Add support of PHP 8.1 enumerations [DependencyInjection] Add support of PHP enumerations Jun 4, 2021
@alexandre-daubois alexandre-daubois force-pushed the feat-dependency-injection-enum-support branch from 6abf811 to 40ababd Compare June 4, 2021 06:48
@stof
Copy link
Member

stof commented Jun 4, 2021

@nicolas-grekas @fabpot should this be merged in 4.4 as it is about supporting a new PHP version ?

@@ -127,6 +127,8 @@ public static function dump($value, int $flags = 0): string
return self::dumpNull($flags);
case $value instanceof \DateTimeInterface:
return $value->format('c');
case $value instanceof \UnitEnum:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we add new things in the Yaml component, I'd really appreciate to add some tests there covering this kind of feature

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added one here. Not sure if there's another one to add? This one should be enough if I'm right.

@@ -127,6 +127,8 @@ public static function dump($value, int $flags = 0): string
return self::dumpNull($flags);
case $value instanceof \DateTimeInterface:
return $value->format('c');
case $value instanceof \UnitEnum:
Copy link
Member

@derrabus derrabus Jun 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to adjust the DI component's composer.json?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, the current composer.json seems good with "^4.4|^5.0"

@alexandre-daubois alexandre-daubois force-pushed the feat-dependency-injection-enum-support branch from 40ababd to 5248a43 Compare June 7, 2021 10:57
@alexandre-daubois alexandre-daubois force-pushed the feat-dependency-injection-enum-support branch from 1a36bef to 1c43829 Compare June 22, 2021 06:36
@alexandre-daubois alexandre-daubois changed the base branch from 5.3 to 4.4 June 22, 2021 06:36
@alexandre-daubois alexandre-daubois force-pushed the feat-dependency-injection-enum-support branch 2 times, most recently from 8bb428a to bb4eb0d Compare June 22, 2021 16:15
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last nitpicking :)

src/Symfony/Component/Yaml/Inline.php Outdated Show resolved Hide resolved
@alexandre-daubois alexandre-daubois force-pushed the feat-dependency-injection-enum-support branch from bb4eb0d to 8f29ff3 Compare June 22, 2021 18:06
@nicolas-grekas nicolas-grekas force-pushed the feat-dependency-injection-enum-support branch from 8f29ff3 to 88c69c0 Compare June 23, 2021 19:07
@nicolas-grekas
Copy link
Member

Thank you @alexandre-daubois.

@nicolas-grekas nicolas-grekas merged commit ef06f33 into symfony:4.4 Jun 23, 2021
@alexandre-daubois alexandre-daubois deleted the feat-dependency-injection-enum-support branch June 23, 2021 21:48
This was referenced Jun 30, 2021
nicolas-grekas added a commit that referenced this pull request Feb 4, 2022
…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):

![image](https://user-images.githubusercontent.com/2211145/147762495-f57f1fff-f56a-4e87-bbc4-3bba97a8e81b.png)

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
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.

[DI] Support PHP 8.1 enums when dumping the container
7 participants