Skip to content

[HttpKernel] Add Kernel::terminate() and HttpKernel::terminate() for post-response logic (without the BC break) #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 6, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Symfony/Component/HttpKernel/Event/PostResponseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -35,10 +35,10 @@ public function __construct(HttpKernelInterface $kernel)
/**
* Returns the kernel in which this event was thrown
*
* @return Symfony\Component\HttpKernel\HttpKernelInterface
* @return HttpKernelInterface
*/
public function getKernel()
{
return $this->kernel;
}
}
}
11 changes: 6 additions & 5 deletions src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,7 +27,7 @@
*
* @api
*/
class HttpCache implements HttpKernelInterface
class HttpCache implements HttpKernelInterface, TerminableInterface
{
private $kernel;
private $store;
Expand Down Expand Up @@ -216,15 +217,15 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
}

/**
* Terminates a request/response cycle
*
* Should be called before shutdown, but after sending the response
* {@inheritdoc}
*
* @api
*/
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be {@inheritdoc} instead of copying it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 9f4391f

public function terminate()
{
$this->kernel->terminate();
if ($this->getKernel() instanceof TerminableInterface) {
$this->getKernel()->terminate();
}
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Symfony/Component/HttpKernel/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* @api
*/
class HttpKernel implements HttpKernelInterface
class HttpKernel implements HttpKernelInterface, TerminableInterface
{
private $dispatcher;
private $resolver;
Expand Down Expand Up @@ -80,9 +80,7 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
}

/**
* Terminates a request/response cycle
*
* Should be called before shutdown, but after sending the response
* {@inheritdoc}
*
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be {@inheritdoc} instead of copying it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 9f4391f

* @api
*/
Expand Down
7 changes: 0 additions & 7 deletions src/Symfony/Component/HttpKernel/HttpKernelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
34 changes: 17 additions & 17 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
*
* @api
*/
abstract class Kernel implements KernelInterface
abstract class Kernel implements KernelInterface, TerminableInterface
{
protected $bundles;
protected $bundleMap;
Expand Down Expand Up @@ -134,6 +134,22 @@ public function boot()
$this->booted = true;
}

/**
* {@inheritdoc}
*
* @api
*/
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be {@inheritdoc} instead of copying it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 9f4391f

public function terminate()
{
if (false === $this->booted) {
return;
}

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

/**
* Shutdowns the kernel.
*
Expand Down Expand Up @@ -171,22 +187,6 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
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
*
Expand Down
36 changes: 36 additions & 0 deletions src/Symfony/Component/HttpKernel/TerminableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 <[email protected]>
* @author Pierre Minnieur <[email protected]>
*
* @api
*/
interface TerminableInterface
{
/**
* Terminates a request/response cycle.
*
* Should be called after sending the response and before shutting down the kernel.
*
* @api
*/
function terminate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

// implements 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);
Expand Down
60 changes: 60 additions & 0 deletions tests/Symfony/Tests/Component/HttpKernel/KernelTest.php
Original file line number Diff line number Diff line change
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