2010-02-17 13:54:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-02-17 13:54:36 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:54:36 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle;
|
|
|
|
|
2011-01-23 17:02:16 +00:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
2011-03-13 18:16:56 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
|
|
|
use Symfony\Component\EventDispatcher\Event;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2010-02-17 13:54:36 +00:00
|
|
|
/**
|
2011-03-07 14:42:52 +00:00
|
|
|
* Lazily loads listeners and subscribers from the dependency injection
|
|
|
|
* container
|
2010-02-17 13:54:36 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-03-07 14:42:52 +00:00
|
|
|
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
|
2010-02-17 13:54:36 +00:00
|
|
|
*/
|
2011-03-13 18:16:56 +00:00
|
|
|
class ContainerAwareEventDispatcher extends EventDispatcher
|
2010-02-17 13:54:36 +00:00
|
|
|
{
|
2011-03-07 14:42:52 +00:00
|
|
|
/**
|
|
|
|
* The container from where services are loaded
|
|
|
|
* @var ContainerInterface
|
|
|
|
*/
|
|
|
|
private $container;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The service IDs of the event listeners and subscribers
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-04-18 13:52:50 +01:00
|
|
|
private $listenerIds = array();
|
2011-01-23 17:02:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container A ContainerInterface instance
|
|
|
|
*/
|
|
|
|
public function __construct(ContainerInterface $container)
|
|
|
|
{
|
|
|
|
$this->container = $container;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-07 14:42:52 +00:00
|
|
|
* Adds a service as event listener
|
|
|
|
*
|
2011-04-06 08:31:06 +01:00
|
|
|
* @param string|array $eventNames One or more events for which the listener is added
|
|
|
|
* @param string $serviceId The ID of the listener service
|
|
|
|
* @param integer $priority The higher this value, the earlier an event listener
|
|
|
|
* will be triggered in the chain.
|
|
|
|
* Defaults to 0.
|
2011-01-23 17:02:16 +00:00
|
|
|
*/
|
2011-04-06 08:31:06 +01:00
|
|
|
public function addListenerService($eventNames, $serviceId, $priority = 0)
|
2010-02-17 13:54:36 +00:00
|
|
|
{
|
2011-03-07 14:42:52 +00:00
|
|
|
if (!is_string($serviceId)) {
|
|
|
|
throw new \InvalidArgumentException('Expected a string argument');
|
|
|
|
}
|
|
|
|
|
2011-04-06 08:31:06 +01:00
|
|
|
foreach ((array) $eventNames as $event) {
|
2011-03-07 14:42:52 +00:00
|
|
|
// Prevent duplicate entries
|
|
|
|
$this->listenerIds[$event][$serviceId] = $priority;
|
2011-01-24 15:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-07 14:42:52 +00:00
|
|
|
* {@inheritDoc}
|
|
|
|
*
|
|
|
|
* Lazily loads listeners for this event from the dependency injection
|
|
|
|
* container.
|
2011-01-24 15:46:04 +00:00
|
|
|
*/
|
2011-03-17 14:27:42 +00:00
|
|
|
public function dispatch($eventName, Event $event = null)
|
2011-01-24 15:46:04 +00:00
|
|
|
{
|
2011-03-07 14:42:52 +00:00
|
|
|
if (isset($this->listenerIds[$eventName])) {
|
|
|
|
foreach ($this->listenerIds[$eventName] as $serviceId => $priority) {
|
2011-03-17 14:27:42 +00:00
|
|
|
$this->addListener($eventName, $this->container->get($serviceId), $priority);
|
2011-01-05 11:13:27 +00:00
|
|
|
}
|
2010-02-17 13:54:36 +00:00
|
|
|
}
|
2011-01-23 17:02:16 +00:00
|
|
|
|
2011-03-17 14:27:42 +00:00
|
|
|
parent::dispatch($eventName, $event);
|
2010-02-17 13:54:36 +00:00
|
|
|
}
|
|
|
|
}
|