Skip to content

Commit 7cfcb0e

Browse files
Merge branch '4.4' into 5.4
* 4.4: [Cache] Fix connecting to Redis via a socket file [DependencyInjection][FrameworkBundle] Fix using PHP 8.1 enum as parameters [PropertyAccessor] Add missing TypeError catch [FrameworkBundle] Fix log channel of TagAwareAdapter [HttpClient] Fix Content-Length header when possible [DependencyInjection] Don't dump polyfilled classes in preload script
2 parents 974580f + 8d76a70 commit 7cfcb0e

33 files changed

+82
-39
lines changed

Container.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function getParameterBag()
107107
/**
108108
* Gets a parameter.
109109
*
110-
* @return array|bool|string|int|float|null
110+
* @return array|bool|string|int|float|\UnitEnum|null
111111
*
112112
* @throws InvalidArgumentException if the parameter is not defined
113113
*/
@@ -127,8 +127,8 @@ public function hasParameter(string $name)
127127
/**
128128
* Sets a parameter.
129129
*
130-
* @param string $name The parameter name
131-
* @param array|bool|string|int|float|null $value The parameter value
130+
* @param string $name The parameter name
131+
* @param array|bool|string|int|float|\UnitEnum|null $value The parameter value
132132
*/
133133
public function setParameter(string $name, $value)
134134
{

ContainerInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function has(string $id);
6363
public function initialized(string $id);
6464

6565
/**
66-
* @return array|bool|string|int|float|null
66+
* @return array|bool|string|int|float|\UnitEnum|null
6767
*
6868
* @throws InvalidArgumentException if the parameter is not defined
6969
*/
@@ -77,8 +77,8 @@ public function hasParameter(string $name);
7777
/**
7878
* Sets a parameter.
7979
*
80-
* @param string $name The parameter name
81-
* @param array|bool|string|int|float|null $value The parameter value
80+
* @param string $name The parameter name
81+
* @param array|bool|string|int|float|\UnitEnum|null $value The parameter value
8282
*/
8383
public function setParameter(string $name, $value);
8484
}

Dumper/PhpDumper.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ class %s extends {$options['class']}
341341
if (!$class || str_contains($class, '$') || \in_array($class, ['int', 'float', 'string', 'bool', 'resource', 'object', 'array', 'null', 'callable', 'iterable', 'mixed', 'void'], true)) {
342342
continue;
343343
}
344-
if (!(class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false)) || (new \ReflectionClass($class))->isUserDefined()) {
344+
if (!(class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false)) || ((new \ReflectionClass($class))->isUserDefined() && !\in_array($class, ['Attribute', 'JsonException', 'ReturnTypeWillChange', 'Stringable', 'UnhandledMatchError', 'ValueError'], true))) {
345345
$code[$options['class'].'.preload.php'] .= sprintf("\$classes[] = '%s';\n", $class);
346346
}
347347
}
@@ -1504,10 +1504,11 @@ private function addDefaultParametersMethod(): string
15041504
if ($key !== $resolvedKey = $this->container->resolveEnvPlaceholders($key)) {
15051505
throw new InvalidArgumentException(sprintf('Parameter name cannot use env parameters: "%s".', $resolvedKey));
15061506
}
1507-
$export = $this->exportParameters([$value]);
1507+
$hasEnum = false;
1508+
$export = $this->exportParameters([$value], '', 12, $hasEnum);
15081509
$export = explode('0 => ', substr(rtrim($export, " ]\n"), 2, -1), 2);
15091510

