moved the container aware HTTP kernel to the HttpKernel component
This commit is contained in:
parent
2eea7682e7
commit
f17f5867a8
@ -11,52 +11,20 @@
|
|||||||
|
|
||||||
namespace Symfony\Bundle\FrameworkBundle;
|
namespace Symfony\Bundle\FrameworkBundle;
|
||||||
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
||||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||||
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
|
use Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel;
|
||||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
||||||
use Symfony\Component\HttpKernel\HttpKernel as BaseHttpKernel;
|
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This HttpKernel is used to manage scope changes of the DI container.
|
* This HttpKernel is used to manage scope changes of the DI container.
|
||||||
*
|
*
|
||||||
* @author Fabien Potencier <fabien@symfony.com>
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||||
|
*
|
||||||
|
* @deprecated This class is deprecated in 2.2 and will be removed in 2.3
|
||||||
*/
|
*/
|
||||||
class HttpKernel extends BaseHttpKernel
|
class HttpKernel extends ContainerAwareHttpKernel
|
||||||
{
|
{
|
||||||
protected $container;
|
|
||||||
|
|
||||||
public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver)
|
|
||||||
{
|
|
||||||
parent::__construct($dispatcher, $controllerResolver);
|
|
||||||
|
|
||||||
$this->container = $container;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
|
|
||||||
{
|
|
||||||
$request->headers->set('X-Php-Ob-Level', ob_get_level());
|
|
||||||
|
|
||||||
$this->container->enterScope('request');
|
|
||||||
$this->container->set('request', $request, 'request');
|
|
||||||
|
|
||||||
try {
|
|
||||||
$response = parent::handle($request, $type, $catch);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
$this->container->leaveScope('request');
|
|
||||||
|
|
||||||
throw $e;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->container->leaveScope('request');
|
|
||||||
|
|
||||||
return $response;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Forwards the request to another controller.
|
* Forwards the request to another controller.
|
||||||
*
|
*
|
||||||
|
@ -0,0 +1,68 @@
|
|||||||
|
<?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\DependencyInjection;
|
||||||
|
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||||
|
use Symfony\Component\HttpKernel\HttpKernel;
|
||||||
|
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
|
||||||
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This HttpKernel is used to manage scope changes of the DI container.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||||
|
*/
|
||||||
|
class ContainerAwareHttpKernel extends HttpKernel
|
||||||
|
{
|
||||||
|
protected $container;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
|
||||||
|
* @param ContainerInterface $container A ContainerInterface instance
|
||||||
|
* @param ControllerResolverInterface $controllerResolver A ControllerResolverInterface instance
|
||||||
|
*/
|
||||||
|
public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver)
|
||||||
|
{
|
||||||
|
parent::__construct($dispatcher, $controllerResolver);
|
||||||
|
|
||||||
|
$this->container = $container;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
|
||||||
|
{
|
||||||
|
$request->headers->set('X-Php-Ob-Level', ob_get_level());
|
||||||
|
|
||||||
|
$this->container->enterScope('request');
|
||||||
|
$this->container->set('request', $request, 'request');
|
||||||
|
|
||||||
|
try {
|
||||||
|
$response = parent::handle($request, $type, $catch);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->container->leaveScope('request');
|
||||||
|
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->container->leaveScope('request');
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
@ -9,16 +9,31 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Symfony\Bundle\FrameworkBundle\Tests;
|
namespace Symfony\Component\HttpKernel\Tests;
|
||||||
|
|
||||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||||
|
use Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Bundle\FrameworkBundle\HttpKernel;
|
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||||
|
|
||||||
class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
class ContainerAwareHttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
if (!class_exists('Symfony\Component\DependencyInjection\Container')) {
|
||||||
|
$this->markTestSkipped('The "DependencyInjection" component is not available');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
|
||||||
|
$this->markTestSkipped('The "EventDispatcher" component is not available');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
|
||||||
|
$this->markTestSkipped('The "HttpFoundation" component is not available');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider getProviderTypes
|
* @dataProvider getProviderTypes
|
||||||
*/
|
*/
|
||||||
@ -46,7 +61,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$dispatcher = new EventDispatcher();
|
$dispatcher = new EventDispatcher();
|
||||||
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
|
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
|
||||||
$kernel = new HttpKernel($dispatcher, $container, $resolver);
|
$kernel = new ContainerAwareHttpKernel($dispatcher, $container, $resolver);
|
||||||
|
|
||||||
$controller = function() use ($expected) {
|
$controller = function() use ($expected) {
|
||||||
return $expected;
|
return $expected;
|
||||||
@ -93,7 +108,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$dispatcher = new EventDispatcher();
|
$dispatcher = new EventDispatcher();
|
||||||
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
|
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
|
||||||
$kernel = new HttpKernel($dispatcher, $container, $resolver);
|
$kernel = new ContainerAwareHttpKernel($dispatcher, $container, $resolver);
|
||||||
|
|
||||||
$controller = function() use ($expected) {
|
$controller = function() use ($expected) {
|
||||||
throw $expected;
|
throw $expected;
|
||||||
@ -123,53 +138,4 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
|||||||
array(HttpKernelInterface::SUB_REQUEST),
|
array(HttpKernelInterface::SUB_REQUEST),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testExceptionInSubRequestsDoesNotMangleOutputBuffers()
|
|
||||||
{
|
|
||||||
$request = new Request();
|
|
||||||
|
|
||||||
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
|
|
||||||
$container
|
|
||||||
->expects($this->at(0))
|
|
||||||
->method('get')
|
|
||||||
->with($this->equalTo('request'))
|
|
||||||
->will($this->returnValue($request))
|
|
||||||
;
|
|
||||||
$container
|
|
||||||
->expects($this->at(1))
|
|
||||||
->method('getParameter')
|
|
||||||
->with($this->equalTo('kernel.debug'))
|
|
||||||
->will($this->returnValue(false))
|
|
||||||
;
|
|
||||||
$container
|
|
||||||
->expects($this->at(2))
|
|
||||||
->method('has')
|
|
||||||
->with($this->equalTo('esi'))
|
|
||||||
->will($this->returnValue(false))
|
|
||||||
;
|
|
||||||
|
|
||||||
$dispatcher = new EventDispatcher();
|
|
||||||
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
|
|
||||||
$resolver->expects($this->once())
|
|
||||||
->method('getController')
|
|
||||||
->will($this->returnValue(function () {
|
|
||||||
ob_start();
|
|
||||||
echo 'bar';
|
|
||||||
throw new \RuntimeException();
|
|
||||||
}));
|
|
||||||
$resolver->expects($this->once())
|
|
||||||
->method('getArguments')
|
|
||||||
->will($this->returnValue(array()));
|
|
||||||
|
|
||||||
$kernel = new HttpKernel($dispatcher, $container, $resolver);
|
|
||||||
|
|
||||||
// simulate a main request with output buffering
|
|
||||||
ob_start();
|
|
||||||
echo 'Foo';
|
|
||||||
|
|
||||||
// simulate a sub-request with output buffering and an exception
|
|
||||||
$kernel->render('/');
|
|
||||||
|
|
||||||
$this->assertEquals('Foo', ob_get_clean());
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
<?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\Tests\RenderingStrategy;
|
||||||
|
|
||||||
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||||
|
use Symfony\Component\HttpKernel\HttpKernel;
|
||||||
|
use Symfony\Component\HttpKernel\RenderingStrategy\DefaultRenderingStrategy;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||||
|
|
||||||
|
class DefaultRenderingStrategyTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
|
||||||
|
$this->markTestSkipped('The "EventDispatcher" component is not available');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
|
||||||
|
$this->markTestSkipped('The "HttpFoundation" component is not available');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExceptionInSubRequestsDoesNotMangleOutputBuffers()
|
||||||
|
{
|
||||||
|
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
|
||||||
|
$resolver
|
||||||
|
->expects($this->once())
|
||||||
|
->method('getController')
|
||||||
|
->will($this->returnValue(function () {
|
||||||
|
ob_start();
|
||||||
|
echo 'bar';
|
||||||
|
throw new \RuntimeException();
|
||||||
|
}))
|
||||||
|
;
|
||||||
|
$resolver
|
||||||
|
->expects($this->once())
|
||||||
|
->method('getArguments')
|
||||||
|
->will($this->returnValue(array()))
|
||||||
|
;
|
||||||
|
|
||||||
|
$kernel = new HttpKernel(new EventDispatcher(), $resolver);
|
||||||
|
$renderer = new DefaultRenderingStrategy($kernel);
|
||||||
|
|
||||||
|
// simulate a main request with output buffering
|
||||||
|
ob_start();
|
||||||
|
echo 'Foo';
|
||||||
|
|
||||||
|
// simulate a sub-request with output buffering and an exception
|
||||||
|
$renderer->render('/', Request::create('/'), array('ignore_errors' => true));
|
||||||
|
|
||||||
|
$this->assertEquals('Foo', ob_get_clean());
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user