8000 [DI] default to service id - *not* FQCN - when building tagged locators by nicolas-grekas · Pull Request #31463 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] default to service id - *not* FQCN - when building tagged locators #31463

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
May 13, 2019
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
8000 Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,26 @@ class TaggedIteratorArgument extends IteratorArgument
private $tag;
private $indexAttribute;
private $defaultIndexMethod;
private $useFqcnAsFallback = false;
private $needsIndexes = false;

/**
* @param string $tag The name of the tag identifying the target services
* @param string|null $indexAttribute The name of the attribute that defines the key referencing each service in the tagged collection
* @param string|null $defaultIndexMethod The static method that should be called to get each service's key when their tag doesn't define the previous attribute
* @param bool $useFqcnAsFallback Whether the FQCN of the service should be used as index when neither the attribute nor the method are defined
* @param bool $needsIndexes Whether indexes are required and should be generated when computing the map
*/
public function __construct(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, bool $useFqcnAsFallback = false)
public function __construct(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, bool $needsIndexes = false)
{
parent::__construct([]);

if (null === $indexAttribute && $needsIndexes) {
$indexAttribute = preg_match('/[^.]++$/', $tag, $m) ? $m[0] : $tag;
}

$this->tag = $tag;
$this->indexAttribute = $indexAttribute;
$this->defaultIndexMethod = $defaultIndexMethod ?: ('getDefault'.str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $indexAttribute ?? ''))).'Name');
$this->useFqcnAsFallback = $useFqcnAsFallback;
$this->needsIndexes = $needsIndexes;
}

public function getTag()
Expand All @@ -54,8 +58,8 @@ public function getDefaultIndexMethod(): ?string
return $this->defaultIndexMethod;
}

public function useFqcnAsFallback(): bool
public function needsIndexes(): bool
{
return $this->useFqcnAsFallback;
return $this->needsIndexes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ trait PriorityTaggedServiceTrait
*/
private function findAndSortTaggedServices($tagNam 8000 e, ContainerBuilder $container)
{
$indexAttribute = $defaultIndexMethod = $useFqcnAsFallback = null;
$indexAttribute = $defaultIndexMethod = $needsIndexes = null;

if ($tagName instanceof TaggedIteratorArgument) {
$indexAttribute = $tagName->getIndexAttribute();
$defaultIndexMethod = $tagName->getDefaultIndexMethod();
$useFqcnAsFallback = $tagName->useFqcnAsFallback();
$needsIndexes = $tagName->needsIndexes();
$tagName = $tagName->getTag();
}

Expand All @@ -55,7 +55,7 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container
foreach ($container->findTaggedServiceIds($tagName, true) as $serviceId => $attributes) {
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;

if (null === $indexAttribute && !$useFqcnAsFallback) {
if (null === $indexAttribute && !$needsIndexes) {
$services[$priority][] = new Reference($serviceId);

continue;
Expand All @@ -77,8 +77,8 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container
$class = $r->name;

if (!$r->hasMethod($defaultIndexMethod)) {
if ($useFqcnAsFallback) {
$services[$priority][$class] = new TypedReference($serviceId, $class);
if ($needsIndexes) {
$services[$priority][$serviceId] = new TypedReference($serviceId, $class);

continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,22 +292,22 @@ public function testTaggedServiceWithIndexAttributeAndDefaultMethod()
public function testTaggedServiceLocatorWithIndexAttribute()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class)
$container->register('bar_tag', BarTagClass::class)
->setPublic(true)
->addTag('foo_bar', ['foo' => 'bar'])
;
$container->register(FooTagClass::class)
$container->register('foo_tag', FooTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(FooBarTaggedClass::class)
$container->register('foo_bar_tagged', FooBarTaggedClass::class)
->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('foo_bar', 'foo')))
->setPublic(true)
;

$container->compile();

$s = $container->get(FooBarTaggedClass::class);
$s = $container->get('foo_bar_tagged');

/** @var ServiceLocator $serviceLocator */
$serviceLocator = $s->getParam();
Expand All @@ -317,28 +317,28 @@ public function testTaggedServiceLocatorWithIndexAttribute()
'bar' => $serviceLocator->get('bar'),
'foo_tag_class' => $serviceLocator->get('foo_tag_class'),
];
$this->assertSame(['bar' => $container->get(BarTagClass::class), 'foo_tag_class' => $container->get(FooTagClass::class)], $same);
$this->assertSame(['bar' => $container->get('bar_tag'), 'foo_tag_class' => $container->get('foo_tag')], $same);
}

public function testTaggedServiceLocatorWithIndexAttributeAndDefaultMethod()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class)
$container->register('bar_tag', BarTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(FooTagClass::class)
$container->register('foo_tag', FooTagClass::class)
->setPublic(true)
->addTag('foo_bar', ['foo' => 'foo'])
;
$container->register(FooBarTaggedClass::class)
$container->register('foo_bar_tagged', FooBarTaggedClass::class)
->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('foo_bar', 'foo', 'getFooBar')))
->setPublic(true)
;

$container->compile();

$s = $container->get(FooBarTaggedClass::class);
$s = $container->get('foo_bar_tagged');

/** @var ServiceLocator $serviceLocator */
$serviceLocator = $s->getParam();
Expand All @@ -348,33 +348,59 @@ public function testTaggedServiceLocatorWithIndexAttributeAndDefaultMethod()
'bar_tab_class_with_defaultmethod' => $serviceLocator->get('bar_tab_class_with_defaultmethod'),
'foo' => $serviceLocator->get('foo'),
];
$this->assertSame(['bar_tab_class_with_defaultmethod' => $container->get(BarTagClass::class), 'foo' => $container->get(FooTagClass::class)], $same);
$this->assertSame(['bar_tab_class_with_defaultmethod' => $container->get('bar_tag'), 'foo' => $container->get('foo_tag')], $same);
}

public function testTaggedServiceLocatorWithFqcnFallback()
public function testTaggedServiceLocatorWithFallback()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class)
$container->register('bar_tag', BarTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(FooBarTaggedClass::class)
$container->register('foo_bar_tagged', FooBarTaggedClass::class)
->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('foo_bar', null, null, true)))
->setPublic(true)
;

$container->compile();

$s = $container->get(FooBarTaggedClass::class);
$s = $container->get('foo_bar_tagged');

/** @var ServiceLocator $serviceLocator */
$serviceLocator = $s->getParam();
$this->assertTrue($s->getParam() instanceof ServiceLocator, sprintf('Wrong instance, should be an instance of ServiceLocator, %s given', \is_object($serviceLocator) ? \get_class($serviceLocator) : \gettype($serviceLocator)));

$same = [
BarTagClass::class => $serviceLocator->get(BarTagClass::class),
$expected = [
'bar_tag' => $container->get('bar_tag'),
];
$this->assertSame($expected, ['bar_tag' => $serviceLocator->get('bar_tag')]);
}

public function testTaggedServiceLocatorWithDefaultIndex()
{
$container = new ContainerBuilder();
$container->register('bar_tag', BarTagClass::class)
->setPublic(true)
->addTag('app.foo_bar', ['foo_bar' => 'baz'])
;
$container->register('foo_bar_tagged', FooBarTaggedClass::class)
->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('app.foo_bar', null, null, true)))
->setPublic(true)
;

$container->compile();

$s = $container->get('foo_bar_tagged');

/** @var ServiceLocator $serviceLocator */
$serviceLocator = $s->getParam();
$this->assertTrue($s->getParam() instanceof ServiceLocator, sprintf('Wrong instance, should be an instance of ServiceLocator, %s given', \is_object($serviceLocator) ? \get_class($serviceLocator) : \gettype($serviceLocator)));

$expected = [
'baz' => $container->get('bar_tag'),
];
$this->assertSame([BarTagClass::class => $container->get(BarTagClass::class)], $same);
$this->assertSame($expected, ['baz' => $serviceLocator->get('baz')]);
}
}

Expand Down
0