[HttpKernel] Add Kernel::terminate() and HttpKernel::terminate() for post-response logic

This commit is contained in:
Jordi Boggiano 2011-12-06 10:00:36 +01:00
parent 7e2ca4ad93
commit 7efe4bcb87
8 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,44 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Event;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\EventDispatcher\Event;
/**
* Allows to execute logic after a response was sent
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class PostResponseEvent extends Event
{
/**
* The kernel in which this event was thrown
* @var Symfony\Component\HttpKernel\HttpKernelInterface
*/
private $kernel;
public function __construct(HttpKernelInterface $kernel)
{
$this->kernel = $kernel;
}
/**
* Returns the kernel in which this event was thrown
*
* @return Symfony\Component\HttpKernel\HttpKernelInterface
*/
public function getKernel()
{
return $this->kernel;
}
}

View File

@ -215,6 +215,18 @@ class HttpCache implements HttpKernelInterface
return $response;
}
/**
* Terminates a request/response cycle
*
* Should be called before shutdown, but after sending the response
*
* @api
*/
public function terminate()
{
$this->kernel->terminate();
}
/**
* Forwards the Request to the backend without storing the Response in the cache.
*

View File

@ -18,6 +18,7 @@ use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@ -78,6 +79,19 @@ class HttpKernel implements HttpKernelInterface
}
}
/**
* Terminates a request/response cycle
*
* Should be called before shutdown, but after sending the response
*
* @api
*/
public function terminate()
{
$event = new PostResponseEvent($this);
$this->dispatcher->dispatch(KernelEvents::TERMINATE, $event);
}
/**
* Handles a request to convert it to a response.
*

View File

@ -44,4 +44,11 @@ interface HttpKernelInterface
* @api
*/
function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true);
/**
* Terminates a request/response cycle
*
* Should be called after sending the response
*/
function terminate();
}

View File

@ -171,6 +171,22 @@ abstract class Kernel implements KernelInterface
return $this->getHttpKernel()->handle($request, $type, $catch);
}
/**
* Terminates a request/response cycle
*
* Should be called before shutdown, but after sending the response
*
* @api
*/
public function terminate()
{
if (false === $this->booted) {
throw new \LogicException('The kernel has been shutdown already');
}
$this->getHttpKernel()->terminate();
}
/**
* Gets a http kernel from the container
*

View File

@ -91,4 +91,15 @@ final class KernelEvents
* @api
*/
const RESPONSE = 'kernel.response';
/**
* The TERMINATE event occurs once a reponse was sent
*
* This event allows you to run expensive post-response jobs.
* The event listener method receives a
* Symfony\Component\HttpKernel\Event\PostResponseEvent instance.
*
* @var string
*/
const TERMINATE = 'kernel.terminate';
}

View File

@ -114,6 +114,7 @@ class TestKernel implements HttpKernelInterface
return new Response('foo');
}
public function terminate() {}
}
class TestKernelThatThrowsException implements HttpKernelInterface
@ -122,4 +123,6 @@ class TestKernelThatThrowsException implements HttpKernelInterface
{
throw new \Exception('bar');
}
public function terminate() {}
}

View File

@ -164,6 +164,20 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('foo', $kernel->handle(new Request())->getContent());
}
public function testTerminate()
{
$dispatcher = new EventDispatcher();
$kernel = new HttpKernel($dispatcher, $this->getResolver());
$dispatcher->addListener(KernelEvents::TERMINATE, function ($event) use (&$called, &$capturedKernel) {
$called = true;
$capturedKernel = $event->getKernel();
});
$kernel->terminate();
$this->assertTrue($called);
$this->assertEquals($kernel, $capturedKernel);
}
protected function getResolver($controller = null)
{
if (null === $controller) {