Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9aaceb1

Browse filesBrowse files
committed
moved the logic from HttpKernel in FrameworkBundle to the HttpKernel component
1 parent b981a6f commit 9aaceb1
Copy full SHA for 9aaceb1

File tree

Expand file treeCollapse file tree

20 files changed

+760
-158
lines changed
Filter options
Expand file treeCollapse file tree

20 files changed

+760
-158
lines changed

‎src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php
+16-45Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,83 +11,54 @@
1111

1212
namespace Symfony\Bridge\Twig\Extension;
1313

14-
use Symfony\Component\HttpFoundation\Request;
15-
use Symfony\Component\HttpKernel\KernelEvents;
16-
use Symfony\Component\HttpKernel\HttpKernelInterface;
17-
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
18-
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14+
use Symfony\Component\HttpKernel\HttpContentRenderer;
15+
use Symfony\Component\HttpKernel\Controller\ControllerReference;
1916

2017
/**
2118
* Provides integration with the HttpKernel component.
2219
*
2320
* @author Fabien Potencier <fabien@symfony.com>
2421
*/
25-
class HttpKernelExtension extends \Twig_Extension implements EventSubscriberInterface
22+
class HttpKernelExtension extends \Twig_Extension
2623
{
27-
private $kernel;
28-
private $request;
24+
private $renderer;
2925

3026
/**
3127
* Constructor.
3228
*
33-
* @param HttpKernelInterface $kernel A HttpKernelInterface install
29+
* @param HttpContentRenderer $kernel A HttpContentRenderer instance
3430
*/
35-
public function __construct(HttpKernelInterface $kernel)
31+
public function __construct(HttpContentRenderer $renderer)
3632
{
37-
$this->kernel = $kernel;
33+
$this->renderer = $renderer;
3834
}
3935

4036
public function getFunctions()
4137
{
4238
return array(
43-
'render' => new \Twig_Function_Method($this, 'render', array('needs_environment' => true, 'is_safe' => array('html'))),
39+
'render' => new \Twig_Function_Method($this, 'render', array('is_safe' => array('html'))),
40+
'controller' => new \Twig_Function_Method($this, 'controller'),
4441
);
4542
}
4643

4744
/**
4845
* Renders a URI.
4946
*
50-
* @param \Twig_Environment $twig A \Twig_Environment instance
51-
* @param string $uri The URI to render
47+
* @param string $uri A URI
48+
* @param array $options An array of options
5249
*
5350
* @return string The Response content
5451
*
55-
* @throws \RuntimeException
52+
* @see Symfony\Component\HttpKernel\HttpContentRenderer::render()
5653
*/
57-
public function render(\Twig_Environment $twig, $uri)
54+
public function render($uri, $options = array())
5855
{
59-
if (null !== $this->request) {
60-
$cookies = $this->request->cookies->all();
61-
$server = $this->request->server->all();
62-
} else {
63-
$cookies = array();
64-
$server = array();
65-
}
66-
67-
$subRequest = Request::create($uri, 'get', array(), $cookies, array(), $server);
68-
if (null !== $this->request && $this->request->getSession()) {
69-
$subRequest->setSession($this->request->getSession());
70-
}
71-
72-
$response = $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
73-
74-
if (!$response->isSuccessful()) {
75-
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $subRequest->getUri(), $response->getStatusCode()));
76-
}
77-
78-
return $response->getContent();
56+
return $this->renderer->render($uri, $options);
7957
}
8058

81-
public function onKernelRequest(GetResponseEvent $event)
59+
public function controller($controller, $attributes = array(), $query = array())
8260
{
83-
$this->request = $event->getRequest();
84-
}
85-
86-
public static function getSubscribedEvents()
87-
{
88-
return array(
89-
KernelEvents::REQUEST => array('onKernelRequest'),
90-
);
61+
return new ControllerReference($controller, $attributes, $query);
9162
}
9263

9364
public function getName()

‎src/Symfony/Bridge/Twig/Tests/Extension/HttpKernelExtensionTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Extension/HttpKernelExtensionTest.php
+10-13Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
1515
use Symfony\Bridge\Twig\Tests\TestCase;
1616
use Symfony\Component\HttpFoundation\Response;
17-
use Symfony\Component\HttpKernel\HttpKernelInterface;
17+
use Symfony\Component\HttpKernel\HttpContentRenderer;
1818

