Skip to content

Commit 915f440

Browse files
author
vagrant
committed
[HttpKernel] removed BC breaks, introduced new TerminableInterface
1 parent 7efe4bc commit 915f440

File tree

8 files changed

+91
-58
lines changed

8 files changed

+91
-58
lines changed

src/Symfony/Component/HttpKernel/Event/PostResponseEvent.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class PostResponseEvent extends Event
2323
{
2424
/**
2525
* The kernel in which this event was thrown
26-
* @var Symfony\Component\HttpKernel\HttpKernelInterface
26+
* @var HttpKernelInterface
2727
*/
2828
private $kernel;
2929

@@ -35,10 +35,10 @@ public function __construct(HttpKernelInterface $kernel)
3535
/**
3636
* Returns the kernel in which this event was thrown
3737
*
38-
* @return Symfony\Component\HttpKernel\HttpKernelInterface
38+
* @return HttpKernelInterface
3939
*/
4040
public function getKernel()
4141
{
4242
return $this->kernel;
4343
}
44-
}
44+
}

src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
namespace Symfony\Component\HttpKernel\HttpCache;
1717

1818
use Symfony\Component\HttpKernel\HttpKernelInterface;
19+
use Symfony\Component\HttpKernel\TerminableInterface;
1920
use Symfony\Component\HttpFoundation\Request;
2021
use Symfony\Component\HttpFoundation\Response;
2122

@@ -26,7 +27,7 @@
2627
*
2728
* @api
2829
*/
29-
class HttpCache implements HttpKernelInterface
30+
class HttpCache implements HttpKernelInterface, TerminableInterface
3031
{
3132
private $kernel;
3233
private $store;
@@ -216,15 +217,17 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
216217
}
217218

218219
/**
219-
* Terminates a request/response cycle
220+
* Terminates a request/response cycle.
220221
*
221-
* Should be called before shutdown, but after sending the response
222+
* Should be called after sending the response and before shutting down the kernel.
222223
*
223224
* @api
224225
*/
225226
public function terminate()
226227
{
227-
$this->kernel->terminate();
228+
if ($this->getKernel() instanceof TerminableInterface) {
229+
$this->getKernel()->terminate();
230+
}
228231
}
229232

230233
/**

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,6 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
7979
}
8080
}
8181

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

src/Symfony/Component/HttpKernel/HttpKernelInterface.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,4 @@ interface HttpKernelInterface
4444
* @api
4545
*/
4646
function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true);
47-
48-
/**
49-
* Terminates a request/response cycle
50-
*
51-
* Should be called after sending the response
52-
*/
53-
function terminate();
5447
}

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
*
4545
* @api
4646
*/
47-
abstract class Kernel implements KernelInterface
47+
abstract class Kernel implements KernelInterface, TerminableInterface
4848
{
4949
protected $bundles;
5050
protected $bundleMap;
@@ -134,6 +134,17 @@ public function boot()
134134
$this->booted = true;
135135
}
136136

137+
/**
138+
* Terminates a request/response cycle.
139+
*
140+
* Should be called after sending the response and before shutting down the kernel.
141+
*
142+
* @api
143+
*/
144+
public function terminate()
145+
{
146+
}
147+
137148
/**
138149
* Shutdowns the kernel.
139150
*
@@ -171,22 +182,6 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
171182
return $this->getHttpKernel()->handle($request, $type, $catch);
172183
}
173184

174-
/**
175-
* Terminates a request/response cycle
176-
*
177-
* Should be called before shutdown, but after sending the response
178-
*
179-
* @api
180-
*/
181-
public function terminate()
182-
{
183-
if (false === $this->booted) {
184-
throw new \LogicException('The kernel has been shutdown already');
185-
}
186-
187-
$this->getHttpKernel()->terminate();
188-
}
189-
190185
/**
191186
* Gets a http kernel from the container
192187
*
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
17+
/**
18+
* Terminable extends the Kernel request/response cycle with dispatching a post
19+
* response event after sending the response and before shutting down the kernel.
20+
*
21+
* @author Jordi Boggiano <[email protected]>
22+
* @author Pierre Minnieur <[email protected]>
23+
*
24+
* @api
25+
*/
26+
interface TerminableInterface
27+
{
28+
/**
29+
* Terminates a request/response cycle.
30+
*
31+
* Should be called after sending the response and before shutting down the kernel.
32+
*
33+
* @api
34+
*/
35+
function terminate();
36+
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,43 @@
1111

1212
namespace Symfony\Tests\Component\HttpKernel\HttpCache;
1313

14+
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
15+
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
16+
use Symfony\Component\HttpKernel\HttpKernelInterface;
1417
require_once __DIR__.'/HttpCacheTestCase.php';
1518

1619
class HttpCacheTest extends HttpCacheTestCase
1720
{
21+
public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
22+
{
23+
$storeMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\HttpCache\\StoreInterface')
24+
->disableOriginalConstructor()
25+
->getMock();
26+
27+
// does not implement TerminableInterface
28+
$kernelMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\HttpKernelInterface')
29+
->disableOriginalConstructor()
30+
->getMock();
31+
32+
$kernelMock->expects($this->never())
33+
->method('terminate');
34+
35+
$kernel = new HttpCache($kernelMock, $storeMock);
36+
$kernel->terminate();
37+
38+
// does implement TerminableInterface
39+
$kernelMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Kernel')
40+
->disableOriginalConstructor()
41+
->setMethods(array('terminate', 'registerBundles', 'registerContainerConfiguration'))
42+
->getMock();
43+
44+
$kernelMock->expects($this->once())
45+
->method('terminate');
46+
47+
$kernel = new HttpCache($kernelMock, $storeMock);
48+
$kernel->terminate();
49+
}
50+
1851
public function testPassesOnNonGetHeadRequests()
1952
{
2053
$this->setNextResponse(200);

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,6 @@ 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-
181167
protected function getResolver($controller = null)
182168
{
183169
if (null === $controller) {

0 commit comments

Comments
 (0)