vendor/shopware/core/Framework/Event/BusinessEventDispatcher.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Event;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Event\EventAction\EventActionCollection;
  8. use Shopware\Core\Framework\Event\EventAction\EventActionDefinition;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class BusinessEventDispatcher implements EventDispatcherInterface
  12. {
  13.     /**
  14.      * @var EventDispatcherInterface
  15.      */
  16.     private $dispatcher;
  17.     /**
  18.      * @var EventActionDefinition
  19.      */
  20.     private $eventActionDefinition;
  21.     /**
  22.      * @var DefinitionInstanceRegistry
  23.      */
  24.     private $definitionRegistry;
  25.     public function __construct(
  26.         EventDispatcherInterface $dispatcher,
  27.         DefinitionInstanceRegistry $definitionRegistry,
  28.         EventActionDefinition $eventActionDefinition
  29.     ) {
  30.         $this->dispatcher $dispatcher;
  31.         $this->eventActionDefinition $eventActionDefinition;
  32.         $this->definitionRegistry $definitionRegistry;
  33.     }
  34.     public function dispatch($event, ?string $eventName null): object
  35.     {
  36.         $event $this->dispatcher->dispatch($event$eventName);
  37.         if ($event instanceof BusinessEventInterface) {
  38.             $this->callActions($event);
  39.         }
  40.         return $event;
  41.     }
  42.     public function addListener($eventName$listener$priority 0): void
  43.     {
  44.         $this->dispatcher->addListener($eventName$listener$priority);
  45.     }
  46.     public function addSubscriber(EventSubscriberInterface $subscriber): void
  47.     {
  48.         $this->dispatcher->addSubscriber($subscriber);
  49.     }
  50.     public function removeListener($eventName$listener): void
  51.     {
  52.         $this->dispatcher->removeListener($eventName$listener);
  53.     }
  54.     public function removeSubscriber(EventSubscriberInterface $subscriber): void
  55.     {
  56.         $this->dispatcher->removeSubscriber($subscriber);
  57.     }
  58.     public function getListeners($eventName null): array
  59.     {
  60.         return $this->dispatcher->getListeners($eventName);
  61.     }
  62.     public function getListenerPriority($eventName$listener): ?int
  63.     {
  64.         return $this->dispatcher->getListenerPriority($eventName$listener);
  65.     }
  66.     public function hasListeners($eventName null): bool
  67.     {
  68.         return $this->dispatcher->hasListeners($eventName);
  69.     }
  70.     private function getActions(string $eventNameContext $context): EventActionCollection
  71.     {
  72.         $criteria = new Criteria();
  73.         $criteria->addFilter(new EqualsFilter('event_action.eventName'$eventName));
  74.         /** @var EventActionCollection $events */
  75.         $events $this->definitionRegistry
  76.             ->getRepository($this->eventActionDefinition->getEntityName())
  77.             ->search($criteria$context)
  78.             ->getEntities();
  79.         return $events;
  80.     }
  81.     private function callActions(BusinessEventInterface $event): void
  82.     {
  83.         $actions $this->getActions($event->getName(), $event->getContext());
  84.         foreach ($actions as $action) {
  85.             $actionEvent = new BusinessEvent($action->getActionName(), $event$action->getConfig());
  86.             $this->dispatcher->dispatch($actionEvent$actionEvent->getActionName());
  87.         }
  88.         $globalEvent = new BusinessEvent(BusinessEvents::GLOBAL_EVENT$event);
  89.         $this->dispatcher->dispatch($globalEvent$globalEvent->getActionName());
  90.     }
  91. }