Skip to content

Commit e421c4f

Browse files
Merge branch '5.2' into 5.3
* 5.2: [DI] fix fixture [ErrorHandler] fix handling buffered SilencedErrorContext [HttpClient] fix Psr18Client when allow_url_fopen=0 [DependencyInjection] Add support of PHP enumerations [Cache] handle prefixed redis connections when clearing pools [Cache] fix eventual consistency when using RedisTagAwareAdapter with a cluster [Uid] Fix fromString() with low base58 values [Validator][Translation] Add ExpressionLanguageSyntax en and fr [HttpKernel] [HttpCache] Keep s-maxage=0 from ESI sub-responses Avoid broken action URL in text notification mail [Cache] Disable locking on Windows by default [DependencyInjection] Fix binding "iterable $foo" when using the PHP-DSL [Config] fix tracking default values that reference the parent class [DependencyInjection] fix accepted types on FactoryTrait::factory() [VarDumper] Fix tests for PHP 8.1 [Mailer] fix encoding of addresses using SmtpTransport [MonologBridge] Fix the server:log help --filter sample
2 parents 2aef83e + 1451513 commit e421c4f

20 files changed

+201
-6
lines changed

Compiler/ResolveBindingsPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ protected function processValue($value, bool $isRoot = false)
135135
}
136136

137137
if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition && !$bindingValue instanceof TaggedIteratorArgument && !$bindingValue instanceof ServiceLocatorArgument) {
138-
throw new InvalidArgumentException(sprintf('Invalid value for binding key "%s" for service "%s": expected null, "%s", "%s", "%s" or ServiceLocatorArgument, "%s" given.', $key, $this->currentId, Reference::class, Definition::class, TaggedIteratorArgument::class, get_debug_type($bindingValue)));
138+
throw new InvalidArgumentException(sprintf('Invalid value for binding key "%s" for service "%s": expected "%s", "%s", "%s", "%s" or null, "%s" given.', $key, $this->currentId, Reference::class, Definition::class, TaggedIteratorArgument::class, ServiceLocatorArgument::class, get_debug_type($bindingValue)));
139139
}
140140
}
141141

Dumper/PhpDumper.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,8 @@ private function dumpValue($value, bool $interpolate = true): string
18711871