1919
class HttpKernelExtensionTest extends TestCase
2020
{
@@ -31,7 +31,7 @@ protected function setUp()
3131

3232
public function testRenderWithoutMasterRequest()
3333
{
34-
$kernel = $this->getKernel($this->returnValue(new Response('foo')));
34+
$kernel = $this->getHttpContentRenderer($this->returnValue('foo'));
3535

3636
$this->assertEquals('foo', $this->renderTemplate($kernel));
3737
}
@@ -41,7 +41,7 @@ public function testRenderWithoutMasterRequest()
4141
*/
4242
public function testRenderWithError()
4343
{
44-
$kernel = $this->getKernel($this->throwException(new \Exception('foo')));
44+
$kernel = $this->getHttpContentRenderer($this->throwException(new \Exception('foo')));
4545

4646
$loader = new \Twig_Loader_Array(array('index' => '{{ render("foo") }}'));
4747
$twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
@@ -50,23 +50,20 @@ public function testRenderWithError()
5050
$this->renderTemplate($kernel);
5151
}
5252

53-
protected function getKernel($return)
53+
protected function getHttpContentRenderer($return)
5454
{
55-
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
56-
$kernel
57-
->expects($this->once())
58-
->method('handle')
59-
->will($return)
60-
;
55+
$strategy = $this->getMock('Symfony\\Component\\HttpKernel\\RenderingStrategy\\RenderingStrategyInterface');
56+
$strategy->expects($this->once())->method('getName')->will($this->returnValue('default'));
57+
$strategy->expects($this->once())->method('render')->will($return);
6158

62-
return $kernel;
59+
return new HttpContentRenderer(array($strategy));
6360
}
6461

65-
protected function renderTemplate(HttpKernelInterface $kernel, $template = '{{ render("foo") }}')
62+
protected function renderTemplate(HttpContentRenderer $renderer, $template = '{{ render("foo") }}')
6663
{
6764
$loader = new \Twig_Loader_Array(array('index' => $template));
6865
$twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
69-
$twig->addExtension(new HttpKernelExtension($kernel));
66+
$twig->addExtension(new HttpKernelExtension($renderer));
7067

7168
return $twig->render('index');
7269
}
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\Bundle\FrameworkBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Reference;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\Exception\LogicException;
18+
19+
/**
20+
* Adds services tagged kernel.content_renderer_strategy as HTTP content rendering strategies.
21+
*
22+
* @author Fabien Potencier <fabien@symfony.com>
23+
*/
24+
class HttpRenderingStrategyPass implements CompilerPassInterface
25+
{
26+
public function process(ContainerBuilder $container)
27+
{
28+
if (false === $container->hasDefinition('http_content_renderer')) {
29+
return;
30+
}
31+
32+
$definition = $container->getDefinition('http_content_renderer');
33+
foreach (array_keys($container->findTaggedServiceIds('kernel.content_renderer_strategy')) as $id) {
34+
$definition->addMethodCall('addStrategy', array(new Reference($id)));
35+
}
36+
}
37+
}

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function load(array $configs, ContainerBuilder $container)
4141

4242
$loader->load('web.xml');
4343
$loader->load('services.xml');
44+
$loader->load('content_generator.xml');
4445

4546
// A translator must always be registered (as support is included by
4647
// default in the Form component). If disabled, an identity translator

‎src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CompilerDebugDumpPass;
2626
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationExtractorPass;
2727
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationDumperPass;
28+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\HttpRenderingStrategyPass;
2829
use Symfony\Component\DependencyInjection\ContainerBuilder;
2930
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
3031
use Symfony\Component\DependencyInjection\Scope;
@@ -65,6 +66,7 @@ public function build(ContainerBuilder $container)
6566
$container->addCompilerPass(new AddCacheClearerPass());
6667
$container->addCompilerPass(new TranslationExtractorPass());
6768
$container->addCompilerPass(new TranslationDumperPass());
69+
$container->addCompilerPass(new HttpRenderingStrategyPass());
6870

6971
if ($container->getParameter('kernel.debug')) {
7072
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);

‎src/Symfony/Bundle/FrameworkBundle/HttpKernel.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/HttpKernel.php
+4-90Lines changed: 4 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ class HttpKernel extends BaseHttpKernel
3030
{
3131
protected $container;
3232

33-
private $esiSupport;
34-
3533
public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver)
3634
{
3735
parent::__construct($dispatcher, $controllerResolver);
@@ -96,97 +94,13 @@ public function forward($controller, array $attributes = array(), array $query =
9694
*
9795
* @throws \RuntimeException
9896
* @throws \Exception
99-
*/
100-
public function render($uri, array $options = array())
101-
{
102-
$request = $this->container->get('request');
103-
104-
$options = array_merge(array(
105-
'ignore_errors' => !$this->container->getParameter('kernel.debug'),
106-
'alt' => null,
107-
'standalone' => false,
108-
'comment' => '',
109-
'default' => null,
110-
), $options);
111-
112-
if (null === $this->esiSupport) {
113-
$this->esiSupport = $this->container->has('esi') && $this->container->get('esi')->hasSurrogateEsiCapability($request);
114-
}
115-
116-
if ($this->esiSupport && (true === $options['standalone'] || 'esi' === $options['standalone'])) {
117-
return $this->container->get('esi')->renderIncludeTag($uri, $options['alt'], $options['ignore_errors'], $options['comment']);
118-
}
119-
120-
if ('js' === $options['standalone']) {
121-
$defaultContent = null;
122-
123-
$templating = $this->container->get('templating');
124-
125-
if ($options['default']) {
126-
if ($templating->exists($options['default'])) {
127-
$defaultContent = $templating->render($options['default']);
128-
} else {
129-
$defaultContent = $options['default'];
130-
}
131-
} elseif ($template = $this->container->getParameter('templating.hinclude.default_template')) {
132-
$defaultContent = $templating->render($template);
133-
}
134-
135-
return $this->renderHIncludeTag($uri, $defaultContent);
136-
}
137-
138-
$subRequest = Request::create($uri, 'get', array(), $request->cookies->all(), array(), $request->server->all());
139-
if ($session = $request->getSession()) {
140-
$subRequest->setSession($session);
141-
}
142-
143-
$level = ob_get_level();
144-
try {
145-
$response = $this->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
146-
147-
if (!$response->isSuccessful()) {
148-
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
149-
}
150-
151-
if (!$response instanceof StreamedResponse) {
152-
return $response->getContent();
153-
}
154-
155-
$response->sendContent();
156-
} catch (\Exception $e) {
157-
if ($options['alt']) {
158-
$alt = $options['alt'];
159-
unset($options['alt']);
160-
161-
return $this->render($alt, $options);
162-
}
163-
164-
if (!$options['ignore_errors']) {
165-
throw $e;
166-
}
167-
168-
// let's clean up the output buffers that were created by the sub-request
169-
while (ob_get_level() > $level) {
170-
ob_get_clean();
171-
}
172-
}
173-
}
174-
175-
/**
176-
* Renders an HInclude tag.
177-
*
178-
* @param string $uri A URI
179-
* @param string $defaultContent Default content
18097
*
181-
* @return string
98+
* @deprecated in 2.2, will be removed in 2.3 (use Symfony\Component\HttpKernel\HttpContentRenderer::render() instead)
18299
*/
183-
public function renderHIncludeTag($uri, $defaultContent = null)
100+
public function render($uri, array $options = array())
184101
{
185-
return sprintf('<hx:include src="%s">%s</hx:include>', $uri, $defaultContent);
186-
}
102+
trigger_error('render() is deprecated since version 2.2 and will be removed in 2.3. Use Symfony\Component\HttpKernel\HttpContentRenderer::render() instead.', E_USER_DEPRECATED);
187103

188-
public function hasEsiSupport()
189-
{
190-
return $this->esiSupport;
104+
$this->container->get('http_content_renderer')->render($uri, $options);
191105
}
192106
}
+48Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<parameters>
8+
<parameter key="http_content_renderer.class">Symfony\Component\HttpKernel\HttpContentRenderer</parameter>
9+
<parameter key="http_content_renderer.strategy.default.class">Symfony\Component\HttpKernel\RenderingStrategy\DefaultRenderingStrategy</parameter>
10+
<parameter key="http_content_renderer.strategy.esi.class">Symfony\Component\HttpKernel\RenderingStrategy\EsiRenderingStrategy</parameter>
11+
<parameter key="http_content_renderer.strategy.hinclude.class">Symfony\Component\HttpKernel\RenderingStrategy\HIncludeRenderingStrategy</parameter>
12+
<parameter key="http_content_renderer.strategy.hinclude.global_template"></parameter>
13+
<parameter key="http_content_renderer.listener.router_proxy.class">Symfony\Component\HttpKernel\EventListener\RouterProxyListener</parameter>
14+
</parameters>
15+
16+
<services>
17+
<service id="http_content_renderer" class="%http_content_renderer.class%">
18+
<tag name="kernel.event_subscriber" />
19+
<argument type="collection" />
20+
<argument>%kernel.debug%</argument>
21+
</service>
22+
23+
<service id="http_content_renderer.strategy.default" class="%http_content_renderer.strategy.default.class%">
24+
<tag name="kernel.content_renderer_strategy" />
25+
<argument type="service" id="http_kernel" />
26+
<call method="setUrlGenerator"><argument type="service" id="router" /></call>
27+
</service>
28+
29+
<service id="http_content_renderer.strategy.esi" class="%http_content_renderer.strategy.esi.class%">
30+
<tag name="kernel.content_renderer_strategy" />
31+
<argument type="service" id="esi" />
32+
<argument type="service" id="http_content_renderer.strategy.default" />
33+
<call method="setUrlGenerator"><argument type="service" id="router" /></call>
34+
</service>
35+
36+
<service id="http_content_renderer.strategy.hinclude" class="%http_content_renderer.strategy.hinclude.class%">
37+
<tag name="kernel.content_renderer_strategy" />
38+
<argument type="service" id="templating" />
39+
<argument>%http_content_renderer.strategy.hinclude.global_template%</argument>
40+
</service>
41+
42+
<!-- FIXME: make the listener registration optional via a configuration setting? -->
43+
<service id="http_content_renderer.listener.router_proxy" class="%http_content_renderer.listener.router_proxy.class%">
44+
<tag name="kernel.event_subscriber" />
45+
</service>
46+
47+
</services>
48+
</container>
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<routes xmlns="http://symfony.com/schema/routing"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
6+
7+
<route id="_proxy" pattern="/_proxy/{_controller}.{_format}" />
8+
</routes>

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181

8282
<service id="templating.helper.actions" class="%templating.helper.actions.class%">
8383
<tag name="templating.helper" alias="actions" />
84-
<argument type="service" id="http_kernel" />
84+
<argument type="service" id="http_content_renderer" />
8585
</service>
8686

8787
<service id="templating.helper.code" class="%templating.helper.code.class%">

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.