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

Commita3d95d6

Browse files
committed
[FrameworkBundle] Fix descriptors for enum in DI
1 parent2b64b1b commita3d95d6

16 files changed

+178
-9
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/Descriptor.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,20 @@ protected function formatValue($value): string
168168
*/
169169
protectedfunctionformatParameter($value):string
170170
{
171+
if ($valueinstanceof \UnitEnum) {
172+
returnvar_export($value,true);
173+
}
174+
175+
// Recursively search for enum values, so we can replace it
176+
// before json_encode (which will not display anything for \UnitEnum otherwise)
177+
if (\is_array($value)) {
178+
array_walk_recursive($value,staticfunction (&$value) {
179+
if ($valueinstanceof \UnitEnum) {
180+
$value =var_export($value,true);
181+
}
182+
});
183+
}
184+
171185
if (\is_bool($value) ||\is_array($value) || (null ===$value)) {
172186
$jsonString =json_encode($value);
173187

‎src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/JsonDescriptor.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ private function writeData(array $data, array $options)
158158
{
159159
$flags =$options['json_encoding'] ??0;
160160

161+
// Recursively search for enum values, so we can replace it
162+
// before json_encode (which will not display anything for \UnitEnum otherwise)
163+
array_walk_recursive($data,staticfunction (&$value) {
164+
if ($valueinstanceof \UnitEnum) {
165+
$value =var_export($value,true);
166+
}
167+
});
168+
161169
$this->write(json_encode($data,$flags | \JSON_PRETTY_PRINT)."\n");
162170
}
163171

‎src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ protected function describeContainerDefinition(Definition $definition, array $op
342342
$argumentsInformation[] =sprintf('Service locator (%d element(s))',\count($argument->getValues()));
343343
}elseif ($argumentinstanceof Definition) {
344344
$argumentsInformation[] ='Inlined Service';
345+
}elseif ($argumentinstanceof \UnitEnum) {
346+
$argumentsInformation[] =var_export($argument,true);
345347
}else {
346348
$argumentsInformation[] =\is_array($argument) ?sprintf('Array (%d element(s))',\count($argument)) :$argument;
347349
}

‎src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/XmlDescriptor.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ private function getArgumentNodes(array $arguments, \DOMDocument $dom): array
386386
foreach ($this->getArgumentNodes($argument,$dom)as$childArgumentXML) {
387387
$argumentXML->appendChild($childArgumentXML);
388388
}
389+
}elseif ($argumentinstanceof \UnitEnum) {
390+
$argumentXML->setAttribute('type','constant');
391+
$argumentXML->appendChild(new \DOMText(var_export($argument,true)));
389392
}else {
390393
$argumentXML->appendChild(new \DOMText($argument));
391394
}

‎src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/AbstractDescriptorTest.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Bundle\FrameworkBundle\Tests\Console\Descriptor;
1313

1414
usePHPUnit\Framework\TestCase;
15+
useSymfony\Bundle\FrameworkBundle\Tests\Fixtures\FooUnitEnum;
1516
useSymfony\Component\Console\Input\ArrayInput;
1617
useSymfony\Component\Console\Output\BufferedOutput;
1718
useSymfony\Component\Console\Style\SymfonyStyle;
@@ -121,6 +122,10 @@ public function getDescribeContainerDefinitionWithArgumentsShownTestData()
121122
$definitionsWithArgs[str_replace('definition_','definition_arguments_',$key)] =$definition;
122123
}
123124

125+
if (\PHP_VERSION_ID >=80100) {
126+
$definitionsWithArgs['definition_arguments_with_enum'] = (newDefinition('definition_with_enum'))->setArgument(0, FooUnitEnum::FOO);
127+
}
128+
124129
return$this->getDescriptionTestData($definitionsWithArgs);
125130
}
126131

@@ -248,7 +253,7 @@ private function assertDescription($expectedDescription, $describedObject, array
248253
}
249254
}
250255

251-
privatefunctiongetDescriptionTestData(array$objects)
256+
privatefunctiongetDescriptionTestData(iterable$objects)
252257
{
253258
$data = [];
254259
foreach ($objectsas$name =>$object) {

‎src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/ObjectsProvider.php‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespaceSymfony\Bundle\FrameworkBundle\Tests\Console\Descriptor;
1313

14+
useSymfony\Bundle\FrameworkBundle\Tests\Fixtures\FooUnitEnum;
15+
useSymfony\Bundle\FrameworkBundle\Tests\Fixtures\Suit;
1416
useSymfony\Component\DependencyInjection\Alias;
1517
useSymfony\Component\DependencyInjection\Argument\IteratorArgument;
1618
useSymfony\Component\DependencyInjection\ContainerBuilder;
@@ -61,14 +63,24 @@ public static function getRoutes()
6163

6264
publicstaticfunctiongetContainerParameters()
6365
{
64-
return [
65-
'parameters_1' =>newParameterBag([
66-
'integer' =>12,
67-
'string' =>'Hello world!',
68-
'boolean' =>true,
69-
'array' => [12,'Hello world!',true],
70-
]),
71-
];
66+
yield'parameters_1' =>newParameterBag([
67+
'integer' =>12,
68+
'string' =>'Hello world!',
69+
'boolean' =>true,
70+
'array' => [12,'Hello world!',true],
71+
]);
72+
73+
if (\PHP_VERSION_ID >=80100) {
74+
yield'parameters_enums' =>newParameterBag([
75+
'unit_enum' => FooUnitEnum::BAR,
76+
'backed_enum' => Suit::Hearts,
77+
'array_of_enums' => Suit::cases(),
78+
'map' => [
79+
'mixed' => [Suit::Hearts, FooUnitEnum::BAR],
80+
'single' => FooUnitEnum::BAR,
81+
],
82+
]);
83+
}
7284
}
7385

7486
publicstaticfunctiongetContainerParameter()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"class":"definition_with_enum",
3+
"public":false,
4+
"synthetic":false,
5+
"lazy":false,
6+
"shared":true,
7+
"abstract":false,
8+
"autowire":false,
9+
"autoconfigure":false,
10+
"arguments": [
11+
"Symfony\\Bundle\\FrameworkBundle\\Tests\\Fixtures\\FooUnitEnum::FOO"
12+
],
13+
"file":null,
14+
"tags": []
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
- Class:`definition_with_enum`
2+
- Public: no
3+
- Synthetic: no
4+
- Lazy: no
5+
- Shared: yes
6+
- Abstract: no
7+
- Autowired: no
8+
- Autoconfigured: no
9+
- Arguments: yes
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---------------- ----------------------------------------------------------------
2+
 Option   Value 
3+
---------------- ----------------------------------------------------------------
4+
Service ID -
5+
Class definition_with_enum
6+
Tags -
7+
Public no
8+
Synthetic no
9+
Lazy no
10+
Shared yes
11+
Abstract no
12+
Autowired no
13+
Autoconfigured no
14+
Arguments Symfony\Bundle\FrameworkBundle\Tests\Fixtures\FooUnitEnum::FOO
15+
---------------- ----------------------------------------------------------------
16+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<definitionclass="definition_with_enum"public="false"synthetic="false"lazy="false"shared="true"abstract="false"autowired="false"autoconfigured="false"file="">
3+
<argumenttype="constant">Symfony\Bundle\FrameworkBundle\Tests\Fixtures\FooUnitEnum::FOO</argument>
4+
</definition>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp