vendor/symfony/doctrine-bridge/ContainerAwareEventManager.php line 45

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Doctrine;
  11. use Doctrine\Common\EventArgs;
  12. use Doctrine\Common\EventManager;
  13. use Psr\Container\ContainerInterface;
  14. /**
  15.  * Allows lazy loading of listener services.
  16.  *
  17.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  18.  */
  19. class ContainerAwareEventManager extends EventManager
  20. {
  21.     /**
  22.      * Map of registered listeners.
  23.      *
  24.      * <event> => <listeners>
  25.      */
  26.     private $listeners = [];
  27.     private $initialized = [];
  28.     private $methods = [];
  29.     private $container;
  30.     public function __construct(ContainerInterface $container)
  31.     {
  32.         $this->container $container;
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      *
  37.      * @return void
  38.      */
  39.     public function dispatchEvent($eventNameEventArgs $eventArgs null)
  40.     {
  41.         if (!isset($this->listeners[$eventName])) {
  42.             return;
  43.         }
  44.         $eventArgs null === $eventArgs EventArgs::getEmptyInstance() : $eventArgs;
  45.         if (!isset($this->initialized[$eventName])) {
  46.             $this->initializeListeners($eventName);
  47.         }
  48.         foreach ($this->listeners[$eventName] as $hash => $listener) {
  49.             $listener->{$this->methods[$eventName][$hash]}($eventArgs);
  50.         }
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      *
  55.      * @return object[][]
  56.      */
  57.     public function getListeners($event null)
  58.     {
  59.         if (null !== $event) {
  60.             if (!isset($this->initialized[$event])) {
  61.                 $this->initializeListeners($event);
  62.             }
  63.             return $this->listeners[$event];
  64.         }
  65.         foreach ($this->listeners as $event => $listeners) {
  66.             if (!isset($this->initialized[$event])) {
  67.                 $this->initializeListeners($event);
  68.             }
  69.         }
  70.         return $this->listeners;
  71.     }
  72.     /**
  73.      * {@inheritdoc}
  74.      *
  75.      * @return bool
  76.      */
  77.     public function hasListeners($event)
  78.     {
  79.         return isset($this->listeners[$event]) && $this->listeners[$event];
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      *
  84.      * @return void
  85.      */
  86.     public function addEventListener($events$listener)
  87.     {
  88.         $hash $this->getHash($listener);
  89.         foreach ((array) $events as $event) {
  90.             // Overrides listener if a previous one was associated already
  91.             // Prevents duplicate listeners on same event (same instance only)
  92.             $this->listeners[$event][$hash] = $listener;
  93.             if (\is_string($listener)) {
  94.                 unset($this->initialized[$event]);
  95.             } else {
  96.                 $this->methods[$event][$hash] = $this->getMethod($listener$event);
  97.             }
  98.         }
  99.     }
  100.     /**
  101.      * {@inheritdoc}
  102.      *
  103.      * @return void
  104.      */
  105.     public function removeEventListener($events$listener)
  106.     {
  107.         $hash $this->getHash($listener);
  108.         foreach ((array) $events as $event) {
  109.             // Check if we actually have this listener associated
  110.             if (isset($this->listeners[$event][$hash])) {
  111.                 unset($this->listeners[$event][$hash]);
  112.             }
  113.             if (isset($this->methods[$event][$hash])) {
  114.                 unset($this->methods[$event][$hash]);
  115.             }
  116.         }
  117.     }
  118.     private function initializeListeners(string $eventName)
  119.     {
  120.         foreach ($this->listeners[$eventName] as $hash => $listener) {
  121.             if (\is_string($listener)) {
  122.                 $this->listeners[$eventName][$hash] = $listener $this->container->get($listener);
  123.                 $this->methods[$eventName][$hash] = $this->getMethod($listener$eventName);
  124.             }
  125.         }
  126.         $this->initialized[$eventName] = true;
  127.     }
  128.     /**
  129.      * @param string|object $listener
  130.      */
  131.     private function getHash($listener): string
  132.     {
  133.         if (\is_string($listener)) {
  134.             return '_service_'.$listener;
  135.         }
  136.         return spl_object_hash($listener);
  137.     }
  138.     /**
  139.      * @param object $listener
  140.      */
  141.     private function getMethod($listenerstring $event): string
  142.     {
  143.         if (!method_exists($listener$event) && method_exists($listener'__invoke')) {
  144.             return '__invoke';
  145.         }
  146.         return $event;
  147.     }
  148. }