Skip to content

Commit 8d76a70

Browse files
ogizanaginicolas-grekas
authored andcommitted
[DependencyInjection][FrameworkBundle] Fix using PHP 8.1 enum as parameters
1 parent 1e65e6e commit 8d76a70

33 files changed

+82
-39
lines changed

Container.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function getParameterBag()
109109
*
110110
* @param string $name The parameter name
111111
*
112-
* @return array|bool|string|int|float|null
112+
* @return array|bool|string|int|float|\UnitEnum|null
113113
*
114114
* @throws InvalidArgumentException if the parameter is not defined
115115
*/
@@ -133,8 +133,8 @@ public function hasParameter($name)
133133
/**
134134
* Sets a parameter.
135135
*
136-
* @param string $name The parameter name
137-
* @param array|bool|string|int|float|null $value The parameter value
136+
* @param string $name The parameter name
137+
* @param array|bool|string|int|float|\UnitEnum|null $value The parameter value
138138
*/
139139
public function setParameter($name, $value)
140140
{

ContainerInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function initialized($id);
7474
*
7575
* @param string $name The parameter name
7676
*
77-
* @return array|bool|string|int|float|null
77+
* @return array|bool|string|int|float|\UnitEnum|null
7878
*
7979
* @throws InvalidArgumentException if the parameter is not defined
8080
*/
@@ -92,8 +92,8 @@ public function hasParameter($name);
9292
/**
9393
* Sets a parameter.
9494
*
95-
* @param string $name The parameter name
96-
* @param array|bool|string|int|float|null $value The parameter value
95+
* @param string $name The parameter name
96+
* @param array|bool|string|int|float|\UnitEnum|null $value The parameter value
9797
*/
9898
public function setParameter($name, $value);
9999
}

Dumper/PhpDumper.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,10 +1436,11 @@ private function addDefaultParametersMethod(): string
14361436
if ($key !== $resolvedKey = $this->container->resolveEnvPlaceholders($key)) {
14371437
throw new InvalidArgumentException(sprintf('Parameter name cannot use env parameters: "%s".', $resolvedKey));
14381438
}
1439-
$export = $this->exportParameters([$value]);
1439+
$hasEnum = false;
1440+
$export = $this->exportParameters([$value], '', 12, $hasEnum);
14401441
$export = explode('0 => ', substr(rtrim($export, " ]\n"), 2, -1), 2);
14411442

