8000 [DependencyInjection] Fix TaggedLocator attribute without indexAttribute argument by lbae · Pull Request #42019 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Fix TaggedLocator attribute without indexAttribute argument #42019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[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
  • Loading branch information
lbae committed Jul 7, 2021
commit 0499fff5322ff1ceb69a4a812139ed8b73ddbcba
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a

if (TaggedLocator::class === $attribute->getName()) {
$attribute = $attribute->newInstance();
$arguments[$index] = new ServiceLocatorArgument(new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute));
$arguments[$index] = new ServiceLocatorArgument(new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute, null, true));
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumer;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerConsumer;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerFactory;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithoutIndex;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService1;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService2;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService3;
Expand Down Expand Up @@ -403,6 +404,35 @@ public function testTaggedLocatorConfiguredViaAttribute()
self::assertSame($container->get(FooTagClass::class), $locator->get('foo'));
}

/**
* @requires PHP 8
*/
public function testTaggedLocatorConfiguredViaAttributeWithoutIndex()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(FooTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(LocatorConsumerWithoutIndex::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

/** @var LocatorConsumerWithoutIndex $s */
$s = $container->get(LocatorConsumerWithoutIndex::class);

$locator = $s->getLocator();
self::assertSame($container->get(BarTagClass::class), $locator->get(BarTagClass::class));
self::assertSame($container->get(FooTagClass::class), $locator->get(FooTagClass::class));
}

/**
* @requires PHP 8
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;

final class LocatorConsumerWithoutIndex
{
public function __construct(
#[TaggedLocator('foo_bar')]
private ContainerInterface $locator,
) {
}

public function getLocator(): ContainerInterface
{
return $this->locator;
}
}
0