updated eventdispatcher

This commit is contained in:
kremsy
2015-01-19 11:11:45 +01:00
parent 0ca7d2dd15
commit f97109a75b
18 changed files with 123 additions and 426 deletions

View File

@ -15,7 +15,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Lazily loads listeners and subscribers from the dependency injection
* container
* container.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
@ -24,19 +24,22 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class ContainerAwareEventDispatcher extends EventDispatcher
{
/**
* The container from where services are loaded
* The container from where services are loaded.
*
* @var ContainerInterface
*/
private $container;
/**
* The service IDs of the event listeners and subscribers
* The service IDs of the event listeners and subscribers.
*
* @var array
*/
private $listenerIds = array();
/**
* The services registered as listeners
* The services registered as listeners.
*
* @var array
*/
private $listeners = array();
@ -52,14 +55,14 @@ class ContainerAwareEventDispatcher extends EventDispatcher
}
/**
* Adds a service as event listener
* Adds a service as event listener.
*
* @param string $eventName Event for which the listener is added
* @param array $callback The service ID of the listener service & the method
* name that has to be called
* @param integer $priority The higher this value, the earlier an event listener
* will be triggered in the chain.
* Defaults to 0.
* name that has to be called
* @param int $priority The higher this value, the earlier an event listener
* will be triggered in the chain.
* Defaults to 0.
*
* @throws \InvalidArgumentException
*/
@ -76,21 +79,18 @@ class ContainerAwareEventDispatcher extends EventDispatcher
{
$this->lazyLoad($eventName);
if (isset($this->listeners[$eventName])) {
foreach ($this->listeners[$eventName] as $key => $l) {
foreach ($this->listenerIds[$eventName] as $i => $args) {
list($serviceId, $method, $priority) = $args;
if ($key === $serviceId.'.'.$method) {
if ($listener === array($l, $method)) {
unset($this->listeners[$eventName][$key]);
if (empty($this->listeners[$eventName])) {
unset($this->listeners[$eventName]);
}
unset($this->listenerIds[$eventName][$i]);
if (empty($this->listenerIds[$eventName])) {
unset($this->listenerIds[$eventName]);
}
}
if (isset($this->listenerIds[$eventName])) {
foreach ($this->listenerIds[$eventName] as $i => $args) {
list($serviceId, $method, $priority) = $args;
$key = $serviceId.'.'.$method;
if (isset($this->listeners[$eventName][$key]) && $listener === array($this->listeners[$eventName][$key], $method)) {
unset($this->listeners[$eventName][$key]);
if (empty($this->listeners[$eventName])) {
unset($this->listeners[$eventName]);
}
unset($this->listenerIds[$eventName][$i]);
if (empty($this->listenerIds[$eventName])) {
unset($this->listenerIds[$eventName]);
}
}
}
@ -100,12 +100,12 @@ class ContainerAwareEventDispatcher extends EventDispatcher
}
/**
* @see EventDispatcherInterface::hasListeners
* @see EventDispatcherInterface::hasListeners()
*/
public function hasListeners($eventName = null)
{
if (null === $eventName) {
return (Boolean) count($this->listenerIds) || (Boolean) count($this->listeners);
return (bool) count($this->listenerIds) || (bool) count($this->listeners);
}
if (isset($this->listenerIds[$eventName])) {
@ -116,7 +116,7 @@ class ContainerAwareEventDispatcher extends EventDispatcher
}
/**
* @see EventDispatcherInterface::getListeners
* @see EventDispatcherInterface::getListeners()
*/
public function getListeners($eventName = null)
{
@ -132,7 +132,7 @@ class ContainerAwareEventDispatcher extends EventDispatcher
}
/**
* Adds a service as event subscriber
* Adds a service as event subscriber.
*
* @param string $serviceId The service ID of the subscriber service
* @param string $class The service's class name (which must implement EventSubscriberInterface)
@ -153,7 +153,7 @@ class ContainerAwareEventDispatcher extends EventDispatcher
}
/**
* {@inheritDoc}
* {@inheritdoc}
*
* Lazily loads listeners for this event from the dependency injection
* container.