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.     /**
  50.      * @param callable $listener can not use native type declaration @see https://github.com/symfony/symfony/issues/42283
  51.      */
  52.     public function addListener(string $eventName$listenerint $priority 0): void
  53.     {
  54.         $this->dispatcher->addListener($eventName$listener$priority);
  55.     }
  56.     public function addSubscriber(EventSubscriberInterface $subscriber): void
  57.     {
  58.         $this->dispatcher->addSubscriber($subscriber);
  59.     }
  60.     /**
  61.      * @param callable $listener can not use native type hint as it is incompatible with symfony <5.3.4
  62.      */
  63.     public function removeListener(string $eventName$listener): void
  64.     {
  65.         $this->dispatcher->removeListener($eventName$listener);
  66.     }
  67.     public function removeSubscriber(EventSubscriberInterface $subscriber): void
  68.     {
  69.         $this->dispatcher->removeSubscriber($subscriber);
  70.     }
  71.     public function getListeners(?string $eventName null): array
  72.     {
  73.         return $this->dispatcher->getListeners($eventName);
  74.     }
  75.     /**
  76.      * @param callable $listener can not use native type hint as it is incompatible with symfony <5.3.4
  77.      */
  78.     public function getListenerPriority(string $eventName$listener): ?int
  79.     {
  80.         return $this->dispatcher->getListenerPriority($eventName$listener);
  81.     }
  82.     public function hasListeners(?string $eventName null): bool
  83.     {
  84.         return $this->dispatcher->hasListeners($eventName);
  85.     }
  86.     private function getActions(BusinessEventInterface $eventContext $context): EventActionCollection
  87.     {
  88.         $name $event->getName();
  89.         $criteria = new Criteria();
  90.         $criteria->addFilter(new EqualsFilter('event_action.eventName'$name));
  91.         $criteria->addFilter(new EqualsFilter('event_action.active'true));
  92.         $criteria->addFilter(new OrFilter([
  93.             new EqualsFilter('event_action.rules.id'null),
  94.             new EqualsAnyFilter('event_action.rules.id'$context->getRuleIds()),
  95.         ]));
  96.         if ($event instanceof SalesChannelAware) {
  97.             $criteria->addFilter(new OrFilter([
  98.                 new EqualsFilter('salesChannels.id'$event->getSalesChannelId()),
  99.                 new EqualsFilter('salesChannels.id'null),
  100.             ]));
  101.         }
  102.         /** @var EventActionCollection $events */
  103.         $events $this->definitionRegistry
  104.             ->getRepository($this->eventActionDefinition->getEntityName())
  105.             ->search($criteria$context)
  106.             ->getEntities();
  107.         return $events;
  108.     }
  109.     private function callActions(BusinessEventInterface $event): void
  110.     {
  111.         $actions $this->getActions($event$event->getContext());
  112.         foreach ($actions as $action) {
  113.             $actionEvent = new BusinessEvent($action->getActionName(), $event$action->getConfig());
  114.             $this->dispatcher->dispatch($actionEvent$actionEvent->getActionName());
  115.         }
  116.         $globalEvent = new BusinessEvent(BusinessEvents::GLOBAL_EVENT$event);
  117.         $this->dispatcher->dispatch($globalEvent$globalEvent->getActionName());
  118.     }
  119. }