Skip to content

[DependencyInjection] Allow using enums in container configurator #50330

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static function processValue($value, $allowServices = false)
switch (true) {
case null === $value:
case \is_scalar($value):
case $value instanceof \UnitEnum:
return $value;

case $value instanceof ArgumentInterface:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;

return function (ContainerConfigurator $container) {
$container->parameters()
->set('unit_enum', FooUnitEnum::BAR)
->set('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]);

$services = $container->services();

$services->defaults()->public();

$services->set('service_container', ContainerInterface::class)
->synthetic();

$services->set(FooClassWithEnumAttribute::class)
->args([FooUnitEnum::BAR]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;

class PhpFileLoaderTest extends TestCase
{
Expand Down Expand Up @@ -165,6 +167,22 @@ public function testEnvConfigurator()
$this->assertSame('%env(int:CCC)%', $container->getDefinition('foo')->getArgument(0));
}

/**
* @requires PHP 8.1
*/
public function testEnumeration()
{
$fixtures = realpath(__DIR__.'/../Fixtures');
$container = new ContainerBuilder();
$loader = new PhpFileLoader($container, new FileLocator($fixtures.'/config'));
$loader->load('services_with_enumeration.php');

$container->compile();

$definition = $container->getDefinition(FooClassWithEnumAttribute::class);
$this->assertSame([FooUnitEnum::BAR], $definition->getArguments());
}

/**
* @group legacy
*/
Expand Down