Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 812fbb4

Browse files
committed
decoupled the Twig HttpKernelExtension runtime from the extension
1 parent 79efb4c commit 812fbb4

File tree

4 files changed

+80
-53
lines changed

4 files changed

+80
-53
lines changed

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

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Bridge\Twig\Extension;
1313

14-
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
1514
use Symfony\Component\HttpKernel\Controller\ControllerReference;
1615

1716
/**
@@ -21,62 +20,16 @@
2120
*/
2221
class HttpKernelExtension extends \Twig_Extension
2322
{
24-
private $handler;
25-
26-
/**
27-
* Constructor.
28-
*
29-
* @param FragmentHandler $handler A FragmentHandler instance
30-
*/
31-
public function __construct(FragmentHandler $handler)
32-
{
33-
$this->handler = $handler;
34-
}
35-
3623
public function getFunctions()
3724
{
3825
return array(
39-
new \Twig_SimpleFunction('render', array($this, 'renderFragment'), array('is_safe' => array('html'))),
40-
new \Twig_SimpleFunction('render_*', array($this, 'renderFragmentStrategy'), array('is_safe' => array('html'))),
41-
new \Twig_SimpleFunction('controller', array($this, 'controller')),
26+
new \Twig_SimpleFunction('render', array(HttpKernelRuntime::class, 'renderFragment'), array('is_safe' => array('html'))),
27+
new \Twig_SimpleFunction('render_*', array(HttpKernelRuntime::class, 'renderFragmentStrategy'), array('is_safe' => array('html'))),
28+
new \Twig_SimpleFunction('controller', HttpKernelRuntime::class.'::controller'),
4229
);
4330
}
4431

45-
/**
46-
* Renders a fragment.
47-
*
48-
* @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
49-
* @param array $options An array of options
50-
*
51-
* @return string The fragment content
52-
*
53-
* @see FragmentHandler::render()
54-
*/
55-
public function renderFragment($uri, $options = array())
56-
{
57-
$strategy = isset($options['strategy']) ? $options['strategy'] : 'inline';
58-
unset($options['strategy']);
59-
60-
return $this->handler->render($uri, $strategy, $options);
61-
}
62-
63-
/**
64-
* Renders a fragment.
65-
*
66-
* @param string $strategy A strategy name
67-
* @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
68-
* @param array $options An array of options
69-
*
70-
* @return string The fragment content
71-
*
72-
* @see FragmentHandler::render()
73-
*/
74-
public function renderFragmentStrategy($strategy, $uri, $options = array())
75-
{
76-
return $this->handler->render($uri, $strategy, $options);
77-
}
78-
79-
public function controller($controller, $attributes = array(), $query = array())
32+
public static function controller($controller, $attributes = array(), $query = array())
8033
{
8134
return new ControllerReference($controller, $attributes, $query);
8235
}
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 <[email protected]>
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\Bridge\Twig\Extension;
13+
14+
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
15+
use Symfony\Component\HttpKernel\Controller\ControllerReference;
16+
17+
/**
18+
* Provides integration with the HttpKernel component.
19+
*
20+
* @author Fabien Potencier <[email protected]>
21+
*/
22+
class HttpKernelRuntime
23+
{
24+
private $handler;
25+
26+
public function __construct(FragmentHandler $handler)
27+
{
28+
$this->handler = $handler;
29+
}
30+
31+
/**
32+
* Renders a fragment.
33+
*
34+
* @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
35+
* @param array $options An array of options
36+
*
37+
* @return string The fragment content
38+
*
39+
* @see FragmentHandler::render()
40+
*/
41+
public function renderFragment($uri, $options = array())
42+
{
43+
$strategy = isset($options['strategy']) ? $options['strategy'] : 'inline';
44+
unset($options['strategy']);
45+
46+
return $this->handler->render($uri, $strategy, $options);
47+
}
48+
49+
/**
50+
* Renders a fragment.
51+
*
52+
* @param string $strategy A strategy name
53+
* @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
54+
* @param array $options An array of options
55+
*
56+
* @return string The fragment content
57+
*
58+
* @see FragmentHandler::render()
59+
*/
60+
public function renderFragmentStrategy($strategy, $uri, $options = array())
61+
{
62+
return $this->handler->render($uri, $strategy, $options);
63+
}
64+
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Twig\Tests\Extension;
1313

1414
use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
15+
use Symfony\Bridge\Twig\Extension\HttpKernelRuntime;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpFoundation\Response;
1718
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
@@ -71,7 +72,13 @@ protected function renderTemplate(FragmentHandler $renderer, $template = '{{ ren
7172
{
7273
$loader = new \Twig_Loader_Array(array('index' => $template));
7374
$twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
74-
$twig->addExtension(new HttpKernelExtension($renderer));
75+
$twig->addExtension(new HttpKernelExtension());
76+
77+
$loader = $this->getMock('Twig_RuntimeLoaderInterface');
78+
$loader->expects($this->any())->method('load')->will($this->returnValueMap(array(
79+
array('Symfony\Bridge\Twig\Extension\HttpKernelRuntime', new HttpKernelRuntime($renderer)),
80+
)));
81+
$twig->addRuntimeLoader($loader);
7582

7683
return $twig->render('index');
7784
}

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,11 @@
104104

105105
<service id="twig.extension.expression" class="Symfony\Bridge\Twig\Extension\ExpressionExtension" public="false" />
106106

107-
<service id="twig.extension.httpkernel" class="Symfony\Bridge\Twig\Extension\HttpKernelExtension" public="false">
107+
<service id="twig.extension.httpkernel" class="Symfony\Bridge\Twig\Extension\HttpKernelExtension" public="false" />
108+
109+
<service id="twig.runtime.httpkernel" class="Symfony\Bridge\Twig\Extension\HttpKernelRuntime">
108110
<argument type="service" id="fragment.handler" />
111+
<tag name="twig.runtime" />
109112
</service>
110113

111114
<service id="twig.extension.httpfoundation" class="Symfony\Bridge\Twig\Extension\HttpFoundationExtension" public="false">

0 commit comments

Comments
 (0)