2010-02-17 13:54:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-02-17 13:54:36 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:54:36 +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\Bundle\FrameworkBundle;
|
|
|
|
|
2011-01-23 17:02:16 +00:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
Replaced EventDispatcher by Doctrine's EventManager implementation
Doctrine's EventManager implementation has several advantages over the
EventDispatcher implementation of Symfony2. Therefore I suggest that we
use their implementation.
Advantages:
* Event Listeners are objects, not callbacks. These objects have handler
methods that have the same name as the event. This helps a lot when
reading the code and makes the code for adding an event listener shorter.
* You can create Event Subscribers, which are event listeners with an
additional getSubscribedEvents() method. The benefit here is that the
code that registers the subscriber doesn't need to know about its
implementation.
* All events are defined in static Events classes, so users of IDEs benefit
of code completion
* The communication between the dispatching class of an event and all
listeners is done through a subclass of EventArgs. This subclass can be
tailored to the type of event. A constructor, setters and getters can be
implemented that verify the validity of the data set into the object.
See examples below.
* Because each event type corresponds to an EventArgs implementation,
developers of event listeners can look up the available EventArgs methods
and benefit of code completion.
* EventArgs::stopPropagation() is more flexible and (IMO) clearer to use
than notifyUntil(). Also, it is a concept that is also used in other
event implementations
Before:
class EventListener
{
public function handle(EventInterface $event, $data) { ... }
}
$dispatcher->connect('core.request', array($listener, 'handle'));
$dispatcher->notify('core.request', new Event(...));
After (with listeners):
final class Events
{
const onCoreRequest = 'onCoreRequest';
}
class EventListener
{
public function onCoreRequest(RequestEventArgs $eventArgs) { ... }
}
$evm->addEventListener(Events::onCoreRequest, $listener);
$evm->dispatchEvent(Events::onCoreRequest, new RequestEventArgs(...));
After (with subscribers):
class EventSubscriber
{
public function onCoreRequest(RequestEventArgs $eventArgs) { ... }
public function getSubscribedEvents()
{
return Events::onCoreRequest;
}
}
$evm->addEventSubscriber($subscriber);
$evm->dispatchEvent(Events::onCoreRequest, new RequestEventArgs(...));
2011-03-05 14:30:34 +00:00
|
|
|
use Doctrine\Common\EventManager;
|
2011-03-07 14:42:52 +00:00
|
|
|
use Doctrine\Common\EventArgs;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2010-02-17 13:54:36 +00:00
|
|
|
/**
|
2011-03-07 14:42:52 +00:00
|
|
|
* Lazily loads listeners and subscribers from the dependency injection
|
|
|
|
* container
|
2010-02-17 13:54:36 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-03-07 14:42:52 +00:00
|
|
|
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
|
2010-02-17 13:54:36 +00:00
|
|
|
*/
|
Replaced EventDispatcher by Doctrine's EventManager implementation
Doctrine's EventManager implementation has several advantages over the
EventDispatcher implementation of Symfony2. Therefore I suggest that we
use their implementation.
Advantages:
* Event Listeners are objects, not callbacks. These objects have handler
methods that have the same name as the event. This helps a lot when
reading the code and makes the code for adding an event listener shorter.
* You can create Event Subscribers, which are event listeners with an
additional getSubscribedEvents() method. The benefit here is that the
code that registers the subscriber doesn't need to know about its
implementation.
* All events are defined in static Events classes, so users of IDEs benefit
of code completion
* The communication between the dispatching class of an event and all
listeners is done through a subclass of EventArgs. This subclass can be
tailored to the type of event. A constructor, setters and getters can be
implemented that verify the validity of the data set into the object.
See examples below.
* Because each event type corresponds to an EventArgs implementation,
developers of event listeners can look up the available EventArgs methods
and benefit of code completion.
* EventArgs::stopPropagation() is more flexible and (IMO) clearer to use
than notifyUntil(). Also, it is a concept that is also used in other
event implementations
Before:
class EventListener
{
public function handle(EventInterface $event, $data) { ... }
}
$dispatcher->connect('core.request', array($listener, 'handle'));
$dispatcher->notify('core.request', new Event(...));
After (with listeners):
final class Events
{
const onCoreRequest = 'onCoreRequest';
}
class EventListener
{
public function onCoreRequest(RequestEventArgs $eventArgs) { ... }
}
$evm->addEventListener(Events::onCoreRequest, $listener);
$evm->dispatchEvent(Events::onCoreRequest, new RequestEventArgs(...));
After (with subscribers):
class EventSubscriber
{
public function onCoreRequest(RequestEventArgs $eventArgs) { ... }
public function getSubscribedEvents()
{
return Events::onCoreRequest;
}
}
$evm->addEventSubscriber($subscriber);
$evm->dispatchEvent(Events::onCoreRequest, new RequestEventArgs(...));
2011-03-05 14:30:34 +00:00
|
|
|
class ContainerAwareEventManager extends EventManager
|
2010-02-17 13:54:36 +00:00
|
|
|
{
|
2011-03-07 14:42:52 +00:00
|
|
|
/**
|
|
|
|
* The container from where services are loaded
|
|
|
|
* @var ContainerInterface
|
|
|
|
*/
|
|
|
|
private $container;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The service IDs of the event listeners and subscribers
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $listenerIds;
|
2011-01-23 17:02:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container A ContainerInterface instance
|
|
|
|
*/
|
|
|
|
public function __construct(ContainerInterface $container)
|
|
|
|
{
|
|
|
|
$this->container = $container;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-07 14:42:52 +00:00
|
|
|
* Adds a service as event listener
|
|
|
|
*
|
|
|
|
* @param string|array $events One or more events for which the listener
|
|
|
|
* is added
|
|
|
|
* @param string $serviceId The ID of the listener service
|
|
|
|
* @param integer $priority The higher this value, the earlier an event
|
|
|
|
* listener will be triggered in the chain.
|
|
|
|
* Defaults to 0.
|
2011-01-23 17:02:16 +00:00
|
|
|
*/
|
2011-03-07 14:42:52 +00:00
|
|
|
public function addEventListenerService($events, $serviceId, $priority = 0)
|
2010-02-17 13:54:36 +00:00
|
|
|
{
|
2011-03-07 14:42:52 +00:00
|
|
|
if (!is_string($serviceId)) {
|
|
|
|
throw new \InvalidArgumentException('Expected a string argument');
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ((array)$events as $event) {
|
|
|
|
// Prevent duplicate entries
|
|
|
|
$this->listenerIds[$event][$serviceId] = $priority;
|
2011-01-24 15:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-07 14:42:52 +00:00
|
|
|
* Adds a service as event subscriber
|
|
|
|
*
|
|
|
|
* @param string $serviceId The ID of the subscriber service
|
|
|
|
* @param integer $priority The higher this value, the earlier an event
|
|
|
|
* listener will be triggered in the chain.
|
|
|
|
* Defaults to 0.
|
2011-01-24 15:46:04 +00:00
|
|
|
*/
|
2011-03-07 14:42:52 +00:00
|
|
|
public function addEventSuscriberService($serviceId, $priority = 0)
|
2011-01-24 15:46:04 +00:00
|
|
|
{
|
2011-03-07 14:42:52 +00:00
|
|
|
if (!is_string($serviceId)) {
|
|
|
|
throw new \InvalidArgumentException('Expected a string argument');
|
2011-01-23 17:02:16 +00:00
|
|
|
}
|
2011-03-07 14:42:52 +00:00
|
|
|
|
|
|
|
// TODO get class name, call static method getSubscribedEvents()
|
|
|
|
// and pass to addEventListenerService
|
2011-01-24 15:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-07 14:42:52 +00:00
|
|
|
* {@inheritDoc}
|
|
|
|
*
|
|
|
|
* Lazily loads listeners for this event from the dependency injection
|
|
|
|
* container.
|
2011-01-24 15:46:04 +00:00
|
|
|
*/
|
2011-03-07 14:42:52 +00:00
|
|
|
public function dispatchEvent($eventName, EventArgs $eventArgs = null)
|
2011-01-24 15:46:04 +00:00
|
|
|
{
|
2011-03-07 14:42:52 +00:00
|
|
|
if (isset($this->listenerIds[$eventName])) {
|
|
|
|
foreach ($this->listenerIds[$eventName] as $serviceId => $priority) {
|
|
|
|
$this->addEventListener($eventName, $this->container->get($serviceId), $priority);
|
2011-01-05 11:13:27 +00:00
|
|
|
}
|
2010-02-17 13:54:36 +00:00
|
|
|
}
|
2011-01-23 17:02:16 +00:00
|
|
|
|
2011-03-07 14:42:52 +00:00
|
|
|
parent::dispatchEvent($eventName, $eventArgs);
|
2010-02-17 13:54:36 +00:00
|
|
|
}
|
|
|
|
}
|