|
16 | 16 | use Symfony\Component\DependencyInjection\Alias;
|
17 | 17 | use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
|
18 | 18 | use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
|
| 19 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
19 | 20 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
20 | 21 | use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
21 | 22 | use Symfony\Component\DependencyInjection\Reference;
|
|
24 | 25 | use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedClass;
|
25 | 26 | use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedForDefaultPriorityClass;
|
26 | 27 | use Symfony\Component\DependencyInjection\Tests\Fixtures\FooTagClass;
|
| 28 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService1; |
| 29 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService2; |
| 30 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService3; |
27 | 31 | use Symfony\Contracts\Service\ServiceProviderInterface;
|
28 | 32 | use Symfony\Contracts\Service\ServiceSubscriberInterface;
|
29 | 33 |
|
@@ -506,6 +510,41 @@ public function testTaggedServiceLocatorWithDefaultIndex()
|
506 | 510 | ];
|
507 | 511 | $this->assertSame($expected, ['baz' => $serviceLocator->get('baz')]);
|
508 | 512 | }
|
| 513 | + |
| 514 | + /** |
| 515 | + * @requires PHP 8 |
| 516 | + */ |
| 517 | + public function testTagsViaAttribute() |
| 518 | + { |
| 519 | + $container = new ContainerBuilder(); |
| 520 | + $container->register('one', TaggedService1::class) |
| 521 | + ->setPublic(true) |
| 522 | + ->setAutoconfigured(true); |
| 523 | + $container->register('two', TaggedService2::class) |
| 524 | + ->setPublic(true) |
| 525 | + ->setAutoconfigured(true); |
| 526 | + $container->register('three', TaggedService3::class) |
| 527 | + ->setPublic(true) |
| 528 | + ->setAutoconfigured(true); |
| 529 | + |
| 530 | + $collector = new TagCollector(); |
| 531 | + $container->addCompilerPass($collector); |
| 532 | + |
| 533 | + $container->compile(); |
| 534 | + |
| 535 | + self::assertSame([ |
| 536 | + 'one' => [ |
| 537 | + ['foo' => 'bar', 'priority' => 0], |
| 538 | + ['bar' => 'baz', 'priority' => 0], |
| 539 | + ], |
| 540 | + 'two' => [ |
| 541 | + ['someAttribute' => 'prio 100', 'priority' => 100], |
| 542 | + ], |
| 543 | + 'three' => [ |
| 544 | + ['someAttribute' => 'custom_tag_class'], |
| 545 | + ], |
| 546 | + ], $collector->collectedTags); |
| 547 | + } |
509 | 548 | }
|
510 | 549 |
|
511 | 550 | class ServiceSubscriberStub implements ServiceSubscriberInterface
|
@@ -566,3 +605,13 @@ public function setSunshine($type)
|
566 | 605 | {
|
567 | 606 | }
|
568 | 607 | }
|
| 608 | + |
| 609 | +final class TagCollector implements CompilerPassInterface |
| 610 | +{ |
| 611 | + public $collectedTags; |
| 612 | + |
| 613 | + public function process(ContainerBuilder $container): void |
| 614 | + { |
| 615 | + $this->collectedTags = $container->findTaggedServiceIds('app.custom_tag'); |
| 616 | + } |
| 617 | +} |
0 commit comments