Skip to content

Commit 6c566c7

Browse files
Merge branch '5.4' into 6.0
* 5.4: [Semaphore] fix tests [HttpClient] fix destructing CurlResponse [Cache] Fix connecting to Redis via a socket file [DependencyInjection][FrameworkBundle] Fix using PHP 8.1 enum as parameters [PropertyAccessor] Add missing TypeError catch [HttpKernel] Fixed error count by log not displayed in WebProfilerBundle Added `kernel.event_listener` to the default list of behavior describing tags, fixing AsEventListener attribute not working on decorators. [WebProfilerBundle] Fixes weird spacing in log message context/trace output [Notifier] fix Microsoft Teams webhook url [FrameworkBundle] Fix log channel of TagAwareAdapter [Postmark] ensure only a single tag can be used with Postmark [Mailer] allow Mailchimp to handle multiple TagHeader's [HttpClient] Fix Content-Length header when possible [DependencyInjection] Don't dump polyfilled classes in preload script
2 parents bf53dbf + 7cfcb0e commit 6c566c7

34 files changed

+81
-38
lines changed

Container.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function getParameterBag(): ParameterBagInterface
103103
/**
104104
* Gets a parameter.
105105
*
106-
* @return array|bool|string|int|float|null
106+
* @return array|bool|string|int|float|\UnitEnum|null
107107
*
108108
* @throws InvalidArgumentException if the parameter is not defined
109109
*/
@@ -117,7 +117,7 @@ public function hasParameter(string $name): bool
117117
return $this->parameterBag->has($name);
118118
}
119119

120-
public function setParameter(string $name, array|bool|string|int|float|null $value)
120+
public function setParameter(string $name, array|bool|string|int|float|\UnitEnum|null $value)
121121
{
122122
$this->parameterBag->set($name, $value);
123123
}

ContainerInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ public function has(string $id): bool;
4848
public function initialized(string $id): bool;
4949

5050
/**
51-
* @return array|bool|string|int|float|null
51+
* @return array|bool|string|int|float|\UnitEnum|null
5252
*
5353
* @throws InvalidArgumentException if the parameter is not defined
5454
*/
5555
public function getParameter(string $name);
5656

5757
public function hasParameter(string $name): bool;
5858

59-
public function setParameter(string $name, array|bool|string|int|float|null $value);
59+
public function setParameter(string $name, array|bool|string|int|float|\UnitEnum|null $value);
6060
}

Dumper/PhpDumper.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ class %s extends {$options['class']}
334334
if (!$class || str_contains($class, '$') || \in_array($class, ['int', 'float', 'string', 'bool', 'resource', 'object', 'array', 'null', 'callable', 'iterable', 'mixed', 'void'], true)) {
335335
continue;
336336
}
337-
if (!(class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false)) || (new \ReflectionClass($class))->isUserDefined()) {
337+
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))) {
338338
$code[$options['class'].'.preload.php'] .= sprintf("\$classes[] = '%s';\n", $class);
339339
}
340340
}
@@ -1491,10 +1491,11 @@ private function addDefaultParametersMethod(): string
14911491
if ($key !== $resolvedKey = $this->container->resolveEnvPlaceholders($key)) {
14921492
throw new InvalidArgumentException(sprintf('Parameter name cannot use env parameters: "%s".', $resolvedKey));
14931493
}
1494-
$export = $this->exportParameters([$value]);
1494+
$hasEnum = false;
1495+
$export = $this->exportParameters([$value], '', 12, $hasEnum);
14951496
$export = explode('0 => ', substr(rtrim($export, " ]\n"), 2, -1), 2);
14961497

1497-
if (preg_match("/\\\$this->(?:getEnv\('(?:[-.\w]*+:)*+\w++'\)|targetDir\.'')/", $export[1])) {
1498+
if ($hasEnum || preg_match("/\\\$this->(?:getEnv\('(?:[-.\w]*+:)*+\w++'\)|targetDir\.'')/", $export[1])) {
14981499
$dynamicPhp[$key] = sprintf('%scase %s: $value = %s; break;', $export[0], $this->export($key), $export[1]);
14991500
} else {
15001501
$php[] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]);
@@ -1504,7 +1505,7 @@ private function addDefaultParametersMethod(): string
15041505

15051506
$code = <<<'EOF'
15061507
1507-
public function getParameter(string $name): array|string|int|float|bool|null
1508+
public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null
15081509
{
15091510
if (isset($this->buildParameters[$name])) {
15101511
return $this->buildParameters[$name];
@@ -1595,12 +1596,12 @@ protected function getDefaultParameters(): array
15951596
/**
15961597
* @throws InvalidArgumentException
15971598
*/
1598-
private function exportParameters(array $parameters, string $path = '', int $indent = 12): string
1599+
private function exportParameters(array $parameters, string $path = '', int $indent = 12, bool &$hasEnum = false): string
15991600
{
16001601
$php = [];
16011602
foreach ($parameters as $key => $value) {
16021603
if (\is_array($value)) {
1603-
$value = $this->exportParameters($value, $path.'/'.$key, $indent + 4);
1604+
$value = $this->exportParameters($value, $path.'/'.$key, $indent + 4, $hasEnum);
16041605
} elseif ($value instanceof ArgumentInterface) {
16051606
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));
16061607
} elseif ($value instanceof Variable) {
@@ -1611,6 +1612,9 @@ private function exportParameters(array $parameters, string $path = '', int $ind
16111612
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));
16121613
} elseif ($value instanceof Expression) {
16131614
throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain expressions. Expression "%s" found in "%s".', $value, $path.'/'.$key));
1615+
} elseif ($value instanceof \UnitEnum) {
1616+
$hasEnum = true;
1617+
$value = sprintf('\%s::%s', \get_class($value), $value->name);
16141618
} else {
16151619
$value = $this->export($value);
16161620
}

ParameterBag/ContainerBag.php

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

ParameterBag/FrozenParameterBag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function add(array $parameters)
5353
/**
5454
* {@inheritdoc}
5555
*/
56-
public function set(string $name, array|bool|string|int|float|null $value)
56+
public function set(string $name, array|bool|string|int|float|\UnitEnum|null $value)
5757
{
5858
throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
5959
}

ParameterBag/ParameterBag.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function all(): array
5959
/**
6060
* {@inheritdoc}
6161
*/
62-
public function get(string $name): array|bool|string|int|float|null
62+
public function get(string $name): array|bool|string|int|float|\UnitEnum|null
6363
{
6464
if (!\array_key_exists($name, $this->parameters)) {
6565
if (!$name) {
@@ -99,7 +99,7 @@ public function get(string $name): array|bool|string|int|float|null
9999
/**
100100
* {@inheritdoc}
101101
*/
102-
public function set(string $name, array|bool|string|int|float|null $value)
102+
public function set(string $name, array|bool|string|int|float|\UnitEnum|null $value)
103103
{
104104
$this->parameters[$name] = $value;
105105
}

ParameterBag/ParameterBagInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function all(): array;
4545
*
4646
* @throws ParameterNotFoundException if the parameter is not defined
4747
*/
48-
public function get(string $name): array|bool|string|int|float|null;
48+
public function get(string $name): array|bool|string|int|float|\UnitEnum|null;
4949

5050
/**
5151
* Removes a parameter.
@@ -57,7 +57,7 @@ public function remove(string $name);
5757
*
5858
* @throws LogicException if the parameter cannot be set
5959
*/
60-
public function set(string $name, array|bool|string|int|float|null $value);
60+
public function set(string $name, array|bool|string|int|float|\UnitEnum|null $value);
6161

6262
/**
6363
* Returns true if a parameter name is defined.

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;
@@ -1219,16 +1220,37 @@ public function testDumpHandlesEnumeration()
12191220
->setPublic(true)
12201221
->addArgument(FooUnitEnum::BAR);
12211222

1223+
$container->setParameter('unit_enum', FooUnitEnum::BAR);
1224+
$container->setParameter('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]);
12221225
$container->compile();
12231226

12241227
$dumper = new PhpDumper($container);
1225-
eval('?>'.$dumper->dump([
1228+
eval('?>'.$dumpedContainer = $dumper->dump([
12261229
'class' => 'Symfony_DI_PhpDumper_Test_Enumeration',
12271230
]));
12281231

1232+
/** @var Container $container */
12291233
$container = new \Symfony_DI_PhpDumper_Test_Enumeration();
12301234

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

12341256
public function testUninitializedSyntheticReference()

Tests/Dumper/XmlDumperTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ public function testDumpHandlesEnumeration()
255255
->setPublic(true)
256256
->addArgument(FooUnitEnum::BAR);
257257

258+
$container->setParameter('unit_enum', FooUnitEnum::BAR);
259+
$container->setParameter('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]);
260+
258261
$container->compile();
259262
$dumper = new XmlDumper($container);
260263

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

0 commit comments

Comments
 (0)