Skip to content

Commit f666764

Browse files
Merge branch '6.4' into 7.0
* 6.4: [Serializer] Remove TranslatableNormalizer service when the Translator is disabled Fix support to denormalize plain object types [Routing] Restore aliases removal in RouteCollection::remove() [Workflow] Add `getEnabledTransition()` to TraceableWorkflow [DependencyInjection] Fix parsing named autowiring aliases that contain underscores [Console] Add Les-Tilleuls.coop as sponsor of version 6.4/7.0 remove duplicated service definition
2 parents 71c053f + f88ff64 commit f666764

File tree

7 files changed

+48
-17
lines changed

7 files changed

+48
-17
lines changed

Attribute/Target.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ public function getParsedName(): string
3636
return lcfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $this->name))));
3737
}
3838

39-
public static function parseName(\ReflectionParameter $parameter, self &$attribute = null): string
39+
public static function parseName(\ReflectionParameter $parameter, self &$attribute = null, string &$parsedName = null): string
4040
{
4141
$attribute = null;
4242
if (!$target = $parameter->getAttributes(self::class)[0] ?? null) {
43+
$parsedName = (new self($parameter->name))->getParsedName();
44+
4345
return $parameter->name;
4446
}
4547

@@ -57,6 +59,6 @@ public static function parseName(\ReflectionParameter $parameter, self &$attribu
5759
throw new InvalidArgumentException(sprintf('Invalid #[Target] name "%s" on parameter "$%s" of "%s()": the first character must be a letter.', $name, $parameter->name, $function));
5860
}
5961

60-
return $parsedName;
62+
return preg_match('/^[a-zA-Z0-9_\x7f-\xff]++$/', $name) ? $name : $parsedName;
6163
}
6264
}

