vendor/zenstruck/messenger-test/src/ZenstruckMessengerTestBundle.php line 30

  1. <?php
  2. /*
  3.  * This file is part of the zenstruck/messenger-test package.
  4.  *
  5.  * (c) Kevin Bond <kevinbond@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Zenstruck\Messenger\Test;
  11. use Psr\Clock\ClockInterface;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. use Symfony\Component\HttpKernel\Bundle\Bundle;
  18. use Symfony\Component\Messenger\Transport\TransportInterface;
  19. use Zenstruck\Messenger\Test\Bus\TestBus;
  20. use Zenstruck\Messenger\Test\Bus\TestBusRegistry;
  21. use Zenstruck\Messenger\Test\Transport\TestTransportFactory;
  22. use Zenstruck\Messenger\Test\Transport\TestTransportRegistry;
  23. /**
  24.  * @author Kevin Bond <kevinbond@gmail.com>
  25.  */
  26. final class ZenstruckMessengerTestBundle extends Bundle implements CompilerPassInterface
  27. {
  28.     public function build(ContainerBuilder $container): void
  29.     {
  30.         $container->register('zenstruck_messenger_test.transport_factory'TestTransportFactory::class)
  31.             ->setArguments([
  32.                 new Reference('messenger.routable_message_bus'),
  33.                 new Reference('event_dispatcher'),
  34.                 new Reference(ClockInterface::class, invalidBehaviorContainerInterface::NULL_ON_INVALID_REFERENCE),
  35.             ])
  36.             ->addTag('messenger.transport_factory')
  37.         ;
  38.         $container->register('zenstruck_messenger_test.transport_registry'TestTransportRegistry::class)
  39.             ->setPublic(true)
  40.         ;
  41.         $container->register('zenstruck_messenger_test.bus_registry'TestBusRegistry::class)
  42.             ->setPublic(true)
  43.         ;
  44.         $container->addCompilerPass($this);
  45.     }
  46.     public function getContainerExtension(): ?ExtensionInterface
  47.     {
  48.         return null;
  49.     }
  50.     public function process(ContainerBuilder $container): void
  51.     {
  52.         $transportRegistry $container->getDefinition('zenstruck_messenger_test.transport_registry');
  53.         foreach ($container->findTaggedServiceIds('messenger.receiver') as $id => $tags) {
  54.             $name $id;
  55.             if (!$class $container->getDefinition($name)->getClass()) {
  56.                 continue;
  57.             }
  58.             if (!\is_a($classTransportInterface::class, true)) {
  59.                 continue;
  60.             }
  61.             foreach ($tags as $tag) {
  62.                 if (isset($tag['alias'])) {
  63.                     $name $tag['alias'];
  64.                 }
  65.             }
  66.             $transportRegistry->addMethodCall('register', [$name, new Reference($id)]);
  67.         }
  68.         $busRegistry $container->getDefinition('zenstruck_messenger_test.bus_registry');
  69.         foreach ($container->findTaggedServiceIds('messenger.bus') as $id => $tags) {
  70.             $name "{$id}.test-bus";
  71.             $busRegistry->addMethodCall('register', [$id, new Reference($name)]);
  72.             $container->register($nameTestBus::class)
  73.                 ->setAutowired(true)
  74.                 ->setPublic(true)
  75.                 ->setArgument('$name'$id)
  76.                 ->setDecoratedService($id)
  77.             ;
  78.         }
  79.     }
  80. }