1510-
if (preg_match("/\\\$this->(?:getEnv\('(?:[-.\w]*+:)*+\w++'\)|targetDir\.'')/", $export[1])) {
1511+
if ($hasEnum || preg_match("/\\\$this->(?:getEnv\('(?:[-.\w]*+:)*+\w++'\)|targetDir\.'')/", $export[1])) {
15111512
$dynamicPhp[$key] = sprintf('%scase %s: $value = %s; break;', $export[0], $this->export($key), $export[1]);
15121513
} else {
15131514
$php[] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]);
@@ -1518,7 +1519,7 @@ private function addDefaultParametersMethod(): string
15181519
$code = <<<'EOF'
15191520
15201521
/**
1521-
* @return array|bool|float|int|string|null
1522+
* @return array|bool|float|int|string|\UnitEnum|null
15221523
*/
15231524
public function getParameter(string $name)
15241525
{
@@ -1611,12 +1612,12 @@ protected function getDefaultParameters(): array
16111612
/**
16121613
* @throws InvalidArgumentException
16131614
*/
1614-
private function exportParameters(array $parameters, string $path = '', int $indent = 12): string
1615+
private function exportParameters(array $parameters, string $path = '', int $indent = 12, bool &$hasEnum = false): string
16151616
{
16161617
$php = [];
16171618
foreach ($parameters as $key => $value) {
16181619
if (\is_array($value)) {
1619-
$value = $this->exportParameters($value, $path.'/'.$key, $indent + 4);
1620+
$value = $this->exportParameters($value, $path.'/'.$key, $indent + 4, $hasEnum);
16201621
} elseif ($value instanceof ArgumentInterface) {
16211622
throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain special arguments. "%s" found in "%s".', get_debug_type($value), $path.'/'.$key));
16221623
} elseif ($value instanceof Variable) {
@@ -1627,6 +1628,9 @@ private function exportParameters(array $parameters, string $path = '', int $ind
16271628
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));
16281629
} elseif ($value instanceof Expression) {
16291630
throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain expressions. Expression "%s" found in "%s".', $value, $path.'/'.$key));
1631+
} elseif ($value instanceof \UnitEnum) {
1632+
$hasEnum = true;
1633+
$value = sprintf('\%s::%s', \get_class($value), $value->name);
16301634
} else {
16311635
$value = $this->export($value);
16321636
}

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(string $name)
4242
{

ParameterBag/ParameterBagInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function all();
4545
/**
4646
* Gets a service container parameter.
4747
*
48-
* @return array|bool|string|int|float|null
48+
* @return array|bool|string|int|float|\UnitEnum|null
4949
*
5050
* @throws ParameterNotFoundException if the parameter is not defined
5151
*/
@@ -59,7 +59,7 @@ public function remove(string $name);
5959
/**
6060
* Sets a service container parameter.
6161
*
62-
* @param array|bool|string|int|float|null $value The parameter value
62+
* @param array|bool|string|int|float|\UnitEnum|null $value The parameter value
6363
*
6464
* @throws LogicException if the parameter cannot be set
6565
*/

Tests/Dumper/PhpDumperTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
2525
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
2626
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
27+
use Symfony\Component\DependencyInjection\Container;
2728
use Symfony\Component\DependencyInjection\ContainerBuilder;
2829
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
2930
use Symfony\Component\DependencyInjection\Definition;
@@ -1237,16 +1238,37 @@ public function testDumpHandlesEnumeration()
12371238
->setPublic(true)
12381239
->addArgument(FooUnitEnum::BAR);
12391240

1241+
$container->setParameter('unit_enum', FooUnitEnum::BAR);
1242+
$container->setParameter('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]);
12401243
$container->compile();
12411244

12421245
$dumper = new PhpDumper($container);
1243-
eval('?>'.$dumper->dump([
1246+
eval('?>'.$dumpedContainer = $dumper->dump([
12441247
'class' => 'Symfony_DI_PhpDumper_Test_Enumeration',
12451248
]));
12461249

1250+
/** @var Container $container */
12471251
$container = new \Symfony_DI_PhpDumper_Test_Enumeration();
12481252

12491253
$this->assertSame(FooUnitEnum::BAR, $container->get('foo')->getBar());
1254+
$this->assertSame(FooUnitEnum::BAR, $container->getParameter('unit_enum'));
1255+
$this->assertSame([FooUnitEnum::BAR, FooUnitEnum::FOO], $container->getParameter('enum_array'));
1256+
$this->assertStringMatchesFormat(<<<'PHP'
1257+
%A
1258+
private function getDynamicParameter(string $name)
1259+
{
1260+
switch ($name) {
1261+
case 'unit_enum': $value = \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR; break;
1262+
case 'enum_array': $value = [
1263+
0 => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR,
1264+
1 => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::FOO,
1265+
]; break;
1266+
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
1267+
}
1268+
%A
1269+
PHP
1270+
, $dumpedContainer
1271+
);
12501272
}
12511273

12521274
public function testUninitializedSyntheticReference()

Tests/Dumper/XmlDumperTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ public function testDumpHandlesEnumeration()
285285
->setPublic(true)
286286
->addArgument(FooUnitEnum::BAR);
287287

288+
$container->setParameter('unit_enum', FooUnitEnum::BAR);
289+
$container->setParameter('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]);
290+
288291
$container->compile();
289292
$dumper = new XmlDumper($container);
290293

Tests/Dumper/YamlDumperTest.php

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

147+
$container->setParameter('unit_enum', FooUnitEnum::BAR);
148+
$container->setParameter('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]);
149+
147150
$container->compile();
148151
$dumper = new YamlDumper($container);
149152

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
@@ -57,7 +57,7 @@ protected function getTestService()
5757
}
5858

5959
/**
60-
* @return array|bool|float|int|string|null
60+
* @return array|bool|float|int|string|\UnitEnum|null
6161
*/
6262
public function getParameter(string $name)
6363
{

0 commit comments

Comments
 (0)