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/HttpKernel/Security/Firewall.php
Fabien Potencier 1c11d81611 made all event listeners lazy loaded
* The register() method on all listeners has been removed
 * Instead, the information is now put directly in the DIC tag

For instance, a listener on core.request had this method:

   public function register(EventDispatcher $dispatcher, $priority = 0)
   {
       $dispatcher->connect('core.response', array($this, 'filter'), $priority);
   }

And this tag in the DIC configuration:

  <tag name="kernel.listener" />

Now, it only has the following configuration:

  <tag name="kernel.listener" event="core.response" method="filter" priority="0" />

The event and method attributes are now mandatory.
2011-01-23 18:07:05 +01:00

97 lines
2.8 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Security;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Firewall uses a FirewallMap to register security listeners for the given
* request.
*
* It allows for different security strategies within the same application
* (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 $currentListeners;
/**
* Constructor.
*
* @param FirewallMap $map A FirewallMap instance
*/
public function __construct(FirewallMapInterface $map, EventDispatcher $dispatcher)
{
$this->map = $map;
$this->dispatcher = $dispatcher;
$this->currentListeners = array();
}
/**
* Handles security.
*
* @param Event $event An Event instance
*/
public function handle(Event $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->get('request_type')) {
return;
}
$request = $event->get('request');
// disconnect all listeners from core.security to avoid the overhead
// of most listeners having to do this manually
$this->dispatcher->disconnect('core.security');
// ensure that listeners disconnect from wherever they have connected to
foreach ($this->currentListeners as $listener) {
$listener->unregister($this->dispatcher);
}
// register listeners for this firewall
list($listeners, $exception) = $this->map->getListeners($request);
if (null !== $exception) {
$exception->register($this->dispatcher);
}
foreach ($listeners as $listener) {
$listener->register($this->dispatcher);
}
// save current listener instances
$this->currentListeners = $listeners;
if (null !== $exception) {
$this->currentListeners[] = $exception;
}
// initiate the listener chain
$e = $this->dispatcher->notifyUntil(new Event($request, 'core.security', array('request' => $request)));
if ($e->isProcessed()) {
$event->setReturnValue($e->getReturnValue());
return true;
}
return;
}
}