vendor/symfony/src/Symfony/Bundle/FrameworkBundle/ContainerAwareEventDispatcher.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\FrameworkBundle;
       
    13 
       
    14 use Symfony\Component\DependencyInjection\ContainerInterface;
       
    15 use Symfony\Component\EventDispatcher\EventDispatcher;
       
    16 use Symfony\Component\EventDispatcher\Event;
       
    17 
       
    18 /**
       
    19  * Lazily loads listeners and subscribers from the dependency injection
       
    20  * container
       
    21  *
       
    22  * @author Fabien Potencier <fabien@symfony.com>
       
    23  * @author Bernhard Schussek <bernhard.schussek@symfony.com>
       
    24  */
       
    25 class ContainerAwareEventDispatcher extends EventDispatcher
       
    26 {
       
    27     /**
       
    28      * The container from where services are loaded
       
    29      * @var ContainerInterface
       
    30      */
       
    31     private $container;
       
    32 
       
    33     /**
       
    34      * The service IDs of the event listeners and subscribers
       
    35      * @var array
       
    36      */
       
    37     private $listenerIds = array();
       
    38 
       
    39     /**
       
    40      * The services registered as listeners
       
    41      * @var array
       
    42      */
       
    43     private $listeners = array();
       
    44 
       
    45     /**
       
    46      * Constructor.
       
    47      *
       
    48      * @param ContainerInterface $container A ContainerInterface instance
       
    49      */
       
    50     public function __construct(ContainerInterface $container)
       
    51     {
       
    52         $this->container = $container;
       
    53     }
       
    54 
       
    55     /**
       
    56      * Adds a service as event listener
       
    57      *
       
    58      * @param string   $eventName Event for which the listener is added
       
    59      * @param array    $callback  The service ID of the listener service & the method
       
    60      *                            name that has to be called
       
    61      * @param integer  $priority  The higher this value, the earlier an event listener
       
    62      *                            will be triggered in the chain.
       
    63      *                            Defaults to 0.
       
    64      */
       
    65     public function addListenerService($eventName, $callback, $priority = 0)
       
    66     {
       
    67         if (!is_array($callback) || 2 !== count($callback)) {
       
    68             throw new \InvalidArgumentException('Expected an array("service", "method") argument');
       
    69         }
       
    70 
       
    71         $this->listenerIds[$eventName][] = array($callback[0], $callback[1], $priority);
       
    72     }
       
    73 
       
    74     /**
       
    75      * {@inheritDoc}
       
    76      *
       
    77      * Lazily loads listeners for this event from the dependency injection
       
    78      * container.
       
    79      *
       
    80      * @throws \InvalidArgumentException if the service is not defined
       
    81      */
       
    82     public function dispatch($eventName, Event $event = null)
       
    83     {
       
    84         if (isset($this->listenerIds[$eventName])) {
       
    85             foreach ($this->listenerIds[$eventName] as $args) {
       
    86                 list($serviceId, $method, $priority) = $args;
       
    87                 $listener = $this->container->get($serviceId);
       
    88 
       
    89                 $key = $serviceId.'.'.$method;
       
    90                 if (!isset($this->listeners[$eventName][$key])) {
       
    91                     $this->addListener($eventName, array($listener, $method), $priority);
       
    92                 } elseif ($listener !== $this->listeners[$eventName][$key]) {
       
    93                     $this->removeListener($eventName, array($this->listeners[$eventName][$key], $method));
       
    94                     $this->addListener($eventName, array($listener, $method), $priority);
       
    95                 }
       
    96 
       
    97                 $this->listeners[$eventName][$key] = $listener;
       
    98             }
       
    99         }
       
   100 
       
   101         parent::dispatch($eventName, $event);
       
   102     }
       
   103 }