8000 [DependencyInjection] Fix TaggedLocator attribute without index argument · symfony/symfony@0499fff · GitHub
[go: up one dir, main page]

Skip to content

Commit 0499fff

Browse files
committed
[DependencyInjection] Fix TaggedLocator attribute without index argument
With this fix, services in service locators are indexed by their fully qualified name instead of a numeric index if `indexAttribute` argument is not provided
1 parent cf4f705 commit 0499fff

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
218218

219219
if (TaggedLocator::class === $attribute->getName()) {
220220
$attribute = $attribute->newInstance();
221-
$arguments[$index] = new ServiceLocatorArgument(new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute));
221+
$arguments[$index] = new ServiceLocatorArgument(new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute, null, true));
222222
break;
223223
}
224224
}
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumer;
3333
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerConsumer;
3434
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerFactory;
35+
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithoutIndex;
3536
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService1;
3637
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService2;
3738
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService3;
@@ -403,6 +404,35 @@ public function testTaggedLocatorConfiguredViaAttribute()
403404
self::assertSame($container->get(FooTagClass::class), $locator->get('foo'));
404405
}
405406

407+
/**
408+
* @requires PHP 8
409+
*/
410+
public function testTaggedLocatorConfiguredViaAttributeWithoutIndex()
411+
{
412+
$container = new ContainerBuilder();
413+
$container->register(BarTagClass::class)
414+
->setPublic(true)
415+
->addTag('foo_bar')
416+
;
417+
$container->register(FooTagClass::class)
418+
->setPublic(true)
419+
->addTag('foo_bar')
420+
;
421+
$container->register(LocatorConsumerWithoutIndex::class)
422+
->setAutowired(true)
423+
->setPublic(true)
424+
;
425+
426+
$container->compile();
427+
428+
/** @var LocatorConsumerWithoutIndex $s */
429+
$s = $container->get(LocatorConsumerWithoutIndex::class);
430+
431+
$locator = $s->getLocator();
432+
self::assertSame($container->get(BarTagClass::class), $locator->get(BarTagClass::class));
433+
self::assertSame($container->get(FooTagClass::class), $locator->get(FooTagClass::class));
434+
}
435+
406436
/**
407437
* @requires PHP 8
408438
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
13+
14+
use Psr\Container\ContainerInterface;
15+
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
16+
17+
final class LocatorConsumerWithoutIndex
18+
{
19+
public function __construct(
20+
#[TaggedLocator('foo_bar')]
21+
private ContainerInterface $locator,
22+
) {
23+
}
24+
25+
public function getLocator(): ContainerInterface
26+
{
27+
return $this->locator;
28+
}
29+
}