Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c4adfe1

Browse filesBrowse files
[DependencyInjection] Add support of PHP 8.1 enumerations
1 parent 3fd41ce commit c4adfe1
Copy full SHA for c4adfe1
Expand file treeCollapse file tree

16 files changed

+196
-0
lines changed

‎src/Symfony/Component/DependencyInjection/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ CHANGELOG
1717
* Add `env()` and `EnvConfigurator` in the PHP-DSL
1818
* Add support for `ConfigBuilder` in the `PhpFileLoader`
1919
* Add `ContainerConfigurator::env()` to get the current environment
20+
* Add support of PHP 8.1 enumerations
2021

2122
5.2.0
2223
-----

‎src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,8 @@ private function dumpValue($value, bool $interpolate = true): string
18711871

18721872
return $code;
18731873
}
1874+
} elseif ($value instanceof \UnitEnum) {
1875+
return sprintf('\%s::%s', \get_class($value), $value->name);
18741876
} elseif ($value instanceof AbstractArgument) {
18751877
throw new RuntimeException($value->getTextWithContext());
18761878
} elseif (\is_object($value) || \is_resource($value)) {

‎src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ private function convertParameters(array $parameters, string $type, \DOMElement
327327
$element->setAttribute('type', 'abstract');
328328
$text = $this->document->createTextNode(self::phpToXml($value->getText()));
329329
$element->appendChild($text);
330+
} elseif ($value instanceof \UnitEnum) {
331+
$element->setAttribute('type', 'constant');
332+
$element->appendChild($this->document->createTextNode(self::phpToXml($value)));
330333
} else {
331334
if (\in_array($value, ['null', 'true', 'false'], true)) {
332335
$element->setAttribute('type', 'string');
@@ -380,6 +383,8 @@ public static function phpToXml($value): string
380383
return 'false';
381384
case $value instanceof Parameter:
382385
return '%'.$value.'%';
386+
case $value instanceof \UnitEnum:
387+
return sprintf('%s::%s', \get_class($value), $value->name);
383388
case \is_object($value) || \is_resource($value):
384389
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
385390
default:

‎src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ private function dumpValue($value)
306306
return new TaggedValue('service', (new Parser())->parse("_:\n".$this->addService('_', $value), Yaml::PARSE_CUSTOM_TAGS)['_']['_']);
307307
} elseif ($value instanceof AbstractArgument) {
308308
return new TaggedValue('abstract', $value->getText());
309+
} elseif ($value instanceof \UnitEnum) {
310+
return new TaggedValue('php/const', sprintf('%s::%s', \get_class($value), $value->name));
309311
} elseif (\is_object($value) || \is_resource($value)) {
310312
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
311313
}

‎src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
4343
use Symfony\Component\DependencyInjection\Tests\Compiler\Wither;
4444
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
45+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
46+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
4547
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
4648
use Symfony\Component\DependencyInjection\Tests\Fixtures\ScalarFactory;
4749
use Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator;
@@ -1224,6 +1226,29 @@ public function testDumpHandlesObjectClassNames()
12241226
$this->assertInstanceOf(\stdClass::class, $container->get('bar'));
12251227
}
12261228

1229+
/**
1230+
* @requires PHP 8.1
1231+
*/
1232+
public function testDumpHandlesEnumeration()
1233+
{
1234+
$container = new ContainerBuilder();
1235+
$container
1236+
->register('foo', FooClassWithEnumAttribute::class)
1237+
->setPublic(true)
1238+
->addArgument(FooUnitEnum::BAR);
1239+
1240+
$container->compile();
1241+
1242+
$dumper = new PhpDumper($container);
1243+
eval('?>'.$dumper->dump([
1244+
'class' => 'Symfony_DI_PhpDumper_Test_Enumeration',
1245+
]));
1246+
1247+
$container = new \Symfony_DI_PhpDumper_Test_Enumeration();
1248+
1249+
$this->assertSame(FooUnitEnum::BAR, $container->get('foo')->getBar());
1250+
}
1251+
12271252
public function testUninitializedSyntheticReference()
12281253
{
12291254
$container = new ContainerBuilder();

‎src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
2222
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2323
use Symfony\Component\DependencyInjection\Reference;
24+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
25+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
2426
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
2527

2628
class XmlDumperTest extends TestCase
@@ -270,4 +272,21 @@ public function testDumpServiceWithAbstractArgument()
270272
$dumper = new XmlDumper($container);
271273
$this->assertStringEqualsFile(self::$fixturesPath.'/xml/services_with_abstract_argument.xml', $dumper->dump());
272274
}
275+
276+
/**
277+
* @requires PHP 8.1
278+
*/
279+
public function testDumpHandlesEnumeration()
280+
{
281+
$container = new ContainerBuilder();
282+
$container
283+
->register(FooClassWithEnumAttribute::class, FooClassWithEnumAttribute::class)
284+
->setPublic(true)
285+
->addArgument(FooUnitEnum::BAR);
286+
287+
$container->compile();
288+
$dumper = new XmlDumper($container);
289+
290+
$this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services_with_enumeration.xml'), $dumper->dump());
291+
}
273292
}

‎src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
2323
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
2424
use Symfony\Component\DependencyInjection\Reference;
25+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
26+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
2527
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
2628
use Symfony\Component\Yaml\Parser;
2729
use Symfony\Component\Yaml\Yaml;
@@ -130,6 +132,23 @@ public function testDumpServiceWithAbstractArgument()
130132
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_with_abstract_argument.yml', $dumper->dump());
131133
}
132134

135+
/**
136+
* @requires PHP 8.1
137+
*/
138+
public function testDumpHandlesEnumeration()
139+
{
140+
$container = new ContainerBuilder();
141+
$container
142+
->register(FooClassWithEnumAttribute::class, FooClassWithEnumAttribute::class)
143+
->setPublic(true)
144+
->addArgument(FooUnitEnum::BAR);
145+
146+
$container->compile();
147+
$dumper = new YamlDumper($container);
148+
149+
$this->assertEquals(file_get_contents(self::$fixturesPath.'/yaml/services_with_enumeration.yml'), $dumper->dump());
150+
}
151+
133152
private function assertEqualYamlStructure(string $expected, string $yaml, string $message = '')
134153
{
135154
$parser = new Parser();
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
class FooClassWithEnumAttribute
6+
{
7+
private FooUnitEnum $bar;
8+
9+
public function __construct(FooUnitEnum $bar)
10+
{
11+
$this->bar = $bar;
12+
}
13+
14+
public function getBar(): FooUnitEnum
15+
{
16+
return $this->bar;
17+
}
18+
}
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
enum FooUnitEnum
6+
{
7+
case BAR;
8+
}
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="true" synthetic="true"/>
5+
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute" class="Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute" public="true">
6+
<argument type="constant">Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR</argument>
7+
</service>
8+
</services>
9+
</container>

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.