Compiler/AutowirePass.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,20 +444,30 @@ private function getAutowiredReference(TypedReference $reference, bool $filterTy
444444
$name = $target = (array_filter($reference->getAttributes(), static fn ($a) => $a instanceof Target)[0] ?? null)?->name;
445445

446446
if (null !== $name ??= $reference->getName()) {
447+
if ($this->container->has($alias = $type.' $'.$name) && !$this->container->findDefinition($alias)->isAbstract()) {
448+
return new TypedReference($alias, $type, $reference->getInvalidBehavior());
449+
}
450+
451+
if (null !== ($alias = $this->getCombinedAlias($type, $name)) && !$this->container->findDefinition($alias)->isAbstract()) {
452+
return new TypedReference($alias, $type, $reference->getInvalidBehavior());
453+
}
454+
447455
$parsedName = (new Target($name))->getParsedName();
448456

449457
if ($this->container->has($alias = $type.' $'.$parsedName) && !$this->container->findDefinition($alias)->isAbstract()) {
450458
return new TypedReference($alias, $type, $reference->getInvalidBehavior());
451459
}
452460

453-
if (null !== ($alias = $this->getCombinedAlias($type, $parsedName) ?? null) && !$this->container->findDefinition($alias)->isAbstract()) {
461+
if (null !== ($alias = $this->getCombinedAlias($type, $parsedName)) && !$this->container->findDefinition($alias)->isAbstract()) {
454462
return new TypedReference($alias, $type, $reference->getInvalidBehavior());
455463
}
456464

457-
if ($this->container->has($name) && !$this->container->findDefinition($name)->isAbstract()) {
465+
if (($this->container->has($n = $name) && !$this->container->findDefinition($n)->isAbstract())
466+
|| ($this->container->has($n = $parsedName) && !$this->container->findDefinition($n)->isAbstract())
467+
) {
458468
foreach ($this->container->getAliases() as $id => $alias) {
459-
if ($name === (string) $alias && str_starts_with($id, $type.' $')) {
460-
return new TypedReference($name, $type, $reference->getInvalidBehavior());
469+
if ($n === (string) $alias && str_starts_with($id, $type.' $')) {
470+
return new TypedReference($n, $type, $reference->getInvalidBehavior());
461471
}
462472
}
463473
}
@@ -471,7 +481,7 @@ private function getAutowiredReference(TypedReference $reference, bool $filterTy
471481
return new TypedReference($type, $type, $reference->getInvalidBehavior());
472482
}
473483

474-
if (null !== ($alias = $this->getCombinedAlias($type) ?? null) && !$this->container->findDefinition($alias)->isAbstract()) {
484+
if (null !== ($alias = $this->getCombinedAlias($type)) && !$this->container->findDefinition($alias)->isAbstract()) {
475485
return new TypedReference($alias, $type, $reference->getInvalidBehavior());
476486
}
477487

Compiler/ResolveBindingsPass.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,19 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
187187

188188
$typeHint = ltrim(ProxyHelper::exportType($parameter) ?? '', '?');
189189

190-
$name = Target::parseName($parameter);
190+
$name = Target::parseName($parameter, parsedName: $parsedName);
191191

192-
if ($typeHint && \array_key_exists($k = preg_replace('/(^|[(|&])\\\\/', '\1', $typeHint).' $'.$name, $bindings)) {
192+
if ($typeHint && (
193+
\array_key_exists($k = preg_replace('/(^|[(|&])\\\\/', '\1', $typeHint).' $'.$name, $bindings)
194+
|| \array_key_exists($k = preg_replace('/(^|[(|&])\\\\/', '\1', $typeHint).' $'.$parsedName, $bindings)
195+
)) {
193196
$arguments[$key] = $this->getBindingValue($bindings[$k]);
194197

195198
continue;
196199
}
197200

198-
if (\array_key_exists('$'.$name, $bindings)) {
199-
$arguments[$key] = $this->getBindingValue($bindings['$'.$name]);
201+
if (\array_key_exists($k = '$'.$name, $bindings) || \array_key_exists($k = '$'.$parsedName, $bindings)) {
202+
$arguments[$key] = $this->getBindingValue($bindings[$k]);
200203

201204
continue;
202205
}
@@ -207,7 +210,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
207210
continue;
208211
}
209212

210-
if (isset($bindingNames[$name]) || isset($bindingNames[$parameter->name])) {
213+
if (isset($bindingNames[$name]) || isset($bindingNames[$parsedName]) || isset($bindingNames[$parameter->name])) {
211214
$bindingKey = array_search($binding, $bindings, true);
212215
$argumentType = substr($bindingKey, 0, strpos($bindingKey, ' '));
213216
$this->errorMessages[] = sprintf('Did you forget to add the type "%s" to argument "$%s" of method "%s::%s()"?', $argumentType, $parameter->name, $reflectionMethod->class, $reflectionMethod->name);

Tests/Compiler/AutowirePassTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,18 @@ public function testAutowireWithNamedArgs()
11911191
$this->assertEquals([new TypedReference(A::class, A::class), 'abc'], $container->getDefinition('foo')->getArguments());
11921192
}
11931193

1194+
public function testAutowireUnderscoreNamedArgument()
1195+
{
1196+
$container = new ContainerBuilder();
1197+
1198+
$container->autowire(\DateTimeImmutable::class.' $now_datetime', \DateTimeImmutable::class);
1199+
$container->autowire('foo', UnderscoreNamedArgument::class)->setPublic(true);
1200+
1201+
(new AutowirePass())->process($container);
1202+
1203+
$this->assertInstanceOf(\DateTimeImmutable::class, $container->get('foo')->now_datetime);
1204+
}
1205+
11941206
public function testAutowireDefaultValueParametersLike()
11951207
{
11961208
$container = new ContainerBuilder();

Tests/Compiler/RegisterServiceSubscribersPassTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ public static function getSubscribedServices(): array
403403

404404
$expected = [
405405
'some.service' => new ServiceClosureArgument(new TypedReference('stdClass $someService', 'stdClass')),
406-
'some_service' => new ServiceClosureArgument(new TypedReference('stdClass $someService', 'stdClass')),
406+
'some_service' => new ServiceClosureArgument(new TypedReference('stdClass $some_service', 'stdClass')),
407407
'another_service' => new ServiceClosureArgument(new TypedReference('stdClass $anotherService', 'stdClass')),
408408
];
409409
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));

Tests/Fixtures/includes/autowiring_classes.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ public function __construct(A $a, Lille $lille, $foo = 'some_val')
213213
}
214214
}
215215

216+
class UnderscoreNamedArgument
217+
{
218+
public function __construct(
219+
public \DateTimeImmutable $now_datetime,
220+
) {
221+
}
222+
}
223+
216224
/*
217225
* Classes used for testing createResourceForClass
218226
*/

Tests/Fixtures/xml/services14.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
55
<services>
6-
<service id="monolog.logger" parent="monolog.logger_prototype">
7-
<argument index="0">app</argument>
8-
</service>
9-
106
<service id="logger" alias="monolog.logger" />
117

128
<service id="monolog.logger" parent="monolog.logger_prototype">

0 commit comments

Comments
 (0)