Skip to content

Commit ac36617

Browse files
ogizanaginicolas-grekas
authored andcommitted
[DependencyInjection][FrameworkBundle] Fix using PHP 8.1 enum as parameters
1 parent 582f572 commit ac36617

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+265
-52
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/Controller/AbstractController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function setContainer(ContainerInterface $container): ?ContainerInterface
5858
/**
5959
* Gets a container parameter by its name.
6060
*
61-
* @return array|bool|float|int|string|null
61+
* @return array|bool|float|int|string|\UnitEnum|null
6262
*
6363
* @final
6464
*/

src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ abstract class Controller implements ContainerAwareInterface
3131
/**
3232
* Gets a container configuration parameter by its name.
3333
*
34-
* @return array|bool|float|int|string|null
34+
* @return array|bool|float|int|string|\UnitEnum|null
3535
*
3636
* @final
3737
*/

src/Symfony/Bundle/FrameworkBundle/Test/TestContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function getParameterBag(): ParameterBagInterface
5959
/**
6060
* {@inheritdoc}
6161
*
62-
* @return array|bool|float|int|string|null
62+
* @return array|bool|float|int|string|\UnitEnum|null
6363
*/
6464
public function getParameter($name)
6565
{

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: 22 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,26 @@ 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+
return;
75+
}
76+
77+
yield 'parameters_enums' => new ParameterBag([
78+
'unit_enum' => FooUnitEnum::BAR,
79+
'backed_enum' => Suit::Hearts,
80+
'array_of_enums' => Suit::cases(),
81+
'map' => [
82+
'mixed' => [Suit::Hearts, FooUnitEnum::BAR],
83+
'single' => FooUnitEnum::BAR,
84+
],
85+
]);
7286
}
7387

7488
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+
}

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