[HttpKernel] removed BC breaks, introduced new TerminableInterface

This commit is contained in:
vagrant 2011-12-06 10:41:41 -08:00
parent 7efe4bcb87
commit 915f440cad
8 changed files with 91 additions and 58 deletions

View File

@ -23,7 +23,7 @@ class PostResponseEvent extends Event
{
/**
* The kernel in which this event was thrown
* @var Symfony\Component\HttpKernel\HttpKernelInterface
* @var HttpKernelInterface
*/
private $kernel;
@ -35,10 +35,10 @@ class PostResponseEvent extends Event
/**
* Returns the kernel in which this event was thrown
*
* @return Symfony\Component\HttpKernel\HttpKernelInterface
* @return HttpKernelInterface
*/
public function getKernel()
{
return $this->kernel;
}
}
}

View File

@ -16,6 +16,7 @@
namespace Symfony\Component\HttpKernel\HttpCache;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@ -26,7 +27,7 @@ use Symfony\Component\HttpFoundation\Response;
*
* @api
*/
class HttpCache implements HttpKernelInterface
class HttpCache implements HttpKernelInterface, TerminableInterface
{
private $kernel;
private $store;
@ -216,15 +217,17 @@ class HttpCache implements HttpKernelInterface
}
/**
* Terminates a request/response cycle
* Terminates a request/response cycle.
*
* Should be called before shutdown, but after sending the response
* Should be called after sending the response and before shutting down the kernel.
*
* @api
*/
public function terminate()
{
$this->kernel->terminate();
if ($this->getKernel() instanceof TerminableInterface) {
$this->getKernel()->terminate();
}
}
/**

View File

@ -79,19 +79,6 @@ 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,11 +44,4 @@ 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

@ -44,7 +44,7 @@ use Symfony\Component\ClassLoader\DebugUniversalClassLoader;
*
* @api
*/
abstract class Kernel implements KernelInterface
abstract class Kernel implements KernelInterface, TerminableInterface
{
protected $bundles;
protected $bundleMap;
@ -134,6 +134,17 @@ abstract class Kernel implements KernelInterface
$this->booted = true;
}
/**
* Terminates a request/response cycle.
*
* Should be called after sending the response and before shutting down the kernel.
*
* @api
*/
public function terminate()
{
}
/**
* Shutdowns the kernel.
*
@ -171,22 +182,6 @@ 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

@ -0,0 +1,36 @@
<?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;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Terminable extends the Kernel request/response cycle with dispatching a post
* response event after sending the response and before shutting down the kernel.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Pierre Minnieur <pierre.minnieur@sensiolabs.de>
*
* @api
*/
interface TerminableInterface
{
/**
* Terminates a request/response cycle.
*
* Should be called after sending the response and before shutting down the kernel.
*
* @api
*/
function terminate();
}

View File

@ -11,10 +11,43 @@
namespace Symfony\Tests\Component\HttpKernel\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;
require_once __DIR__.'/HttpCacheTestCase.php';
class HttpCacheTest extends HttpCacheTestCase
{
public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
{
$storeMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\HttpCache\\StoreInterface')
->disableOriginalConstructor()
->getMock();
// does not implement TerminableInterface
$kernelMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\HttpKernelInterface')
->disableOriginalConstructor()
->getMock();
$kernelMock->expects($this->never())
->method('terminate');
$kernel = new HttpCache($kernelMock, $storeMock);
$kernel->terminate();
// does implement TerminableInterface
$kernelMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Kernel')
->disableOriginalConstructor()
->setMethods(array('terminate', 'registerBundles', 'registerContainerConfiguration'))
->getMock();
$kernelMock->expects($this->once())
->method('terminate');
$kernel = new HttpCache($kernelMock, $storeMock);
$kernel->terminate();
}
public function testPassesOnNonGetHeadRequests()
{
$this->setNextResponse(200);

View File

@ -164,20 +164,6 @@ 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) {