[HttpKernel] added PostResponseEvent dispatching to HttpKernel
This commit is contained in:
parent
915f440cad
commit
2a61714df6
@ -30,7 +30,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class HttpKernel implements HttpKernelInterface
|
||||
class HttpKernel implements HttpKernelInterface, TerminableInterface
|
||||
{
|
||||
private $dispatcher;
|
||||
private $resolver;
|
||||
@ -79,6 +79,19 @@ class HttpKernel implements HttpKernelInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Terminates a request/response cycle.
|
||||
*
|
||||
* Should be called after sending the response and before shutting down the kernel.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function terminate()
|
||||
{
|
||||
$event = new PostResponseEvent($this);
|
||||
$this->dispatcher->dispatch(KernelEvents::TERMINATE, $event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a request to convert it to a response.
|
||||
*
|
||||
|
@ -143,6 +143,13 @@ abstract class Kernel implements KernelInterface, TerminableInterface
|
||||
*/
|
||||
public function terminate()
|
||||
{
|
||||
if (false === $this->booted) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->getHttpKernel() instanceof TerminableInterface) {
|
||||
$this->getHttpKernel()->terminate();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -35,7 +35,7 @@ class HttpCacheTest extends HttpCacheTestCase
|
||||
$kernel = new HttpCache($kernelMock, $storeMock);
|
||||
$kernel->terminate();
|
||||
|
||||
// does implement TerminableInterface
|
||||
// implements TerminableInterface
|
||||
$kernelMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Kernel')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('terminate', 'registerBundles', 'registerContainerConfiguration'))
|
||||
|
@ -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) {
|
||||
|
@ -652,6 +652,66 @@ EOF;
|
||||
$kernel->initializeBundles();
|
||||
}
|
||||
|
||||
public function testTerminateReturnsSilentlyIfKernelIsNotBooted()
|
||||
{
|
||||
$kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('getHttpKernel'))
|
||||
->getMock();
|
||||
|
||||
$kernel->expects($this->never())
|
||||
->method('getHttpKernel');
|
||||
|
||||
$kernel->setIsBooted(false);
|
||||
$kernel->terminate();
|
||||
}
|
||||
|
||||
public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
|
||||
{
|
||||
// does not implement TerminableInterface
|
||||
$httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$httpKernelMock
|
||||
->expects($this->never())
|
||||
->method('terminate');
|
||||
|
||||
$kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('getHttpKernel'))
|
||||
->getMock();
|
||||
|
||||
$kernel->expects($this->once())
|
||||
->method('getHttpKernel')
|
||||
->will($this->returnValue($httpKernelMock));
|
||||
|
||||
$kernel->setIsBooted(true);
|
||||
$kernel->terminate();
|
||||
|
||||
// implements TerminableInterface
|
||||
$httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('terminate'))
|
||||
->getMock();
|
||||
|
||||
$httpKernelMock
|
||||
->expects($this->once())
|
||||
->method('terminate');
|
||||
|
||||
$kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('getHttpKernel'))
|
||||
->getMock();
|
||||
|
||||
$kernel->expects($this->exactly(2))
|
||||
->method('getHttpKernel')
|
||||
->will($this->returnValue($httpKernelMock));
|
||||
|
||||
$kernel->setIsBooted(true);
|
||||
$kernel->terminate();
|
||||
}
|
||||
|
||||
protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
|
||||
{
|
||||
$bundle = $this
|
||||
|
Reference in New Issue
Block a user