This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php

148 lines
4.0 KiB
PHP
Raw Normal View History

2010-01-04 14:26:20 +00:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.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.
*/
namespace Symfony\Component\EventDispatcher;
2010-01-04 14:26:20 +00:00
/**
* EventDispatcher implements a dispatcher object.
*
* @author Fabien Potencier <fabien@symfony.com>
2010-01-04 14:26:20 +00:00
*/
class EventDispatcher implements EventDispatcherInterface
2010-01-04 14:26:20 +00:00
{
protected $listeners = array();
/**
* Connects a listener to a given event name.
*
* Listeners with a higher priority are executed first.
*
* @param string $name An event name
* @param mixed $listener A PHP callable
* @param integer $priority The priority (between -10 and 10 -- defaults to 0)
*/
public function connect($name, $listener, $priority = 0)
2010-01-04 14:26:20 +00:00
{
if (!isset($this->listeners[$name][$priority])) {
if (!isset($this->listeners[$name])) {
$this->listeners[$name] = array();
}
$this->listeners[$name][$priority] = array();
}
2010-01-04 14:26:20 +00:00
$this->listeners[$name][$priority][] = $listener;
2010-01-04 14:26:20 +00:00
}
/**
* Disconnects one, or all listeners for the given event name.
*
* @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
*
* @return void
*/
public function disconnect($name, $listener = null)
2010-01-04 14:26:20 +00:00
{
if (!isset($this->listeners[$name])) {
2011-01-24 15:46:04 +00:00
return;
}
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
}
/**
* Notifies all listeners of a given event.
*
* @param EventInterface $event An EventInterface instance
*/
public function notify(EventInterface $event)
2010-01-04 14:26:20 +00:00
{
foreach ($this->getListeners($event->getName()) as $listener) {
call_user_func($listener, $event);
}
2010-01-04 14:26:20 +00:00
}
/**
* Notifies all listeners of a given event until one processes the event.
*
* @param EventInterface $event An EventInterface instance
*
* @return mixed The returned value of the listener that processed the event
*/
public function notifyUntil(EventInterface $event)
2010-01-04 14:26:20 +00:00
{
foreach ($this->getListeners($event->getName()) as $listener) {
$ret = call_user_func($listener, $event);
if ($event->isProcessed()) {
return $ret;
}
}
2010-01-04 14:26:20 +00:00
}
/**
* Filters a value by calling all listeners of a given event.
*
* @param EventInterface $event An EventInterface instance
* @param mixed $value The value to be filtered
*
* @return mixed The filtered value
*/
public function filter(EventInterface $event, $value)
2010-01-04 14:26:20 +00:00
{
foreach ($this->getListeners($event->getName()) as $listener) {
$value = call_user_func($listener, $event, $value);
}
return $value;
2010-01-04 14:26:20 +00: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
{
return (Boolean) count($this->getListeners($name));
2010-01-04 14:26:20 +00: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
{
if (!isset($this->listeners[$name])) {
return array();
}
2010-01-04 14:26:20 +00:00
krsort($this->listeners[$name]);
return call_user_func_array('array_merge', $this->listeners[$name]);
}
2010-01-04 14:26:20 +00:00
}