2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-08-21 13:32:21 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-04-07 02:07:59 +01:00
|
|
|
*
|
2011-08-21 13:32:21 +01:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-01-04 14:26:20 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\EventDispatcher;
|
|
|
|
|
2010-01-04 14:26:20 +00:00
|
|
|
/**
|
2011-03-17 14:27:42 +00:00
|
|
|
* The EventDispatcherInterface is the central point of Symfony's event listener system.
|
|
|
|
*
|
2011-03-13 18:03:18 +00:00
|
|
|
* Listeners are registered on the manager and events are dispatched through the
|
|
|
|
* manager.
|
2010-01-04 14:26:20 +00:00
|
|
|
*
|
2011-03-13 18:03:18 +00:00
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2011-03-18 08:01:19 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-05-05 08:15:48 +01:00
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
2011-03-24 09:06:26 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-01-04 14:26:20 +00:00
|
|
|
*/
|
2011-01-25 13:23:23 +00:00
|
|
|
class EventDispatcher implements EventDispatcherInterface
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-03-13 18:03:18 +00:00
|
|
|
private $listeners = array();
|
2011-05-30 15:00:29 +01:00
|
|
|
private $sorted = array();
|
2011-03-13 18:03:18 +00:00
|
|
|
|
|
|
|
/**
|
2011-03-17 14:27:42 +00:00
|
|
|
* @see EventDispatcherInterface::dispatch
|
2011-03-24 09:06:26 +00:00
|
|
|
*
|
|
|
|
* @api
|
2011-03-13 18:03:18 +00:00
|
|
|
*/
|
2011-03-17 14:27:42 +00:00
|
|
|
public function dispatch($eventName, Event $event = null)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-03-18 08:01:19 +00:00
|
|
|
if (!isset($this->listeners[$eventName])) {
|
|
|
|
return;
|
|
|
|
}
|
2010-12-03 17:47:54 +00:00
|
|
|
|
2011-03-18 08:01:19 +00:00
|
|
|
if (null === $event) {
|
|
|
|
$event = new Event();
|
|
|
|
}
|
2011-03-13 18:03:18 +00:00
|
|
|
|
2011-05-05 08:15:48 +01:00
|
|
|
$this->doDispatch($this->getListeners($eventName), $eventName, $event);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
2011-03-13 18:03:18 +00:00
|
|
|
* @see EventDispatcherInterface::getListeners
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-03-13 18:03:18 +00:00
|
|
|
public function getListeners($eventName = null)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-03-18 08:01:19 +00:00
|
|
|
if (null !== $eventName) {
|
2011-05-30 15:00:29 +01:00
|
|
|
if (!isset($this->sorted[$eventName])) {
|
|
|
|
$this->sortListeners($eventName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->sorted[$eventName];
|
2011-03-13 18:03:18 +00:00
|
|
|
}
|
|
|
|
|
2011-03-18 17:48:26 +00:00
|
|
|
foreach (array_keys($this->listeners) as $eventName) {
|
2011-05-30 15:00:29 +01:00
|
|
|
if (!isset($this->sorted[$eventName])) {
|
|
|
|
$this->sortListeners($eventName);
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2011-05-30 15:00:29 +01:00
|
|
|
return $this->sorted;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2011-03-13 18:03:18 +00:00
|
|
|
* @see EventDispatcherInterface::hasListeners
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-03-18 08:01:19 +00:00
|
|
|
public function hasListeners($eventName = null)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-03-18 08:01:19 +00:00
|
|
|
return (Boolean) count($this->getListeners($eventName));
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2011-03-17 14:27:42 +00:00
|
|
|
* @see EventDispatcherInterface::addListener
|
2011-03-24 09:06:26 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-05-05 18:02:52 +01:00
|
|
|
public function addListener($eventName, $listener, $priority = 0)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-05-05 18:02:52 +01:00
|
|
|
$this->listeners[$eventName][$priority][] = $listener;
|
2011-05-30 15:00:29 +01:00
|
|
|
unset($this->sorted[$eventName]);
|
2011-03-13 18:03:18 +00:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-03-13 18:03:18 +00:00
|
|
|
/**
|
2011-03-17 14:27:42 +00:00
|
|
|
* @see EventDispatcherInterface::removeListener
|
2011-03-13 18:03:18 +00:00
|
|
|
*/
|
2011-05-05 18:02:52 +01:00
|
|
|
public function removeListener($eventName, $listener)
|
2011-03-13 18:03:18 +00:00
|
|
|
{
|
2011-05-05 18:02:52 +01:00
|
|
|
if (!isset($this->listeners[$eventName])) {
|
|
|
|
return;
|
|
|
|
}
|
2011-03-18 08:01:19 +00:00
|
|
|
|
2011-05-05 18:02:52 +01:00
|
|
|
foreach ($this->listeners[$eventName] as $priority => $listeners) {
|
|
|
|
if (false !== ($key = array_search($listener, $listeners))) {
|
2011-05-30 15:00:29 +01:00
|
|
|
unset($this->listeners[$eventName][$priority][$key], $this->sorted[$eventName]);
|
2011-03-13 18:03:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-26 13:08:42 +01:00
|
|
|
|
2011-03-13 18:03:18 +00:00
|
|
|
/**
|
2011-03-17 14:27:42 +00:00
|
|
|
* @see EventDispatcherInterface::addSubscriber
|
2011-06-14 13:40:48 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-03-13 18:03:18 +00:00
|
|
|
*/
|
2011-06-14 13:40:27 +01:00
|
|
|
public function addSubscriber(EventSubscriberInterface $subscriber)
|
2011-03-13 18:03:18 +00:00
|
|
|
{
|
2011-06-14 13:40:27 +01:00
|
|
|
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
|
|
|
|
if (is_string($params)) {
|
|
|
|
$this->addListener($eventName, array($subscriber, $params));
|
|
|
|
} else {
|
2012-04-12 11:03:16 +01:00
|
|
|
$this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0);
|
2011-06-14 13:40:27 +01:00
|
|
|
}
|
2011-05-05 18:02:52 +01:00
|
|
|
}
|
2011-03-17 14:27:42 +00:00
|
|
|
}
|
|
|
|
|
2011-03-18 08:00:58 +00:00
|
|
|
/**
|
|
|
|
* @see EventDispatcherInterface::removeSubscriber
|
|
|
|
*/
|
|
|
|
public function removeSubscriber(EventSubscriberInterface $subscriber)
|
|
|
|
{
|
2011-08-23 21:41:08 +01:00
|
|
|
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
|
|
|
|
$this->removeListener($eventName, array($subscriber, is_string($params) ? $params : $params[0]));
|
2011-05-05 18:02:52 +01:00
|
|
|
}
|
2011-03-18 08:00:58 +00:00
|
|
|
}
|
|
|
|
|
2011-03-17 14:27:42 +00:00
|
|
|
/**
|
2011-05-05 08:15:48 +01:00
|
|
|
* Triggers the listeners of an event.
|
2011-03-17 14:27:42 +00:00
|
|
|
*
|
|
|
|
* This method can be overridden to add functionality that is executed
|
|
|
|
* for each listener.
|
|
|
|
*
|
2011-05-05 08:15:48 +01:00
|
|
|
* @param array[callback] $listeners The event listeners.
|
|
|
|
* @param string $eventName The name of the event to dispatch.
|
|
|
|
* @param Event $event The event object to pass to the event handlers/listeners.
|
2011-03-17 14:27:42 +00:00
|
|
|
*/
|
2011-05-05 08:15:48 +01:00
|
|
|
protected function doDispatch($listeners, $eventName, Event $event)
|
2011-03-17 14:27:42 +00:00
|
|
|
{
|
2011-05-05 08:15:48 +01:00
|
|
|
foreach ($listeners as $listener) {
|
2011-05-26 10:56:59 +01:00
|
|
|
call_user_func($listener, $event);
|
2011-05-05 08:15:48 +01:00
|
|
|
if ($event->isPropagationStopped()) {
|
|
|
|
break;
|
|
|
|
}
|
2011-03-17 14:27:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sorts the internal list of listeners for the given event by priority.
|
|
|
|
*
|
2011-03-18 08:01:19 +00:00
|
|
|
* @param string $eventName The name of the event.
|
2011-03-17 14:27:42 +00:00
|
|
|
*/
|
|
|
|
private function sortListeners($eventName)
|
|
|
|
{
|
2011-05-30 15:00:29 +01:00
|
|
|
$this->sorted[$eventName] = array();
|
2011-05-30 07:57:53 +01:00
|
|
|
|
2011-05-30 15:00:29 +01:00
|
|
|
if (isset($this->listeners[$eventName])) {
|
|
|
|
krsort($this->listeners[$eventName]);
|
|
|
|
$this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]);
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2011-03-17 14:27:42 +00:00
|
|
|
}
|