18721872
return $code;
18731873
}
1874+
} elseif ($value instanceof \UnitEnum) {
1875+
return sprintf('\%s::%s', \get_class($value), $value->name);
18741876
} elseif ($value instanceof AbstractArgument) {
18751877
throw new RuntimeException($value->getTextWithContext());
18761878
} elseif (\is_object($value) || \is_resource($value)) {

Dumper/XmlDumper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ private function convertParameters(array $parameters, string $type, \DOMElement
324324
$element->setAttribute('type', 'binary');
325325
$text = $this->document->createTextNode(self::phpToXml(base64_encode($value)));
326326
$element->appendChild($text);
327+
} elseif ($value instanceof \UnitEnum) {
328+
$element->setAttribute('type', 'constant');
329+
$element->appendChild($this->document->createTextNode(self::phpToXml($value)));
327330
} elseif ($value instanceof AbstractArgument) {
328331
$element->setAttribute('type', 'abstract');
329332
$text = $this->document->createTextNode(self::phpToXml($value->getText()));
@@ -381,6 +384,8 @@ public static function phpToXml($value): string
381384
return 'false';
382385
case $value instanceof Parameter:
383386
return '%'.$value.'%';
387+
case $value instanceof \UnitEnum:
388+
return sprintf('%s::%s', \get_class($value), $value->name);
384389
case \is_object($value) || \is_resource($value):
385390
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
386391
default:

Dumper/YamlDumper.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ private function dumpValue($value)
306306
return $this->getExpressionCall((string) $value);
307307
} elseif ($value instanceof Definition) {
308308
return new TaggedValue('service', (new Parser())->parse("_:\n".$this->addService('_', $value), Yaml::PARSE_CUSTOM_TAGS)['_']['_']);
309+
} elseif ($value instanceof \UnitEnum) {
310+
return new TaggedValue('php/const', sprintf('%s::%s', \get_class($value), $value->name));
309311
} elseif ($value instanceof AbstractArgument) {
310312
return new TaggedValue('abstract', $value->getText());
311313
} elseif (\is_object($value) || \is_resource($value)) {

Loader/Configurator/Traits/BindTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ trait BindTrait
3434
final public function bind(string $nameOrFqcn, $valueOrRef): self
3535
{
3636
$valueOrRef = static::processValue($valueOrRef, true);
37-
if (!preg_match('/^(?:(?:array|bool|float|int|string)[ \t]*+)?\$/', $nameOrFqcn) && !$valueOrRef instanceof Reference) {
37+
if (!preg_match('/^(?:(?:array|bool|float|int|string|iterable)[ \t]*+)?\$/', $nameOrFqcn) && !$valueOrRef instanceof Reference) {
3838
throw new InvalidArgumentException(sprintf('Invalid binding for service "%s": named arguments must start with a "$", and FQCN must map to references. Neither applies to binding "%s".', $this->id, $nameOrFqcn));
3939
}
4040
$bindings = $this->definition->getBindings();

Loader/Configurator/Traits/FactoryTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
1313

1414
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
15+
use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator;
1516

1617
trait FactoryTrait
1718
{
1819
/**
1920
* Sets a factory.
2021
*
21-
* @param string|array $factory A PHP callable reference
22+
* @param string|array|ReferenceConfigurator $factory A PHP callable reference
2223
*
2324
* @return $this
2425
*/

Tests/Dumper/PhpDumperTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
4343
use Symfony\Component\DependencyInjection\Tests\Compiler\Wither;
4444
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
45+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
46+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
4547
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
4648
use Symfony\Component\DependencyInjection\Tests\Fixtures\ScalarFactory;
4749
use Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator;
@@ -1228,6 +1230,29 @@ public function testDumpHandlesObjectClassNames()
12281230
$this->assertInstanceOf(\stdClass::class, $container->get('bar'));
12291231
}
12301232

1233+
/**
1234+
* @requires PHP 8.1
1235+
*/
1236+
public function testDumpHandlesEnumeration()
1237+
{
1238+
$container = new ContainerBuilder();
1239+
$container
1240+
->register('foo', FooClassWithEnumAttribute::class)
1241+
->setPublic(true)
1242+
->addArgument(FooUnitEnum::BAR);
1243+
1244+
$container->compile();
1245+
1246+
$dumper = new PhpDumper($container);
1247+
eval('?>'.$dumper->dump([
1248+
'class' => 'Symfony_DI_PhpDumper_Test_Enumeration',
1249+
]));
1250+
1251+
$container = new \Symfony_DI_PhpDumper_Test_Enumeration();
1252+
1253+
$this->assertSame(FooUnitEnum::BAR, $container->get('foo')->getBar());
1254+
}
1255+
12311256
public function testUninitializedSyntheticReference()
12321257
{
12331258
$container = new ContainerBuilder();

Tests/Dumper/XmlDumperTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
2323
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2424
use Symfony\Component\DependencyInjection\Reference;
25+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
26+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
2527
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
2628

2729
class XmlDumperTest extends TestCase
@@ -272,6 +274,23 @@ public function testDumpAbstractServices()
272274
$this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services_abstract.xml'), $dumper->dump());
273275
}
274276

277+
/**
278+
* @requires PHP 8.1
279+
*/
280+
public function testDumpHandlesEnumeration()
281+
{
282+
$container = new ContainerBuilder();
283+
$container
284+
->register(FooClassWithEnumAttribute::class, FooClassWithEnumAttribute::class)
285+
->setPublic(true)
286+
->addArgument(FooUnitEnum::BAR);
287+
288+
$container->compile();
289+
$dumper = new XmlDumper($container);
290+
291+
$this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services_with_enumeration.xml'), $dumper->dump());
292+
}
293+
275294
public function testDumpServiceWithAbstractArgument()
276295
{
277296
$container = new ContainerBuilder();

Tests/Dumper/YamlDumperTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
2424
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
2525
use Symfony\Component\DependencyInjection\Reference;
26+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
27+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
2628
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
2729
use Symfony\Component\Yaml\Parser;
2830
use Symfony\Component\Yaml\Yaml;
@@ -131,6 +133,23 @@ public function testServiceClosure()
131133
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_with_service_closure.yml', $dumper->dump());
132134
}
133135

136+
/**
137+
* @requires PHP 8.1
138+
*/
139+
public function testDumpHandlesEnumeration()
140+
{
141+
$container = new ContainerBuilder();
142+
$container
143+
->register(FooClassWithEnumAttribute::class, FooClassWithEnumAttribute::class)
144+
->setPublic(true)
145+
->addArgument(FooUnitEnum::BAR);
146+
147+
$container->compile();
148+
$dumper = new YamlDumper($container);
149+
150+
$this->assertEquals(file_get_contents(self::$fixturesPath.'/yaml/services_with_enumeration.yml'), $dumper->dump());
151+
}
152+
134153
public function testDumpServiceWithAbstractArgument()
135154
{
136155
$container = new ContainerBuilder();
@@ -142,7 +161,6 @@ public function testDumpServiceWithAbstractArgument()
142161
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_with_abstract_argument.yml', $dumper->dump());
143162
}
144163

145-
146164
private function assertEqualYamlStructure(string $expected, string $yaml, string $message = '')
147165
{
148166
$parser = new Parser();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
class FooClassWithEnumAttribute
6+
{
7+
private FooUnitEnum $bar;
8+
9+
public function __construct(FooUnitEnum $bar)
10+
{
11+
$this->bar = $bar;
12+
}
13+
14+
public function getBar(): FooUnitEnum
15+
{
16+
return $this->bar;
17+
}
18+
}

0 commit comments

Comments
 (0)