Skip to content

Commit

Permalink
[HttpKernel] added PostResponseEvent dispatching to HttpKernel
Browse files Browse the repository at this point in the history
  • Loading branch information
vagrant committed Dec 6, 2011
1 parent 915f440 commit 2a61714
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/Symfony/Component/HttpKernel/HttpKernel.php
Expand Up @@ -30,7 +30,7 @@
*
* @api
*/
class HttpKernel implements HttpKernelInterface
class HttpKernel implements HttpKernelInterface, TerminableInterface
{
private $dispatcher;
private $resolver;
Expand Down Expand Up @@ -79,6 +79,19 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
}
}

/**
* 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.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -143,6 +143,13 @@ public function boot()
*/
public function terminate()
{
if (false === $this->booted) {
return;
}

if ($this->getHttpKernel() instanceof TerminableInterface) {
$this->getHttpKernel()->terminate();
}
}

/**
Expand Down
Expand Up @@ -35,7 +35,7 @@ public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
$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'))
Expand Down
14 changes: 14 additions & 0 deletions tests/Symfony/Tests/Component/HttpKernel/HttpKernelTest.php
Expand Up @@ -164,6 +164,20 @@ public function testHandleWithAResponseListener()
$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) {
Expand Down
60 changes: 60 additions & 0 deletions tests/Symfony/Tests/Component/HttpKernel/KernelTest.php
Expand Up @@ -652,6 +652,66 @@ public function testInitializeBundleThrowsExceptionWhenABundleExtendsItself()
$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
Expand Down

0 comments on commit 2a61714

Please sign in to comment.