Skip to content

Commit a3d95d6

Browse files
committed
[FrameworkBundle] Fix descriptors for enum in DI
1 parent 2b64b1b commit a3d95d6

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
protected function formatParameter($value): string
170170
{
171+
if ($value instanceof \UnitEnum) {
172+
return var_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, static function (&$value) {
179+
if ($value instanceof \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, static function (&$value) {
164+
if ($value instanceof \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 ($argument instanceof Definition) {
344344
$argumentsInformation[] = 'Inlined Service';
345+
} elseif ($argument instanceof \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 ($argument instanceof \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
namespace Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\FooUnitEnum;
1516
use Symfony\Component\Console\Input\ArrayInput;
1617
use Symfony\Component\Console\Output\BufferedOutput;
1718
use Symfony\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'] = (new Definition('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-
private function getDescriptionTestData(array $objects)
256+
private function getDescriptionTestData(iterable $objects)
252257
{
253258
$data = [];
254259
foreach ($objects as $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
namespace Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor;
1313

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

6264
public static function getContainerParameters()
6365
{
64-
return [
65-
'parameters_1' => new ParameterBag([
66-
'integer' => 12,
67-
'string' => 'Hello world!',
68-
'boolean' => true,
69-
'array' => [12, 'Hello world!', true],
70-
]),
71-
];
66+
yield 'parameters_1' => new ParameterBag([
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' => new ParameterBag([
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
public static function getContainerParameter()
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+
<definition class="definition_with_enum" public="false" synthetic="false" lazy="false" shared="true" abstract="false" autowired="false" autoconfigured="false" file="">
3+
<argument type="constant">Symfony\Bundle\FrameworkBundle\Tests\Fixtures\FooUnitEnum::FOO</argument>
4+
</definition>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy