Skip to content

Commit f767f62

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 d296d1e + 46b8354 commit f767f62

23 files changed

+188
-16
lines changed

Console/Descriptor/Descriptor.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ protected function formatValue($value): string
173173
*/
174174
protected function formatParameter($value): string
175175
{
176+
if ($value instanceof \UnitEnum) {
177+
return var_export($value, true);
178+
}
179+
180+
// Recursively search for enum values, so we can replace it
181+
// before json_encode (which will not display anything for \UnitEnum otherwise)
182+
if (\is_array($value)) {
183+
array_walk_recursive($value, static function (&$value) {
184+
if ($value instanceof \UnitEnum) {
185+
$value = var_export($value, true);
186+
}
187+
});
188+
}
189+
176190
if (\is_bool($value) || \is_array($value) || (null === $value)) {
177191
$jsonString = json_encode($value);
178192

Console/Descriptor/JsonDescriptor.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ private function writeData(array $data, array $options)
184184
{
185185
$flags = $options['json_encoding'] ?? 0;
186186

187+
// Recursively search for enum values, so we can replace it
188+
// before json_encode (which will not display anything for \UnitEnum otherwise)
189+
array_walk_recursive($data, static function (&$value) {
190+
if ($value instanceof \UnitEnum) {
191+
$value = var_export($value, true);
192+
}
193+
});
194+
187195
$this->write(json_encode($data, $flags | \JSON_PRETTY_PRINT)."\n");
188196
}
189197

Console/Descriptor/TextDescriptor.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ protected function describeContainerDefinition(Definition $definition, array $op
348348
$argumentsInformation[] = sprintf('Service locator (%d element(s))', \count($argument->getValues()));
349349
} elseif ($argument instanceof Definition) {
350350
$argumentsInformation[] = 'Inlined Service';
351+
} elseif ($argument instanceof \UnitEnum) {
352+
$argumentsInformation[] = var_export($argument, true);
351353
} elseif ($argument instanceof AbstractArgument) {
352354
$argumentsInformation[] = sprintf('Abstract argument (%s)', $argument->getText());
353355
} else {

Console/Descriptor/XmlDescriptor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,9 @@ private function getArgumentNodes(array $arguments, \DOMDocument $dom): array
419419
foreach ($this->getArgumentNodes($argument, $dom) as $childArgumentXML) {
420420
$argumentXML->appendChild($childArgumentXML);
421421
}
422+
} elseif ($argument instanceof \UnitEnum) {
423+
$argumentXML->setAttribute('type', 'constant');
424+
$argumentXML->appendChild(new \DOMText(var_export($argument, true)));
422425
} else {
423426
$argumentXML->appendChild(new \DOMText($argument));
424427
}

Controller/AbstractController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function setContainer(ContainerInterface $container): ?ContainerInterface
7575
/**
7676
* Gets a container parameter by its name.
7777
*
78-
* @return array|bool|float|int|string|null
78+
* @return array|bool|float|int|string|\UnitEnum|null
7979
*/
8080
protected function getParameter(string $name)
8181
{

DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2256,7 +2256,8 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
22562256
if (method_exists(TagAwareAdapter::class, 'setLogger')) {
22572257
$container
22582258
->getDefinition($name)
2259-
->addMethodCall('setLogger', [new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]);
2259+
->addMethodCall('setLogger', [new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)])
2260+
->addTag('monolog.logger', ['channel' => 'cache']);
22602261
}
22612262

22622263
$pool['name'] = $tagAwareId = $name;

Test/TestContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function getParameterBag(): ParameterBagInterface
6464
/**
6565
* {@inheritdoc}
6666
*
67-
* @return array|bool|float|int|string|null
67+
* @return array|bool|float|int|string|\UnitEnum|null
6868
*/
6969
public function getParameter(string $name)
7070
{

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

@@ -261,7 +266,7 @@ private function assertDescription($expectedDescription, $describedObject, array
261266
}
262267
}
263268

264-
private function getDescriptionTestData(array $objects)
269+
private function getDescriptionTestData(iterable $objects)
265270
{
266271
$data = [];
267272
foreach ($objects as $name => $object) {

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\AbstractArgument;
1618
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
@@ -62,14 +64,26 @@ public static function getRoutes()
6264

6365
public static function getContainerParameters()
6466
{
65-
return [
66-
'parameters_1' => new ParameterBag([
67-
'integer' => 12,
68-
'string' => 'Hello world!',
69-
'boolean' => true,
70-
'array' => [12, 'Hello world!', true],
71-
]),
72-
];
67+
yield 'parameters_1' => new ParameterBag([
68+
'integer' => 12,
69+
'string' => 'Hello world!',
70+
'boolean' => true,
71+
'array' => [12, 'Hello world!', true],
72+
]);
73+
74+
if (\PHP_VERSION_ID < 80100) {
75+
return;
76+
}
77+
78+
yield 'parameters_enums' => new ParameterBag([
79+
'unit_enum' => FooUnitEnum::BAR,
80+
'backed_enum' => Suit::Hearts,
81+
'array_of_enums' => Suit::cases(),
82+
'map' => [
83+
'mixed' => [Suit::Hearts, FooUnitEnum::BAR],
84+
'single' => FooUnitEnum::BAR,
85+
],
86+
]);
7387
}
7488

7589
public static function getContainerParameter()

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,7 @@ public function testCachePoolServices()
16451645
$this->assertEquals([
16461646
['setLogger', [new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]],
16471647
], $tagAwareDefinition->getMethodCalls());
1648+
$this->assertSame([['channel' => 'cache']], $tagAwareDefinition->getTag('monolog.logger'));
16481649
}
16491650
}
16501651

0 commit comments

Comments
 (0)