From c37c67c5845eb379101b5bdca2b38a821cba0f13 Mon Sep 17 00:00:00 2001 From: Aleksey Polyvanyi Date: Sat, 29 Oct 2022 16:57:41 +0200 Subject: [PATCH] -allow enum as service parameter in php config files --- .../Configurator/AbstractConfigurator.php | 3 ++- .../config/services_with_enumeration.php | 23 +++++++++++++++++++ .../Tests/Loader/PhpFileLoaderTest.php | 15 ++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/services_with_enumeration.php diff --git a/src/Symfony/Component/DependencyInjection/Loader/Configurator/AbstractConfigurator.php b/src/Symfony/Component/DependencyInjection/Loader/Configurator/AbstractConfigurator.php index 9276f0a6b753a..da0b85f4dc2b5 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/Configurator/AbstractConfigurator.php +++ b/src/Symfony/Component/DependencyInjection/Loader/Configurator/AbstractConfigurator.php @@ -56,7 +56,7 @@ public function __wakeup() /** * Checks that a value is valid, optionally replacing Definition and Reference configurators by their configure value. * - * @param bool $allowServices whether Definition and Reference are allowed; by default, only scalars and arrays are + * @param bool $allowServices whether Definition and Reference are allowed; by default, only scalars, arrays and enum are * * @return mixed the value, optionally cast to a Definition/Reference */ @@ -98,6 +98,7 @@ public static function processValue(mixed $value, bool $allowServices = false): switch (true) { case null === $value: case \is_scalar($value): + case $value instanceof \UnitEnum: return $value; case $value instanceof ArgumentInterface: diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/services_with_enumeration.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/services_with_enumeration.php new file mode 100644 index 0000000000000..6499081f248d5 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/services_with_enumeration.php @@ -0,0 +1,23 @@ +parameters() + ->set('unit_enum', FooUnitEnum::BAR) + ->set('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]); + + $services = $containerConfigurator->services(); + + $services->defaults()->public(); + + $services->set('service_container', ContainerInterface::class) + ->synthetic(); + + $services->set(FooClassWithEnumAttribute::class) + ->args([FooUnitEnum::BAR]); +}; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php index 4187f9861ce10..ef153e178bc03 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php @@ -21,6 +21,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 { @@ -165,6 +167,19 @@ public function testEnvConfigurator() $this->assertSame('%env(int:CCC)%', $container->getDefinition('foo')->getArgument(0)); } + 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()); + } + public function testNestedBundleConfigNotAllowed() { $fixtures = realpath(__DIR__.'/../Fixtures');