1442-
if (preg_match("/\\\$this->(?:getEnv\('(?:[-.\w]*+:)*+\w++'\)|targetDir\.'')/", $export[1])) {
1443+
if ($hasEnum || preg_match("/\\\$this->(?:getEnv\('(?:[-.\w]*+:)*+\w++'\)|targetDir\.'')/", $export[1])) {
14431444
$dynamicPhp[$key] = sprintf('%scase %s: $value = %s; break;', $export[0], $this->export($key), $export[1]);
14441445
} else {
14451446
$php[] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]);
@@ -1450,7 +1451,7 @@ private function addDefaultParametersMethod(): string
14501451
$code = <<<'EOF'
14511452
14521453
/**
1453-
* @return array|bool|float|int|string|null
1454+
* @return array|bool|float|int|string|\UnitEnum|null
14541455
*/
14551456
public function getParameter($name)
14561457
{
@@ -1545,12 +1546,12 @@ protected function getDefaultParameters(): array
15451546
/**
15461547
* @throws InvalidArgumentException
15471548
*/
1548-
private function exportParameters(array $parameters, string $path = '', int $indent = 12): string
1549+
private function exportParameters(array $parameters, string $path = '', int $indent = 12, bool &$hasEnum = false): string
15491550
{
15501551
$php = [];
15511552
foreach ($parameters as $key => $value) {
15521553
if (\is_array($value)) {
1553-
$value = $this->exportParameters($value, $path.'/'.$key, $indent + 4);
1554+
$value = $this->exportParameters($value, $path.'/'.$key, $indent + 4, $hasEnum);
15541555
} elseif ($value instanceof ArgumentInterface) {
15551556
throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain special arguments. "%s" found in "%s".', \get_class($value), $path.'/'.$key));
15561557
} elseif ($value instanceof Variable) {
@@ -1561,6 +1562,9 @@ private function exportParameters(array $parameters, string $path = '', int $ind
15611562
throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain references to other services (reference to service "%s" found in "%s").', $value, $path.'/'.$key));
15621563
} elseif ($value instanceof Expression) {
15631564
throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain expressions. Expression "%s" found in "%s".', $value, $path.'/'.$key));
1565+
} elseif ($value instanceof \UnitEnum) {
1566+
$hasEnum = true;
1567+
$value = sprintf('\%s::%s', \get_class($value), $value->name);
15641568
} else {
15651569
$value = $this->export($value);
15661570
}

ParameterBag/ContainerBag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function all()
3636
/**
3737
* {@inheritdoc}
3838
*
39-
* @return array|bool|string|int|float|null
39+
* @return array|bool|string|int|float|\UnitEnum|null
4040
*/
4141
public function get($name)
4242
{

ParameterBag/ParameterBagInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function all();
4747
*
4848
* @param string $name The parameter name
4949
*
50-
* @return array|bool|string|int|float|null
50+
* @return array|bool|string|int|float|\UnitEnum|null
5151
*
5252
* @throws ParameterNotFoundException if the parameter is not defined
5353
*/
@@ -63,8 +63,8 @@ public function remove($name);
6363
/**
6464
* Sets a service container parameter.
6565
*
66-
* @param string $name The parameter name
67-
* @param array|bool|string|int|float|null $value The parameter value
66+
* @param string $name The parameter name
67+
* @param array|bool|string|int|float|\UnitEnum|null $value The parameter value
6868
*
6969
* @throws LogicException if the parameter can not be set
7070
*/

Tests/Dumper/PhpDumperTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
2323
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
2424
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
25+
use Symfony\Component\DependencyInjection\Container;
2526
use Symfony\Component\DependencyInjection\ContainerBuilder;
2627
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
2728
use Symfony\Component\DependencyInjection\Definition;
@@ -1217,16 +1218,37 @@ public function testDumpHandlesEnumeration()
12171218
->setPublic(true)
12181219
->addArgument(FooUnitEnum::BAR);
12191220

1221+
$container->setParameter('unit_enum', FooUnitEnum::BAR);
1222+
$container->setParameter('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]);
12201223
$container->compile();
12211224

12221225
$dumper = new PhpDumper($container);
1223-
eval('?>'.$dumper->dump([
1226+
eval('?>'.$dumpedContainer = $dumper->dump([
12241227
'class' => 'Symfony_DI_PhpDumper_Test_Enumeration',
12251228
]));
12261229

1230+
/** @var Container $container */
12271231
$container = new \Symfony_DI_PhpDumper_Test_Enumeration();
12281232

12291233
$this->assertSame(FooUnitEnum::BAR, $container->get('foo')->getBar());
1234+
$this->assertSame(FooUnitEnum::BAR, $container->getParameter('unit_enum'));
1235+
$this->assertSame([FooUnitEnum::BAR, FooUnitEnum::FOO], $container->getParameter('enum_array'));
1236+
$this->assertStringMatchesFormat(<<<'PHP'
1237+
%A
1238+
private function getDynamicParameter(string $name)
1239+
{
1240+
switch ($name) {
1241+
case 'unit_enum': $value = \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR; break;
1242+
case 'enum_array': $value = [
1243+
0 => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR,
1244+
1 => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::FOO,
1245+
]; break;
1246+
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
1247+
}
1248+
%A
1249+
PHP
1250+
, $dumpedContainer
1251+
);
12301252
}
12311253

12321254
public function testUninitializedSyntheticReference()

Tests/Dumper/XmlDumperTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ public function testDumpHandlesEnumeration()
263263
->setPublic(true)
264264
->addArgument(FooUnitEnum::BAR);
265265

266+
$container->setParameter('unit_enum', FooUnitEnum::BAR);
267+
$container->setParameter('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]);
268+
266269
$container->compile();
267270
$dumper = new XmlDumper($container);
268271

Tests/Dumper/YamlDumperTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ public function testDumpHandlesEnumeration()
142142
->setPublic(true)
143143
->addArgument(FooUnitEnum::BAR);
144144

145+
$container->setParameter('unit_enum', FooUnitEnum::BAR);
146+
$container->setParameter('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]);
147+
145148
$container->compile();
146149
$dumper = new YamlDumper($container);
147150

Tests/Fixtures/FooUnitEnum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
enum FooUnitEnum
66
{
77
case BAR;
8+
case FOO;
89
}

Tests/Fixtures/php/services10.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected function getTestService()
6060
}
6161

6262
/**
63-
* @return array|bool|float|int|string|null
63+
* @return array|bool|float|int|string|\UnitEnum|null
6464
*/
6565
public function getParameter($name)
6666
{

0 commit comments

Comments
 (0)