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

Skip to content

Cannot set an enum as argument in services.php #50275

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

Closed
ludekbenedik opened this issue May 9, 2023 · 4 comments
Closed

Cannot set an enum as argument in services.php #50275

ludekbenedik opened this issue May 9, 2023 · 4 comments

Comments

@ludekbenedik
Copy link

Symfony version(s) affected

5.4.23

Description

Although Symfony 5.4 allows set an enum as an argument in DI Definition, it is not possible set the enum as the argument in Configurator.

Works:

protected function build(ContainerBuilder $container)
{
    $container->register(MyService::class)
        ->setPublic(true)
        ->setArgument('$myEnum', MyEnum::First)
    ;
}

Not works:

return static function (ContainerConfigurator $configurator): void {
    $configurator->services()->set(MyService::class)
        ->public()
        ->arg('$myEnum', MyEnum::First)
    ;
};

Throws: Cannot use values of type "App\Service\MyEnum" in service configuration files.

How to reproduce

config/services.php

$configurator->services()->set(MyService::class)
    ->arg('$myEnum', MyEnum::First)
;

src/Service/MyEnum.php

<?php

namespace App\Service;

enum MyEnum: string
{
    case First = 'first';
    case Second = 'second';
}

src/Service/MyService.php

<?php

namespace App\Service;

class MyService
{
    public function __construct(MyEnum $myEnum)
    {
    }
}

Possible Solution

Symfony\Component\DependencyInjection\Loader\Configurator\AbstractConfigurator::processValue

Line 102

Replace

case null === $value:
case \is_scalar($value):
    return $value;

by

case null === $value:
case \is_scalar($value):
case $value instanceof \UnitEnum:
    return $value;

Additional Context

No response

@stof
Copy link
Member

stof commented May 10, 2023

This change makes sense. Can you submit a PR with the fix (and a test preventing regressions) ?

@nicolas-grekas
Copy link
Member

This works on 6.2 since #48045

@ludekbenedik
Copy link
Author

I wrote some relevant arguments why this fix/feature should be merged to 5.4 LTS.

@stof
Copy link
Member

stof commented May 16, 2023

features are never merged into patch versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants