Skip to content

Commit f17f586

Browse files
committed
moved the container aware HTTP kernel to the HttpKernel component
1 parent 2eea768 commit f17f586

File tree

4 files changed

+156
-90
lines changed

4 files changed

+156
-90
lines changed

src/Symfony/Bundle/FrameworkBundle/HttpKernel.php

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,20 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle;
1313

14-
use Symfony\Component\HttpFoundation\Request;
1514
use Symfony\Component\HttpFoundation\Response;
16-
use Symfony\Component\HttpFoundation\StreamedResponse;
1715
use Symfony\Component\HttpKernel\HttpKernelInterface;
18-
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
19-
use Symfony\Component\DependencyInjection\ContainerInterface;
20-
use Symfony\Component\HttpKernel\HttpKernel as BaseHttpKernel;
21-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16+
use Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel;
2217

2318
/**
2419
* This HttpKernel is used to manage scope changes of the DI container.
2520
*
2621
* @author Fabien Potencier <fabien@symfony.com>
2722
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
23+
*
24+
* @deprecated This class is deprecated in 2.2 and will be removed in 2.3
2825
*/
29-
class HttpKernel extends BaseHttpKernel
26+
class HttpKernel extends ContainerAwareHttpKernel
3027
{
31-
protected $container;
32-
33-
public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver)
34-
{
35-
parent::__construct($dispatcher, $controllerResolver);
36-
37-
$this->container = $container;
38-
}
39-
40-
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
41-
{
42-
$request->headers->set('X-Php-Ob-Level', ob_get_level());
43-
44-
$this->container->enterScope('request');
45-
$this->container->set('request', $request, 'request');
46-
47-
try {
48-
$response = parent::handle($request, $type, $catch);
49-
} catch (\Exception $e) {
50-
$this->container->leaveScope('request');
51-
52-
throw $e;
53-
}
54-
55-
$this->container->leaveScope('request');
56-
57-
return $response;
58-
}
59-
6028
/**
6129
* Forwards the request to another controller.
6230
*
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\DependencyInjection;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpKernel\HttpKernelInterface;
17+
use Symfony\Component\HttpKernel\HttpKernel;
18+
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
19+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20+
use Symfony\Component\DependencyInjection\ContainerInterface;
21+
22+
/**
23+
* This HttpKernel is used to manage scope changes of the DI container.
24+
*
25+
* @author Fabien Potencier <fabien@symfony.com>
26+
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
27+
*/
28+
class ContainerAwareHttpKernel extends HttpKernel
29+
{
30+
protected $container;
31+
32+
/**
33+
* Constructor.
34+
*
35+
* @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
36+
* @param ContainerInterface $container A ContainerInterface instance
37+
* @param ControllerResolverInterface $controllerResolver A ControllerResolverInterface instance
38+
*/
39+
public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver)
40+
{
41+
parent::__construct($dispatcher, $controllerResolver);
42+
43+
$this->container = $container;
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
50+
{
51+
$request->headers->set('X-Php-Ob-Level', ob_get_level());
52+
53+
$this->container->enterScope('request');
54+
$this->container->set('request', $request, 'request');
55+
56+
try {
57+
$response = parent::handle($request, $type, $catch);
58+
} catch (\Exception $e) {
59+
$this->container->leaveScope('request');
60+
61+
throw $e;
62+
}
63+
64+
$this->container->leaveScope('request');
65+
66+
return $response;
67+
}
68+
}

src/Symfony/Bundle/FrameworkBundle/Tests/HttpKernelTest.php renamed to src/Symfony/Component/HttpKernel/Tests/DependencyInjection/ContainerAwareHttpKernelTest.php

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,31 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Bundle\FrameworkBundle\Tests;
12+
namespace Symfony\Component\HttpKernel\Tests;
1313

1414
use Symfony\Component\HttpKernel\HttpKernelInterface;
15+
use Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel;
1516
use Symfony\Component\HttpFoundation\Response;
1617
use Symfony\Component\HttpFoundation\Request;
17-
use Symfony\Bundle\FrameworkBundle\HttpKernel;
1818
use Symfony\Component\EventDispatcher\EventDispatcher;
1919

20-
class HttpKernelTest extends \PHPUnit_Framework_TestCase
20+
class ContainerAwareHttpKernelTest extends \PHPUnit_Framework_TestCase
2121
{
22+
protected function setUp()
23+
{
24+
if (!class_exists('Symfony\Component\DependencyInjection\Container')) {
25+
$this->markTestSkipped('The "DependencyInjection" component is not available');
26+
}
27+
28+
if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
29+
$this->markTestSkipped('The "EventDispatcher" component is not available');
30+
}
31+
32+
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
33+
$this->markTestSkipped('The "HttpFoundation" component is not available');
34+
}
35+
}
36+
2237
/**
2338
* @dataProvider getProviderTypes
2439
*/
@@ -46,7 +61,7 @@ public function testHandle($type)
4661

4762
$dispatcher = new EventDispatcher();
4863
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
49-
$kernel = new HttpKernel($dispatcher, $container, $resolver);
64+
$kernel = new ContainerAwareHttpKernel($dispatcher, $container, $resolver);
5065

5166
$controller = function() use ($expected) {
5267
return $expected;
@@ -93,7 +108,7 @@ public function testHandleRestoresThePreviousRequestOnException($type)
93108

94109
$dispatcher = new EventDispatcher();
95110
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
96-
$kernel = new HttpKernel($dispatcher, $container, $resolver);
111+
$kernel = new ContainerAwareHttpKernel($dispatcher, $container, $resolver);
97112

98113
$controller = function() use ($expected) {
99114
throw $expected;
@@ -123,53 +138,4 @@ public function getProviderTypes()
123138
array(HttpKernelInterface::SUB_REQUEST),
124139
);
125140
}
126-
127-
public function testExceptionInSubRequestsDoesNotMangleOutputBuffers()
128-
{
129-
$request = new Request();
130-
131-
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
132-
$container
133-
->expects($this->at(0))
134-
->method('get')
135-
->with($this->equalTo('request'))
136-
->will($this->returnValue($request))
137-
;
138-
$container
139-
->expects($this->at(1))
140-
->method('getParameter')
141-
->with($this->equalTo('kernel.debug'))
142-
->will($this->returnValue(false))
143-
;
144-
$container
145-
->expects($this->at(2))
146-
->method('has')
147-
->with($this->equalTo('esi'))
148-
->will($this->returnValue(false))
149-
;
150-
151-
$dispatcher = new EventDispatcher();
152-
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
153-
$resolver->expects($this->once())
154-
->method('getController')
155-
->will($this->returnValue(function () {
156-
ob_start();
157-
echo 'bar';
158-
throw new \RuntimeException();
159-
}));
160-
$resolver->expects($this->once())
161-
->method('getArguments')
162-
->will($this->returnValue(array()));
163-
164-
$kernel = new HttpKernel($dispatcher, $container, $resolver);
165-
166-
// simulate a main request with output buffering
167-
ob_start();
168-
echo 'Foo';
169-
170-
// simulate a sub-request with output buffering and an exception
171-
$kernel->render('/');
172-
173-
$this->assertEquals('Foo', ob_get_clean());
174-
}
175141
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Tests\RenderingStrategy;
13+
14+
use Symfony\Component\HttpKernel\HttpKernelInterface;
15+
use Symfony\Component\HttpKernel\HttpKernel;
16+
use Symfony\Component\HttpKernel\RenderingStrategy\DefaultRenderingStrategy;
17+
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\EventDispatcher\EventDispatcher;
20+
21+
class DefaultRenderingStrategyTest extends \PHPUnit_Framework_TestCase
22+
{
23+
protected function setUp()
24+
{
25+
if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
26+
$this->markTestSkipped('The "EventDispatcher" component is not available');
27+
}
28+
29+
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
30+
$this->markTestSkipped('The "HttpFoundation" component is not available');
31+
}
32+
}
33+
34+
public function testExceptionInSubRequestsDoesNotMangleOutputBuffers()
35+
{
36+
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
37+
$resolver
38+
->expects($this->once())
39+
->method('getController')
40+
->will($this->returnValue(function () {
41+
ob_start();
42+
echo 'bar';
43+
throw new \RuntimeException();
44+
}))
45+
;
46+
$resolver
47+
->expects($this->once())
48+
->method('getArguments')
49+
->will($this->returnValue(array()))
50+
;
51+
52+
$kernel = new HttpKernel(new EventDispatcher(), $resolver);
53+
$renderer = new DefaultRenderingStrategy($kernel);
54+
55+
// simulate a main request with output buffering
56+
ob_start();
57+
echo 'Foo';
58+
59+
// simulate a sub-request with output buffering and an exception
60+
$renderer->render('/', Request::create('/'), array('ignore_errors' => true));
61+
62+
$this->assertEquals('Foo', ob_get_clean());
63+
}
64+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy