Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 87a8823

Browse files
[DI] default to service id - *not* FQCN - when building tagged locators
1 parent 04f117f commit 87a8823

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

src/Symfony/Component/DependencyInjection/Argument/TaggedIteratorArgument.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ class TaggedIteratorArgument extends IteratorArgument
2121
private $tag;
2222
private $indexAttribute;
2323
private $defaultIndexMethod;
24-
private $useFqcnAsFallback = false;
24+
private $useIdAsFallback = false;
2525

2626
/**
2727
* @param string $tag The name of the tag identifying the target services
2828
* @param string|null $indexAttribute The name of the attribute that defines the key referencing each service in the tagged collection
2929
* @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
30-
* @param bool $useFqcnAsFallback Whether the FQCN of the service should be used as index when neither the attribute nor the method are defined
30+
* @param bool $useIdAsFallback Whether the id of the service should be used as index when neither the attribute nor the method are defined
3131
*/
32-
public function __construct(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, bool $useFqcnAsFallback = false)
32+
public function __construct(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, bool $useIdAsFallback = false)
3333
{
3434
parent::__construct([]);
3535

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

4242
public function getTag()
@@ -54,8 +54,8 @@ public function getDefaultIndexMethod(): ?string
5454
return $this->defaultIndexMethod;
5555
}
5656

57-
public function useFqcnAsFallback(): bool
57+
public function useIdAsFallback(): bool
5858
{
59-
return $this->useFqcnAsFallback;
59+
return $this->useIdAsFallback;
6060
}
6161
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ trait PriorityTaggedServiceTrait
4141
*/
4242
private function findAndSortTaggedServices($tagName, ContainerBuilder $container)
4343
{
44-
$indexAttribute = $defaultIndexMethod = $useFqcnAsFallback = null;
44+
$indexAttribute = $defaultIndexMethod = $useIdAsFallback = null;
4545

4646
if ($tagName instanceof TaggedIteratorArgument) {
4747
$indexAttribute = $tagName->getIndexAttribute();
4848
$defaultIndexMethod = $tagName->getDefaultIndexMethod();
49-
$useFqcnAsFallback = $tagName->useFqcnAsFallback();
49+
$useIdAsFallback = $tagName->useIdAsFallback();
5050
$tagName = $tagName->getTag();
5151
}
5252

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

58-
if (null === $indexAttribute && !$useFqcnAsFallback) {
58+
if (null === $indexAttribute && !$useIdAsFallback) {
5959
$services[$priority][] = new Reference($serviceId);
6060

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

7979
if (!$r->hasMethod($defaultIndexMethod)) {
80-
if ($useFqcnAsFallback) {
81-
$services[$priority][$class] = new TypedReference($serviceId, $class);
80+
if ($useIdAsFallback) {
81+
$services[$priority][$serviceId] = new TypedReference($serviceId, $class);
8282

8383
continue;
8484
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -292,22 +292,22 @@ public function testTaggedServiceWithIndexAttributeAndDefaultMethod()
292292
public function testTaggedServiceLocatorWithIndexAttribute()
293293
{
294294
$container = new ContainerBuilder();
295-
$container->register(BarTagClass::class)
295+
$container->register('bar_tag', BarTagClass::class)
296296
->setPublic(true)
297297
->addTag('foo_bar', ['foo' => 'bar'])
298298
;
299-
$container->register(FooTagClass::class)
299+
$container->register('foo_tag', FooTagClass::class)
300300
->setPublic(true)
301301
->addTag('foo_bar')
302302
;
303-
$container->register(FooBarTaggedClass::class)
303+
$container->register('foo_bar_tagged', FooBarTaggedClass::class)
304304
->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('foo_bar', 'foo')))
305305
->setPublic(true)
306306
;
307307

308308
$container->compile();
309309

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

312312
/** @var ServiceLocator $serviceLocator */
313313
$serviceLocator = $s->getParam();
@@ -317,28 +317,28 @@ public function testTaggedServiceLocatorWithIndexAttribute()
317317
'bar' => $serviceLocator->get('bar'),
318318
'foo_tag_class' => $serviceLocator->get('foo_tag_class'),
319319
];
320-
$this->assertSame(['bar' => $container->get(BarTagClass::class), 'foo_tag_class' => $container->get(FooTagClass::class)], $same);
320+
$this->assertSame(['bar' => $container->get('bar_tag'), 'foo_tag_class' => $container->get('foo_tag')], $same);
321321
}
322322

323323
public function testTaggedServiceLocatorWithIndexAttributeAndDefaultMethod()
324324
{
325325
$container = new ContainerBuilder();
326-
$container->register(BarTagClass::class)
326+
$container->register('bar_tag', BarTagClass::class)
327327
->setPublic(true)
328328
->addTag('foo_bar')
329329
;
330-
$container->register(FooTagClass::class)
330+
$container->register('foo_tag', FooTagClass::class)
331331
->setPublic(true)
332332
->addTag('foo_bar', ['foo' => 'foo'])
333333
;
334-
$container->register(FooBarTaggedClass::class)
334+
$container->register('foo_bar_tagged', FooBarTaggedClass::class)
335335
->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('foo_bar', 'foo', 'getFooBar')))
336336
->setPublic(true)
337337
;
338338

339339
$container->compile();
340340

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

343343
/** @var ServiceLocator $serviceLocator */
344344
$serviceLocator = $s->getParam();
@@ -348,33 +348,33 @@ public function testTaggedServiceLocatorWithIndexAttributeAndDefaultMethod()
348348
'bar_tab_class_with_defaultmethod' => $serviceLocator->get('bar_tab_class_with_defaultmethod'),
349349
'foo' => $serviceLocator->get('foo'),
350350
];
351-
$this->assertSame(['bar_tab_class_with_defaultmethod' => $container->get(BarTagClass::class), 'foo' => $container->get(FooTagClass::class)], $same);
351+
$this->assertSame(['bar_tab_class_with_defaultmethod' => $container->get('bar_tag'), 'foo' => $container->get('foo_tag')], $same);
352352
}
353353

354-
public function testTaggedServiceLocatorWithFqcnFallback()
354+
public function testTaggedServiceLocatorWithIdFallback()
355355
{
356356
$container = new ContainerBuilder();
357-
$container->register(BarTagClass::class)
357+
$container->register('bar_tag', BarTagClass::class)
358358
->setPublic(true)
359359
->addTag('foo_bar')
360360
;
361-
$container->register(FooBarTaggedClass::class)
361+
$container->register('foo_bar_tagged', FooBarTaggedClass::class)
362362
->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('foo_bar', null, null, true)))
363363
->setPublic(true)
364364
;
365365

366366
$container->compile();
367367

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

370370
/** @var ServiceLocator $serviceLocator */
371371
$serviceLocator = $s->getParam();
372372
$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)));
373373

374-
$same = [
375-
BarTagClass::class => $serviceLocator->get(BarTagClass::class),
374+
$expected = [
375+
'bar_tag' => $container->get('bar_tag'),
376376
];
377-
$this->assertSame([BarTagClass::class => $container->get(BarTagClass::class)], $same);
377+
$this->assertSame($expected, ['bar_tag' => $serviceLocator->get('bar_tag')]);
378378
}
379379
}
380380

0 commit comments

Comments
 (0)