2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\EventDispatcher;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony package.
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EventDispatcher implements a dispatcher object.
|
|
|
|
*
|
|
|
|
* @see http://developer.apple.com/documentation/Cocoa/Conceptual/Notifications/index.html Apple's Cocoa framework
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class EventDispatcher
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $listeners = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connects a listener to a given event name.
|
|
|
|
*
|
|
|
|
* @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])) {
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* Disconnects a listener for a given event name.
|
|
|
|
*
|
2010-09-01 10:10:31 +01:00
|
|
|
* @param string $name An event name
|
|
|
|
* @param mixed|null $listener A PHP callable or null to disconnect all listeners
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return mixed false if listener does not exist, null otherwise
|
|
|
|
*/
|
2010-09-01 10:10:31 +01:00
|
|
|
public function disconnect($name, $listener = null)
|
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 false;
|
|
|
|
}
|
|
|
|
|
2010-08-26 13:08:42 +01:00
|
|
|
foreach ($this->listeners[$name] as $priority => $callables) {
|
|
|
|
foreach ($callables as $i => $callable) {
|
|
|
|
if ($listener === $callable) {
|
|
|
|
unset($this->listeners[$name][$priority][$i]);
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies all listeners of a given event.
|
|
|
|
*
|
|
|
|
* @param Event $event A Event instance
|
|
|
|
*
|
|
|
|
* @return Event The Event instance
|
|
|
|
*/
|
|
|
|
public function notify(Event $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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $event;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Notifies all listeners of a given event until one returns a non null value.
|
|
|
|
*
|
|
|
|
* @param Event $event A Event instance
|
|
|
|
*
|
|
|
|
* @return Event The Event instance
|
|
|
|
*/
|
|
|
|
public function notifyUntil(Event $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-08 14:32:30 +01:00
|
|
|
if (call_user_func($listener, $event)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$event->setProcessed(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $event;
|
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.
|
|
|
|
*
|
|
|
|
* @param Event $event A Event instance
|
|
|
|
* @param mixed $value The value to be filtered
|
|
|
|
*
|
|
|
|
* @return Event The Event instance
|
|
|
|
*/
|
|
|
|
public function filter(Event $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);
|
|
|
|
}
|
|
|
|
|
|
|
|
$event->setReturnValue($value);
|
|
|
|
|
|
|
|
return $event;
|
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.
|
|
|
|
*
|
|
|
|
* @param string $name The event name
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @param string $name The event name
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
2010-08-26 13:08:42 +01:00
|
|
|
$listeners = array();
|
|
|
|
$all = $this->listeners[$name];
|
|
|
|
ksort($all);
|
|
|
|
foreach ($all as $l) {
|
|
|
|
$listeners = array_merge($listeners, $l);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $listeners;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|