vendor/enqueue/async-event-dispatcher/DependencyInjection/AsyncEventsPass.php line 93

Open in your IDE?
  1. <?php
  2. namespace Enqueue\AsyncEventDispatcher\DependencyInjection;
  3. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  4. use Symfony\Component\DependencyInjection\ContainerBuilder;
  5. use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class AsyncEventsPass implements CompilerPassInterface
  8. {
  9.     public function process(ContainerBuilder $container): void
  10.     {
  11.         if (false == $container->hasDefinition('enqueue.events.async_listener')) {
  12.             return;
  13.         }
  14.         if (false == $container->hasDefinition('enqueue.events.registry')) {
  15.             return;
  16.         }
  17.         $defaultClient $container->getParameter('enqueue.default_client');
  18.         $registeredToEvent = [];
  19.         foreach ($container->findTaggedServiceIds('kernel.event_listener') as $serviceId => $tagAttributes) {
  20.             foreach ($tagAttributes as $tagAttribute) {
  21.                 if (false == isset($tagAttribute['async'])) {
  22.                     continue;
  23.                 }
  24.                 $event $tagAttribute['event'];
  25.                 $service $container->getDefinition($serviceId);
  26.                 $service->clearTag('kernel.event_listener');
  27.                 $service->addTag('enqueue.async_event_listener'$tagAttribute);
  28.                 if (false == isset($registeredToEvent[$event])) {
  29.                     $container->getDefinition('enqueue.events.async_listener')
  30.                         ->addTag('kernel.event_listener', [
  31.                             'event' => $event,
  32.                             'method' => 'onEvent',
  33.                         ])
  34.                     ;
  35.                     $container->getDefinition('enqueue.events.async_processor')
  36.                         ->addTag('enqueue.processor', [
  37.                             'topic' => 'event.'.$event,
  38.                             'client' => $defaultClient,
  39.                         ])
  40.                     ;
  41.                     $registeredToEvent[$event] = true;
  42.                 }
  43.             }
  44.         }
  45.         foreach ($container->findTaggedServiceIds('kernel.event_subscriber') as $serviceId => $tagAttributes) {
  46.             foreach ($tagAttributes as $tagAttribute) {
  47.                 if (false == isset($tagAttribute['async'])) {
  48.                     continue;
  49.                 }
  50.                 $service $container->getDefinition($serviceId);
  51.                 $service->clearTag('kernel.event_subscriber');
  52.                 $service->addTag('enqueue.async_event_subscriber'$tagAttribute);
  53.                 /** @var EventSubscriberInterface $serviceClass */
  54.                 $serviceClass $service->getClass();
  55.                 foreach ($serviceClass::getSubscribedEvents() as $event => $data) {
  56.                     if (false == isset($registeredToEvent[$event])) {
  57.                         $container->getDefinition('enqueue.events.async_listener')
  58.                             ->addTag('kernel.event_listener', [
  59.                                 'event' => $event,
  60.                                 'method' => 'onEvent',
  61.                             ])
  62.                         ;
  63.                         $container->getDefinition('enqueue.events.async_processor')
  64.                             ->addTag('enqueue.processor', [
  65.                                 'topicName' => 'event.'.$event,
  66.                                 'client' => $defaultClient,
  67.                             ])
  68.                         ;
  69.                         $registeredToEvent[$event] = true;
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.         $registerListenersPass = new RegisterListenersPass(
  75.             'enqueue.events.event_dispatcher',
  76.             'enqueue.async_event_listener',
  77.             'enqueue.async_event_subscriber'
  78.         );
  79.         $registerListenersPass->process($container);
  80.     }
  81. }