Commit f09b9144 authored by Igor Wiedler's avatar Igor Wiedler

support for before and after filters

parent d3810642
...@@ -29,7 +29,7 @@ use Symfony\Component\Routing\Matcher\UrlMatcher; ...@@ -29,7 +29,7 @@ use Symfony\Component\Routing\Matcher\UrlMatcher;
class Framework extends HttpKernel class Framework extends HttpKernel
{ {
protected $routes; protected $routes;
protected $errorHandlers = array(); protected $handlers = array('error' => array(), 'before' => array(), 'after' => array());
/** /**
* Constructor. * Constructor.
...@@ -50,7 +50,9 @@ class Framework extends HttpKernel ...@@ -50,7 +50,9 @@ class Framework extends HttpKernel
$dispatcher = new EventDispatcher(); $dispatcher = new EventDispatcher();
$dispatcher->connect('core.request', array($this, 'parseRequest')); $dispatcher->connect('core.request', array($this, 'parseRequest'));
$dispatcher->connect('core.request', array($this, 'runBeforeFilters'));
$dispatcher->connect('core.view', array($this, 'parseResponse')); $dispatcher->connect('core.view', array($this, 'parseResponse'));
$dispatcher->connect('core.view', array($this, 'runAfterFilters'));
$dispatcher->connect('core.exception', array($this, 'handleException')); $dispatcher->connect('core.exception', array($this, 'handleException'));
$resolver = new ControllerResolver(); $resolver = new ControllerResolver();
...@@ -156,6 +158,42 @@ class Framework extends HttpKernel ...@@ -156,6 +158,42 @@ class Framework extends HttpKernel
return $this; return $this;
} }
/**
* Register a before filter.
*
* Before filters are run before any route has been matched.
*
* This method is chainable.
*
* @param mixed $callback Before filter callback
*
* @return $this
*/
public function before($callback)
{
$this->handlers['before'][] = $callback;
return $this;
}
/**
* Register an after filter.
*
* After filters are run after the controller has been executed.
*
* This method is chainable.
*
* @param mixed $callback After filter callback
*
* @return $this
*/
public function after($callback)
{
$this->handlers['after'][] = $callback;
return $this;
}
/** /**
* Register an error handler. * Register an error handler.
* *
...@@ -175,7 +213,7 @@ class Framework extends HttpKernel ...@@ -175,7 +213,7 @@ class Framework extends HttpKernel
*/ */
public function error($callback) public function error($callback)
{ {
$this->errorHandlers[] = $callback; $this->handlers['error'][] = $callback;
return $this; return $this;
} }
...@@ -236,6 +274,20 @@ class Framework extends HttpKernel ...@@ -236,6 +274,20 @@ class Framework extends HttpKernel
$request->attributes->add($attributes); $request->attributes->add($attributes);
} }
/**
* Handler for core.request
*
* Runs before filters right after the request comes in.
*
* @see __construct()
*/
public function runBeforeFilters(Event $event)
{
foreach ($this->handlers['before'] as $beforeFilter) {
$beforeFilter();
}
}
/** /**
* Handler for core.view * Handler for core.view
* *
...@@ -253,6 +305,22 @@ class Framework extends HttpKernel ...@@ -253,6 +305,22 @@ class Framework extends HttpKernel
return $response; return $response;
} }
/**
* Handler for core.view
*
* Runs after filters.
*
* @see __construct()
*/
public function runAfterFilters(Event $event, $response)
{
foreach ($this->handlers['after'] as $afterFilter) {
$afterFilter();
}
return $response;
}
/** /**
* Handler for core.exception * Handler for core.exception
* *
...@@ -265,7 +333,7 @@ class Framework extends HttpKernel ...@@ -265,7 +333,7 @@ class Framework extends HttpKernel
{ {
$exception = $event->get('exception'); $exception = $event->get('exception');
$prevResult = null; $prevResult = null;
foreach ($this->errorHandlers as $callback) { foreach ($this->handlers['error'] as $callback) {
$result = $callback($exception); $result = $callback($exception);
if (null !== $result && !$prevResult) { if (null !== $result && !$prevResult) {
$response = $this->parseResponse($event, $result); $response = $this->parseResponse($event, $result);
......
<?php
namespace Silex\Tests;
use Silex\Framework;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.org>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* Error handler test cases.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.org>
*/
class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
{
public function testBeforeAndAfterFilter()
{
$i = 0;
$test = $this;
$framework = Framework::create();
$framework->before(function() use(&$i, $test) {
$test->assertEquals(0, $i);
$i++;
});
$framework->match('/foo', function() use(&$i, $test) {
$test->assertEquals(1, $i);
$i++;
});
$framework->after(function() use(&$i, $test) {
$test->assertEquals(2, $i);
$i++;
});
$request = Request::create('http://test.com/foo');
$framework->handle($request);
$test->assertEquals(3, $i);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment