Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit78c44fb

Browse files
[DependencyInjection] Add support of PHP 8.1 enumerations
1 parent3fd41ce commit78c44fb

21 files changed

+253
-3
lines changed

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

Lines 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‎

Lines 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 ($valueinstanceof \UnitEnum) {
1875+
returnsprintf('\%s::%s',\get_class($value),$value->name);
18741876
}elseif ($valueinstanceof AbstractArgument) {
18751877
thrownewRuntimeException($value->getTextWithContext());
18761878
}elseif (\is_object($value) ||\is_resource($value)) {

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

Lines 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 ($valueinstanceof \UnitEnum) {
331+
$element->setAttribute('type','enumeration');
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$valueinstanceof Parameter:
382385
return'%'.$value.'%';
386+
case$valueinstanceof \UnitEnum:
387+
returnsprintf('%s::%s',\get_class($value),$value->name);
383388
case\is_object($value) ||\is_resource($value):
384389
thrownewRuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
385390
default:

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ private function dumpValue($value)
306306
returnnewTaggedValue('service', (newParser())->parse("_:\n".$this->addService('_',$value), Yaml::PARSE_CUSTOM_TAGS)['_']['_']);
307307
}elseif ($valueinstanceof AbstractArgument) {
308308
returnnewTaggedValue('abstract',$value->getText());
309+
}elseif ($valueinstanceof \UnitEnum) {
310+
returnnewTaggedValue('php/enum',$value);
309311
}elseif (\is_object($value) ||\is_resource($value)) {
310312
thrownewRuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
311313
}

‎src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file
550550
$arguments[$key] =$arg->nodeValue;
551551
break;
552552
case'constant':
553+
case'enumeration':
553554
$arguments[$key] =\constant(trim($arg->nodeValue));
554555
break;
555556
default:

‎src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ protected function loadFile($file)
766766
}
767767

768768
try {
769-
$configuration =$this->yamlParser->parseFile($file, Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS);
769+
$configuration =$this->yamlParser->parseFile($file, Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_ENUM);
770770
}catch (ParseException$e) {
771771
thrownewInvalidArgumentException(sprintf('The file "%s" does not contain valid YAML:',$file).$e->getMessage(),0,$e);
772772
}
@@ -884,6 +884,13 @@ private function resolveServices($value, string $file, bool $isParameter = false
884884
if ('abstract' ===$value->getTag()) {
885885
returnnewAbstractArgument($value->getValue());
886886
}
887+
if ('php/enum' ===$value->getTag()) {
888+
if (\defined($value->getValue())) {
889+
return\constant($value->getValue());
890+
}
891+
892+
thrownewInvalidArgumentException(sprintf('The enumeration case "%s" is not defined.',$value->getValue()));
893+
}
887894

888895
thrownewInvalidArgumentException(sprintf('Unsupported tag "!%s".',$value->getTag()));
889896
}

‎src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@
299299
<xsd:enumerationvalue="collection" />
300300
<xsd:enumerationvalue="string" />
301301
<xsd:enumerationvalue="constant" />
302+
<xsd:enumerationvalue="enumeration" />
302303
<xsd:enumerationvalue="binary" />
303304
</xsd:restriction>
304305
</xsd:simpleType>
@@ -311,6 +312,7 @@
311312
<xsd:enumerationvalue="expression" />
312313
<xsd:enumerationvalue="string" />
313314
<xsd:enumerationvalue="constant" />
315+
<xsd:enumerationvalue="enumeration" />
314316
<xsd:enumerationvalue="binary" />
315317
<xsd:enumerationvalue="iterator" />
316318
<xsd:enumerationvalue="service_locator" />

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
useSymfony\Component\DependencyInjection\Tests\Compiler\Foo;
4343
useSymfony\Component\DependencyInjection\Tests\Compiler\Wither;
4444
useSymfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
45+
useSymfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
46+
useSymfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
4547
useSymfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
4648
useSymfony\Component\DependencyInjection\Tests\Fixtures\ScalarFactory;
4749
useSymfony\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+
publicfunctiontestDumpHandlesEnumeration()
1233+
{
1234+
$container =newContainerBuilder();
1235+
$container
1236+
->register('foo', FooClassWithEnumAttribute::class)
1237+
->setPublic(true)
1238+
->addArgument(FooUnitEnum::BAR);
1239+
1240+
$container->compile();
1241+
1242+
$dumper =newPhpDumper($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
publicfunctiontestUninitializedSyntheticReference()
12281253
{
12291254
$container =newContainerBuilder();

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
useSymfony\Component\DependencyInjection\Dumper\XmlDumper;
2222
useSymfony\Component\DependencyInjection\Loader\XmlFileLoader;
2323
useSymfony\Component\DependencyInjection\Reference;
24+
useSymfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
25+
useSymfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
2426
useSymfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
2527

2628
class XmlDumperTestextends TestCase
@@ -270,4 +272,21 @@ public function testDumpServiceWithAbstractArgument()
270272
$dumper =newXmlDumper($container);
271273
$this->assertStringEqualsFile(self::$fixturesPath.'/xml/services_with_abstract_argument.xml',$dumper->dump());
272274
}
275+
276+
/**
277+
* @requires PHP >= 8.1
278+
*/
279+
publicfunctiontestDumpHandlesEnumeration()
280+
{
281+
$container =newContainerBuilder();
282+
$container
283+
->register(FooClassWithEnumAttribute::class, FooClassWithEnumAttribute::class)
284+
->setPublic(true)
285+
->addArgument(FooUnitEnum::BAR);
286+
287+
$container->compile();
288+
$dumper =newXmlDumper($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‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
useSymfony\Component\DependencyInjection\Dumper\YamlDumper;
2323
useSymfony\Component\DependencyInjection\Loader\YamlFileLoader;
2424
useSymfony\Component\DependencyInjection\Reference;
25+
useSymfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
26+
useSymfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
2527
useSymfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
2628
useSymfony\Component\Yaml\Parser;
2729
useSymfony\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+
publicfunctiontestDumpHandlesEnumeration()
139+
{
140+
$container =newContainerBuilder();
141+
$container
142+
->register(FooClassWithEnumAttribute::class, FooClassWithEnumAttribute::class)
143+
->setPublic(true)
144+
->addArgument(FooUnitEnum::BAR);
145+
146+
$container->compile();
147+
$dumper =newYamlDumper($container);
148+
149+
$this->assertEquals(file_get_contents(self::$fixturesPath.'/yaml/services_with_enumeration.yml'),$dumper->dump());
150+
}
151+
133152
privatefunctionassertEqualYamlStructure(string$expected,string$yaml,string$message ='')
134153
{
135154
$parser =newParser();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp