Skip to content

Commit 2a61714

Browse files
author
vagrant
committed
[HttpKernel] added PostResponseEvent dispatching to HttpKernel
1 parent 915f440 commit 2a61714

File tree

5 files changed

+96
-2
lines changed

5 files changed

+96
-2
lines changed

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @api
3232
*/
33-
class HttpKernel implements HttpKernelInterface
33+
class HttpKernel implements HttpKernelInterface, TerminableInterface
3434
{
3535
private $dispatcher;
3636
private $resolver;
@@ -79,6 +79,19 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
7979
}
8080
}
8181

82+
/**
83+
* Terminates a request/response cycle.
84+
*
85+
* Should be called after sending the response and before shutting down the kernel.
86+
*
87+
* @api
88+
*/
89+
public function terminate()
90+
{
91+
$event = new PostResponseEvent($this);
92+
$this->dispatcher->dispatch(KernelEvents::TERMINATE, $event);
93+
}
94+
8295
/**
8396
* Handles a request to convert it to a response.
8497
*

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ public function boot()
143143
*/
144144
public function terminate()
145145
{
146+
if (false === $this->booted) {
147+
return;
148+
}
149+
150+
if ($this->getHttpKernel() instanceof TerminableInterface) {
151+
$this->getHttpKernel()->terminate();
152+
}
146153
}
147154

148155
/**

tests/Symfony/Tests/Component/HttpKernel/HttpCache/HttpCacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
3535
$kernel = new HttpCache($kernelMock, $storeMock);
3636
$kernel->terminate();
3737

38-
// does implement TerminableInterface
38+
// implements TerminableInterface
3939
$kernelMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Kernel')
4040
->disableOriginalConstructor()
4141
->setMethods(array('terminate', 'registerBundles', 'registerContainerConfiguration'))

tests/Symfony/Tests/Component/HttpKernel/HttpKernelTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,20 @@ public function testHandleWithAResponseListener()
164164
$this->assertEquals('foo', $kernel->handle(new Request())->getContent());
165165
}
166166

167+
public function testTerminate()
168+
{
169+
$dispatcher = new EventDispatcher();
170+
$kernel = new HttpKernel($dispatcher, $this->getResolver());
171+
$dispatcher->addListener(KernelEvents::TERMINATE, function ($event) use (&$called, &$capturedKernel) {
172+
$called = true;
173+
$capturedKernel = $event->getKernel();
174+
});
175+
176+
$kernel->terminate();
177+
$this->assertTrue($called);
178+
$this->assertEquals($kernel, $capturedKernel);
179+
}
180+
167181
protected function getResolver($controller = null)
168182
{
169183
if (null === $controller) {

tests/Symfony/Tests/Component/HttpKernel/KernelTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,66 @@ public function testInitializeBundleThrowsExceptionWhenABundleExtendsItself()
652652
$kernel->initializeBundles();
653653
}
654654

655+
public function testTerminateReturnsSilentlyIfKernelIsNotBooted()
656+
{
657+
$kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
658+
->disableOriginalConstructor()
659+
->setMethods(array('getHttpKernel'))
660+
->getMock();
661+
662+
$kernel->expects($this->never())
663+
->method('getHttpKernel');
664+
665+
$kernel->setIsBooted(false);
666+
$kernel->terminate();
667+
}
668+
669+
public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
670+
{
671+
// does not implement TerminableInterface
672+
$httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')
673+
->disableOriginalConstructor()
674+
->getMock();
675+
676+
$httpKernelMock
677+
->expects($this->never())
678+
->method('terminate');
679+
680+
$kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
681+
->disableOriginalConstructor()
682+
->setMethods(array('getHttpKernel'))
683+
->getMock();
684+
685+
$kernel->expects($this->once())
686+
->method('getHttpKernel')
687+
->will($this->returnValue($httpKernelMock));
688+
689+
$kernel->setIsBooted(true);
690+
$kernel->terminate();
691+
692+
// implements TerminableInterface
693+
$httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
694+
->disableOriginalConstructor()
695+
->setMethods(array('terminate'))
696+
->getMock();
697+
698+
$httpKernelMock
699+
->expects($this->once())
700+
->method('terminate');
701+
702+
$kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
703+
->disableOriginalConstructor()
704+
->setMethods(array('getHttpKernel'))
705+
->getMock();
706+
707+
$kernel->expects($this->exactly(2))
708+
->method('getHttpKernel')
709+
->will($this->returnValue($httpKernelMock));
710+
711+
$kernel->setIsBooted(true);
712+
$kernel->terminate();
713+
}
714+
655715
protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
656716
{
657717
$bundle = $this

0 commit comments

Comments
 (0)