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

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\EqualsAnyFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
  10. use Shopware\Core\Framework\Event\EventAction\EventActionCollection;
  11. use Shopware\Core\Framework\Event\EventAction\EventActionDefinition;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class BusinessEventDispatcher implements EventDispatcherInterface
  15. {
  16.     /**
  17.      * @var EventDispatcherInterface
  18.      */
  19.     private $dispatcher;
  20.     /**
  21.      * @var EventActionDefinition
  22.      */
  23.     private $eventActionDefinition;
  24.     /**
  25.      * @var DefinitionInstanceRegistry
  26.      */
  27.     private $definitionRegistry;
  28.     public function __construct(
  29.         EventDispatcherInterface $dispatcher,
  30.         DefinitionInstanceRegistry $definitionRegistry,
  31.         EventActionDefinition $eventActionDefinition
  32.     ) {
  33.         $this->dispatcher $dispatcher;
  34.         $this->eventActionDefinition $eventActionDefinition;
  35.         $this->definitionRegistry $definitionRegistry;
  36.     }
  37.     public function dispatch($event, ?string $eventName null): object
  38.     {
  39.         $event $this->dispatcher->dispatch($event$eventName);
  40.         if (!$event instanceof BusinessEventInterface) {
  41.             return $event;
  42.         }
  43.         if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
  44.             return $event;
  45.         }
  46.         $this->callActions($event);
  47.         return $event;
  48.     }
  49.     public function addListener($eventName$listener$priority 0): void
  50.     {
  51.         $this->dispatcher->addListener($eventName$listener$priority);
  52.     }
  53.     public function addSubscriber(EventSubscriberInterface $subscriber): void
  54.     {
  55.         $this->dispatcher->addSubscriber($subscriber);
  56.     }
  57.     public function removeListener($eventName$listener): void
  58.     {
  59.         $this->dispatcher->removeListener($eventName$listener);
  60.     }
  61.     public function removeSubscriber(EventSubscriberInterface $subscriber): void
  62.     {
  63.         $this->dispatcher->removeSubscriber($subscriber);
  64.     }
  65.     public function getListeners($eventName null): array
  66.     {
  67.         return $this->dispatcher->getListeners($eventName);
  68.     }
  69.     public function getListenerPriority($eventName$listener): ?int
  70.     {
  71.         return $this->dispatcher->getListenerPriority($eventName$listener);
  72.     }
  73.     public function hasListeners($eventName null): bool
  74.     {
  75.         return $this->dispatcher->hasListeners($eventName);
  76.     }
  77.     private function getActions(BusinessEventInterface $eventContext $context): EventActionCollection
  78.     {
  79.         $name $event->getName();
  80.         $criteria = new Criteria();
  81.         $criteria->addFilter(new EqualsFilter('event_action.eventName'$name));
  82.         $criteria->addFilter(new EqualsFilter('event_action.active'true));
  83.         $criteria->addFilter(new OrFilter([
  84.             new EqualsFilter('event_action.rules.id'null),
  85.             new EqualsAnyFilter('event_action.rules.id'$context->getRuleIds()),
  86.         ]));
  87.         if ($event instanceof SalesChannelAware) {
  88.             $criteria->addFilter(new OrFilter([
  89.                 new EqualsFilter('salesChannels.id'$event->getSalesChannelId()),
  90.                 new EqualsFilter('salesChannels.id'null),
  91.             ]));
  92.         }
  93.         /** @var EventActionCollection $events */
  94.         $events $this->definitionRegistry
  95.             ->getRepository($this->eventActionDefinition->getEntityName())
  96.             ->search($criteria$context)
  97.             ->getEntities();
  98.         return $events;
  99.     }
  100.     private function callActions(BusinessEventInterface $event): void
  101.     {
  102.         $actions $this->getActions($event$event->getContext());
  103.         foreach ($actions as $action) {
  104.             $actionEvent = new BusinessEvent($action->getActionName(), $event$action->getConfig());
  105.             $this->dispatcher->dispatch($actionEvent$actionEvent->getActionName());
  106.         }
  107.         $globalEvent = new BusinessEvent(BusinessEvents::GLOBAL_EVENT$event);
  108.         $this->dispatcher->dispatch($globalEvent$globalEvent->getActionName());
  109.     }
  110. }