2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony package.
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
2010-01-04 14:26:20 +00:00
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-04-07 02:07:59 +01:00
|
|
|
*
|
2010-01-04 14:26:20 +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\Component\EventDispatcher;
|
|
|
|
|
2010-01-04 14:26:20 +00:00
|
|
|
/**
|
|
|
|
* EventDispatcher implements a dispatcher object.
|
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
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
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $listeners = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connects a listener to a given event name.
|
|
|
|
*
|
2011-01-14 13:37:18 +00:00
|
|
|
* Listeners with a higher priority are executed first.
|
|
|
|
*
|
2010-05-06 12:25:53 +01:00
|
|
|
* @param string $name An event name
|
|
|
|
* @param mixed $listener A PHP callable
|
2010-08-26 13:08:42 +01:00
|
|
|
* @param integer $priority The priority (between -10 and 10 -- defaults to 0)
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-08-26 13:08:42 +01:00
|
|
|
public function connect($name, $listener, $priority = 0)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-08-26 13:08:42 +01:00
|
|
|
if (!isset($this->listeners[$name][$priority])) {
|
2010-10-13 06:43:21 +01:00
|
|
|
if (!isset($this->listeners[$name])) {
|
|
|
|
$this->listeners[$name] = array();
|
|
|
|
}
|
2010-08-26 13:08:42 +01:00
|
|
|
$this->listeners[$name][$priority] = array();
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-08-26 13:08:42 +01:00
|
|
|
$this->listeners[$name][$priority][] = $listener;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2010-12-03 17:47:54 +00:00
|
|
|
* Disconnects one, or all listeners for the given event name.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2011-01-25 13:23:23 +00:00
|
|
|
* @param string $name An event name
|
|
|
|
* @param mixed|null $listener The listener to remove, or null to remove all
|
2011-01-24 15:46:04 +00:00
|
|
|
*
|
2010-12-03 17:47:54 +00:00
|
|
|
* @return void
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-12-03 17:47:54 +00:00
|
|
|
public function disconnect($name, $listener = null)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-12-03 17:47:54 +00:00
|
|
|
if (!isset($this->listeners[$name])) {
|
2011-01-24 15:46:04 +00:00
|
|
|
return;
|
2010-12-03 17:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $listener) {
|
|
|
|
unset($this->listeners[$name]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->listeners[$name] as $priority => $callables) {
|
|
|
|
foreach ($callables as $i => $callable) {
|
|
|
|
if ($listener === $callable) {
|
|
|
|
unset($this->listeners[$name][$priority][$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies all listeners of a given event.
|
|
|
|
*
|
2011-01-25 13:23:23 +00:00
|
|
|
* @param EventInterface $event An EventInterface instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-01-25 13:23:23 +00:00
|
|
|
public function notify(EventInterface $event)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($this->getListeners($event->getName()) as $listener) {
|
2010-05-06 12:25:53 +01:00
|
|
|
call_user_func($listener, $event);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2011-01-26 06:50:06 +00:00
|
|
|
* Notifies all listeners of a given event until one processes the event.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2011-01-25 13:23:23 +00:00
|
|
|
* @param EventInterface $event An EventInterface instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2011-01-26 06:50:06 +00:00
|
|
|
* @return mixed The returned value of the listener that processed the event
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-01-25 13:23:23 +00:00
|
|
|
public function notifyUntil(EventInterface $event)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($this->getListeners($event->getName()) as $listener) {
|
2011-01-26 06:50:06 +00:00
|
|
|
$ret = call_user_func($listener, $event);
|
|
|
|
if ($event->isProcessed()) {
|
|
|
|
return $ret;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Filters a value by calling all listeners of a given event.
|
|
|
|
*
|
2011-01-25 13:23:23 +00:00
|
|
|
* @param EventInterface $event An EventInterface instance
|
|
|
|
* @param mixed $value The value to be filtered
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2011-01-26 06:50:06 +00:00
|
|
|
* @return mixed The filtered value
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-01-25 13:23:23 +00:00
|
|
|
public function filter(EventInterface $event, $value)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($this->getListeners($event->getName()) as $listener) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$value = call_user_func($listener, $event, $value);
|
|
|
|
}
|
|
|
|
|
2011-01-26 06:50:06 +00:00
|
|
|
return $value;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the given event name has some listeners.
|
|
|
|
*
|
2010-11-15 22:12:46 +00:00
|
|
|
* @param string $name The event name
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return Boolean true if some listeners are connected, false otherwise
|
|
|
|
*/
|
|
|
|
public function hasListeners($name)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-08-26 13:08:42 +01:00
|
|
|
return (Boolean) count($this->getListeners($name));
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns all listeners associated with a given event name.
|
|
|
|
*
|
2010-11-15 22:12:46 +00:00
|
|
|
* @param string $name The event name
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return array An array of listeners
|
|
|
|
*/
|
|
|
|
public function getListeners($name)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!isset($this->listeners[$name])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return array();
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-02-03 23:29:28 +00:00
|
|
|
krsort($this->listeners[$name]);
|
2010-08-26 13:08:42 +01:00
|
|
|
|
2011-02-03 23:29:28 +00:00
|
|
|
return call_user_func_array('array_merge', $this->listeners[$name]);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|