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

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