diff options
author | Bernhard Schussek <bernhard.schussek@symfony-project.com> | 2011-03-05 15:30:34 +0100 |
---|---|---|
committer | Bernhard Schussek <bernhard.schussek@symfony-project.com> | 2011-03-05 15:30:34 +0100 |
commit | c0b58aaf0672541eb7215d4018201b0e21ff957d (patch) | |
tree | 7a76588be3608fe885ed1a5d45c95419f64166fe /Http/Firewall.php | |
parent | a45d4a21c023980a2d652234d7068a477a20f6e8 (diff) | |
download | symfony-security-c0b58aaf0672541eb7215d4018201b0e21ff957d.zip symfony-security-c0b58aaf0672541eb7215d4018201b0e21ff957d.tar.gz symfony-security-c0b58aaf0672541eb7215d4018201b0e21ff957d.tar.bz2 |
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(...));
Diffstat (limited to 'Http/Firewall.php')
-rw-r--r-- | Http/Firewall.php | 40 |
1 files changed, 19 insertions, 21 deletions
diff --git a/Http/Firewall.php b/Http/Firewall.php index f7fabbb..55d2b28 100644 --- a/Http/Firewall.php +++ b/Http/Firewall.php @@ -11,11 +11,11 @@ namespace Symfony\Component\Security\Http; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\EventInterface; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpKernel\Events; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; use Symfony\Component\HttpFoundation\Request; +use Doctrine\Common\EventManager; /** * Firewall uses a FirewallMap to register security listeners for the given @@ -25,14 +25,12 @@ use Symfony\Component\HttpFoundation\Request; * (a Basic authentication for the /api, and a web based authentication for * everything else for instance). * - * The handle method must be connected to the core.request event. - * * @author Fabien Potencier <fabien.potencier@symfony-project.com> */ class Firewall { protected $map; - protected $dispatcher; + protected $evm; protected $currentListeners; /** @@ -40,42 +38,42 @@ class Firewall * * @param FirewallMap $map A FirewallMap instance */ - public function __construct(FirewallMapInterface $map, EventDispatcherInterface $dispatcher) + public function __construct(FirewallMapInterface $map, EventManager $evm) { $this->map = $map; - $this->dispatcher = $dispatcher; + $this->evm = $evm; $this->currentListeners = array(); } /** * Handles security. * - * @param EventInterface $event An EventInterface instance + * @param RequestEventArgs $eventArgs An RequestEventArgs instance */ - public function handle(EventInterface $event) + public function onCoreRequest(RequestEventArgs $eventArgs) { - if (HttpKernelInterface::MASTER_REQUEST !== $event->get('request_type')) { + if (HttpKernelInterface::MASTER_REQUEST !== $eventArgs->getRequestType()) { return; } - $request = $event->get('request'); + $request = $eventArgs->getRequest(); - // disconnect all listeners from core.security to avoid the overhead + // disconnect all listeners from onCoreSecurity to avoid the overhead // of most listeners having to do this manually - $this->dispatcher->disconnect('core.security'); + $this->evm->removeEventListeners(Events::onCoreSecurity); // ensure that listeners disconnect from wherever they have connected to foreach ($this->currentListeners as $listener) { - $listener->unregister($this->dispatcher); + $listener->unregister($this->evm); } // register listeners for this firewall list($listeners, $exception) = $this->map->getListeners($request); if (null !== $exception) { - $exception->register($this->dispatcher); + $exception->register($this->evm); } foreach ($listeners as $listener) { - $listener->register($this->dispatcher); + $listener->register($this->evm); } // save current listener instances @@ -85,11 +83,11 @@ class Firewall } // initiate the listener chain - $ret = $this->dispatcher->notifyUntil($securityEvent = new Event($request, 'core.security', array('request' => $request))); - if ($securityEvent->isProcessed()) { - $event->setProcessed(); + $securityEventArgs = new RequestEventArgs($eventArgs->getKernel(), $request, $eventArgs->getRequestType()); + $this->evm->dispatchEvent($securityEventArgs); - return $ret; + if ($securityEventArgs->hasResponse()) { + $eventArgs->setResponse($securityEventArgs->getResponse()); } } } |