diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
index 711b631d54590..3ea0b1b93f731 100644
--- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
+++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
@@ -1812,6 +1812,8 @@ private function dumpValue($value, bool $interpolate = true): string
return $code;
}
+ } elseif ($value instanceof \UnitEnum) {
+ return sprintf('\%s::%s', \get_class($value), $value->name);
} elseif (\is_object($value) || \is_resource($value)) {
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
}
diff --git a/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php
index 02a501bd2c34c..8017fc579aaa2 100644
--- a/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php
+++ b/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php
@@ -313,6 +313,9 @@ private function convertParameters(array $parameters, string $type, \DOMElement
$element->setAttribute('type', 'binary');
$text = $this->document->createTextNode(self::phpToXml(base64_encode($value)));
$element->appendChild($text);
+ } elseif ($value instanceof \UnitEnum) {
+ $element->setAttribute('type', 'constant');
+ $element->appendChild($this->document->createTextNode(self::phpToXml($value)));
} else {
if (\in_array($value, ['null', 'true', 'false'], true)) {
$element->setAttribute('type', 'string');
@@ -366,6 +369,8 @@ public static function phpToXml($value): string
return 'false';
case $value instanceof Parameter:
return '%'.$value.'%';
+ case $value instanceof \UnitEnum:
+ return sprintf('%s::%s', \get_class($value), $value->name);
case \is_object($value) || \is_resource($value):
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
default:
diff --git a/src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php
index c055a686128c2..5dce997d9df9d 100644
--- a/src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php
+++ b/src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php
@@ -286,6 +286,8 @@ private function dumpValue($value)
return $this->getExpressionCall((string) $value);
} elseif ($value instanceof Definition) {
return new TaggedValue('service', (new Parser())->parse("_:\n".$this->addService('_', $value), Yaml::PARSE_CUSTOM_TAGS)['_']['_']);
+ } elseif ($value instanceof \UnitEnum) {
+ return new TaggedValue('php/const', sprintf('%s::%s', \get_class($value), $value->name));
} elseif (\is_object($value) || \is_resource($value)) {
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
}
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php
index b46fbf937b910..3468e35a944c1 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php
@@ -40,6 +40,8 @@
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
use Symfony\Component\DependencyInjection\Tests\Compiler\Wither;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
use Symfony\Component\DependencyInjection\Tests\Fixtures\ScalarFactory;
use Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1;
@@ -1208,6 +1210,29 @@ public function testDumpHandlesObjectClassNames()
$this->assertInstanceOf(\stdClass::class, $container->get('bar'));
}
+ /**
+ * @requires PHP 8.1
+ */
+ public function testDumpHandlesEnumeration()
+ {
+ $container = new ContainerBuilder();
+ $container
+ ->register('foo', FooClassWithEnumAttribute::class)
+ ->setPublic(true)
+ ->addArgument(FooUnitEnum::BAR);
+
+ $container->compile();
+
+ $dumper = new PhpDumper($container);
+ eval('?>'.$dumper->dump([
+ 'class' => 'Symfony_DI_PhpDumper_Test_Enumeration',
+ ]));
+
+ $container = new \Symfony_DI_PhpDumper_Test_Enumeration();
+
+ $this->assertSame(FooUnitEnum::BAR, $container->get('foo')->getBar());
+ }
+
public function testUninitializedSyntheticReference()
{
$container = new ContainerBuilder();
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php
index dda18a306207e..18caa150f278e 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php
@@ -21,6 +21,8 @@
use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
class XmlDumperTest extends TestCase
{
@@ -249,4 +251,21 @@ public function testDumpAbstractServices()
$this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services_abstract.xml'), $dumper->dump());
}
+
+ /**
+ * @requires PHP 8.1
+ */
+ public function testDumpHandlesEnumeration()
+ {
+ $container = new ContainerBuilder();
+ $container
+ ->register(FooClassWithEnumAttribute::class, FooClassWithEnumAttribute::class)
+ ->setPublic(true)
+ ->addArgument(FooUnitEnum::BAR);
+
+ $container->compile();
+ $dumper = new XmlDumper($container);
+
+ $this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services_with_enumeration.xml'), $dumper->dump());
+ }
}
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php
index b359f668d7758..9a973afe69df7 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php
@@ -22,6 +22,8 @@
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;
@@ -129,6 +131,23 @@ public function testServiceClosure()
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_with_service_closure.yml', $dumper->dump());
}
+ /**
+ * @requires PHP 8.1
+ */
+ public function testDumpHandlesEnumeration()
+ {
+ $container = new ContainerBuilder();
+ $container
+ ->register(FooClassWithEnumAttribute::class, FooClassWithEnumAttribute::class)
+ ->setPublic(true)
+ ->addArgument(FooUnitEnum::BAR);
+
+ $container->compile();
+ $dumper = new YamlDumper($container);
+
+ $this->assertEquals(file_get_contents(self::$fixturesPath.'/yaml/services_with_enumeration.yml'), $dumper->dump());
+ }
+
private function assertEqualYamlStructure(string $expected, string $yaml, string $message = '')
{
$parser = new Parser();
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooClassWithEnumAttribute.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooClassWithEnumAttribute.php
new file mode 100644
index 0000000000000..3b2235efdd76b
--- /dev/null
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooClassWithEnumAttribute.php
@@ -0,0 +1,18 @@
+bar = $bar;
+ }
+
+ public function getBar(): FooUnitEnum
+ {
+ return $this->bar;
+ }
+}
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooUnitEnum.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooUnitEnum.php
new file mode 100644
index 0000000000000..d51cf9c995e26
--- /dev/null
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooUnitEnum.php
@@ -0,0 +1,8 @@
+
+
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: