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