8000 Bumped Twig version to 1.24 and get rid of initRuntime · symfony/symfony@2fed6d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2fed6d3

Browse files
committed
Bumped Twig version to 1.24 and get rid of initRuntime
1 parent 273740f commit 2fed6d3

15 files changed

+240
-170
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": ">=5.5.9",
2020
"doctrine/common": "~2.4",
21-
"twig/twig": "~1.23|~2.0",
21+
"twig/twig": "~1.25|~2.0",
2222
"psr/cache": "~1.0",
2323
"psr/log": "~1.0",
2424
"symfony/polyfill-intl-icu": "~1.0",
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\Bridge\Twig\Extension;
13+
14+
use Symfony\Component\DependencyInjection\ContainerInterface;
15+
16+
/**
17+
* Loads Twig extension runtimes.
18+
*
19+
* @author Fabien Potencier <fabien@symfony.com>
20+
*/
21+
class ContainerAwareRuntimeLoader implements \Twig_RuntimeLoaderInterface
22+
{
23+
private $container;
24+
25+
public function __construct(ContainerInterface $container)
26+
{
27+
$this->container = $container;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function load($name)
34+
{
35+
$id = 'twig.extension.runtime.'.$name;
36+
37+
if ($this->container->has($id)) {
38+
return $this->container->get($id);
39+
}
40+
}
41+
}

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

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Bridge\Twig\Extension;
1313

1414
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
15-
use Symfony\Bridge\Twig\Form\TwigRenderer;
1615
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
1716

1817
/**
@@ -23,19 +22,6 @@
2322
*/
2423
class FormExtension extends \Twig_Extension
2524
{
26-
/**
27-
* This property is public so that it can be accessed directly from compiled
28-
* templates without having to call a getter, which slightly decreases performance.
29-
*
30-
* @var TwigRenderer
31-
*/
32-
public $renderer;
33-
34-
public function __construct(TwigRenderer $renderer)
35-
{
36-
$this->renderer = $renderer;
37-
}
38-
3925
/**
4026
* {@inheritdoc}
4127
*/
@@ -85,58 +71,6 @@ public function getTests()
8571
);
8672
}
8773

88-
/**
89-
* {@inheritdoc}
90-
*/
91-
public function renderCsrfToken($tokenId)
92-
{
93-
return $this->renderer->renderCsrfToken($tokenId);
94-
}
95-
96-
/**
97-
* Makes a technical name human readable.
98-
*
99-
* @param string $text The text to humanize
100-
*
101-
* @return string The humanized text
102-
*/
103-
public function humanize($text)
104-
{
105-
return $this->renderer->humanize($text);
106-
}
107-
108-
/**
109-
* Returns whether a choice is selected for a given form value.
110-
*
111-
* Unfortunately Twig does not support an efficient way to execute the
112-
* "is_selected" closure passed to the template by ChoiceType. It is faster
113-
* to implement the logic here (around 65ms for a specific form).
114-
*
115-
* Directly implementing the logic here is also faster than doing so in
116-
* ChoiceView (around 30ms).
117-
*
118-
* The worst option tested so far is to implement the logic in ChoiceView
119-
* and access the ChoiceView method directly in the template. Doing so is
120-
* around 220ms slower than doing the method call here in the filter. Twig
121-
* seems to be much more efficient at executing filters than at executing
122-
* methods of an object.
123-
*
124-
* @param ChoiceView $choice The choice to check
125-
* @param string|array $selectedValue The selected value to compare
126-
*
127-
* @return bool Whether the choice is selected
128-
*
129-
* @see ChoiceView::isSelected()
130-
*/
131-
public function isSelectedChoice(ChoiceView $choice, $selectedValue)
132-
{
133-
if (is_array($selectedValue)) {
134-
return in_array($choice->value, $selectedValue, true);
135-
}
136-
137-
return $choice->value === $selectedValue;
138-
}
139-
14074
/**
14175
* {@inheritdoc}
14276
*/
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien F438 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\Bridge\Twig\Extension;
13+
14+
use Symfony\Bridge\Twig\Form\TwigRenderer;
15+
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
16+
17+
/**
18+
* FormExtension extends Twig with form capabilities.
19+
*
20+
* @author Fabien Potencier <fabien@symfony.com>
21+
*/
22+
class FormExtensionRuntime
23+
{
24+
/**
25+
* This property is public so that it can be accessed directly from compiled
26+
* templates without having to call a getter, which slightly decreases performance.
27+
*
28+
* @var TwigRenderer
29+
*/
30+
public $renderer;
31+
32+
public function __construct(TwigRenderer $renderer)
33+
{
34+
$this->renderer = $renderer;
35+
}
36+
37+
/**
38+
* Renders a CSRF token.
39+
*
40+
* @param string $intention The intention of the protected action.
41+
*
42+
* @return string A CSRF token.
43+
*/
44+
public function renderCsrfToken($intention)
45+
{
46+
return $this->renderer->renderCsrfToken($intention);
47+
}
48+
49+
/**
50+
* Makes a technical name human readable.
51+
*
52+
* @param string $text The text to humanize.
53+
*
54+
* @return string The humanized text.
55+
*/
56+
public function humanize($text)
57+
{
58+
return $this->renderer->humanize($text);
59+
}
60+
61+
/**
62+
* Returns whether a choice is selected for a given form value.
63+
*
64+
* Unfortunately Twig does not support an efficient way to execute the
65+
* "is_selected" closure passed to the template by ChoiceType. It is faster
66+
* to implement the logic here (around 65ms for a specific form).
67+
*
68+
* Directly implementing the logic here is also faster than doing so in
69+
* ChoiceView (around 30ms).
70+
*
71+
* The worst option tested so far is to implement the logic in ChoiceView
72+
* and access the ChoiceView method directly in the template. Doing so is
73+
* around 220ms slower than doing the method call here in the filter. Twig
74+
* seems to be much more efficient at executing filters than at executing
75+
* methods of an object.
76+
*
77+
* @param ChoiceView $choice The choice to check.
78+
* @param string|array $selectedValue The selected value to compare.
79+
*
80+
* @return bool Whether the choice is selected.
81+
*
82+
* @see ChoiceView::isSelected()
83+
*/
84+
public function isSelectedChoice(ChoiceView $choice, $selectedValue)
85+
{
86+
if (is_array($selectedValue)) {
87+
return in_array($choice->value, $selectedValue, true);
88+
}
89+
90+
return $choice->value === $selectedValue;
91+
}
92+
}

src/Symfony/Bridge/Twig/Node/FormThemeNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function compile(\Twig_Compiler $compiler)
3030
{
3131
$compiler
3232
->addDebugInfo($this)
33-
->write('$this->env->getExtension(\'form\')->renderer->setTheme(')
33+
->write('$this->env->getRuntime(\'form\')->renderer->setTheme(')
3434
->subcompile($this->getNode('form'))
3535
->raw(', ')
3636
->subcompile($this->getNode('resources'))

src/Symfony/Bridge/Twig/Node/RenderBlockNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function compile(\Twig_Compiler $compiler)
2525
{
2626
$compiler->addDebugInfo($this);
2727
$arguments = iterator_to_array($this->getNode('arguments'));
28-
$compiler->write('$this->env->getExtension(\'form\')->renderer->renderBlock(');
28+
$compiler->write('$this->env->getRuntime(\'form\')->renderer->renderBlock(');
2929

3030
if (isset($arguments[0])) {
3131
$compiler->subcompile($arguments[0]);

src/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class SearchAndRenderBlockNode extends \Twig_Node_Expression_Function
1919
public function compile(\Twig_Compiler $compiler)
2020
{
2121
$compiler->addDebugInfo($this);
22-
$compiler->raw('$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(');
22+
$compiler->raw('$this->env->getRuntime(\'form\')->renderer->searchAndRenderBlock(');
2323

2424
preg_match('/_([^_]+)$/', $this->getAttribute('name'), $matches);
2525

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

Lines changed: 25 additions & 27 deletions
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\FormExtension;
15+
use Symfony\Bridge\Twig\Extension\FormExtensionRuntime;
1516
use Symfony\Bridge\Twig\Form\TwigRenderer;
1617
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
1718
use Symfony\Bridge\Twig\Extension\TranslationExtension;
@@ -22,10 +23,11 @@
2223

2324
class FormExtensionBootstrap3LayoutTest extends AbstractBootstrap3LayoutTest
2425
{
25-
/**
26-
* @var FormExtension
27-
*/
28-
protected $extension;
26+
protected $testableFeatures = array(
27+
'choice_attr',
28+
);
29+
30+
private $environment;
2931

3032
protected function setUp()
3133
{
@@ -36,27 +38,23 @@ protected function setUp()
3638
__DIR__.'/Fixtures/templates/form',
3739
));
3840

39-
$environment = new \Twig_Environment($loader, array('strict_variables' => true));
40-
$environment->addExtension(new TranslationExtension(new StubTranslator()));
41+
$this->environment = new \Twig_Environment($loader, array('strict_variables' => true));
42+
$this->environment->addExtension(new TranslationExtension(new StubTranslator()));
43+
$this->environment->addExtension(new FormExtension());
4144

4245
$rendererEngine = new TwigRendererEngine(array(
4346
'bootstrap_3_layout.html.twig',
4447
'custom_widgets.html.twig',
45-
), $environment);
48+
), $this->environment);
4649
$renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'));
4750

48-
$this->extension = new FormExtension($renderer);
49-
50-
$environment->addExtension($this->extension);
51-
52-
$this->extension->initRuntime($environment);
53-
}
54-
55-
protected function tearDown()
56-
{
57-
parent::tearDown();
51+
$loader = $this->getMock('Twig_RuntimeLoaderInterface');
52+
$loader->expects($this->any())->method('load')->will($this->returnValueMap(array(
53+
array('form', new FormExtensionRuntime($renderer)),
54+
array('translator', null),
55+
)));
5856

59-
$this->extension = null;
57+
$this->environment->registerRuntimeLoader($loader);
6058
}
6159

6260
public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
@@ -85,7 +83,7 @@ public function testStartTagHasActionAttributeWhenActionIsZero()
8583

8684
protected function renderForm(FormView $view, array $vars = array())
8785
{
88-
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
86+
return (string) $this->environment->getRuntime('form')->renderer->renderBlock($view, 'form', $vars);
8987
}
9088

9189
protected function renderLabel(FormView $view, $label = null, array $vars = array())
@@ -94,41 +92,41 @@ protected function renderLabel(FormView $view, $label = null, array $vars = arra
9492
$vars += array('label' => $label);
9593
}
9694

97-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'label', $vars);
95+
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'label', $vars);
9896
}
9997

10098
protected function renderErrors(FormView $view)
10199
{
102-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'errors');
100+
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'errors');
103101
}
104102

105103
protected function renderWidget(FormView $view, array $vars = array())
106104
{
107-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars);
105+
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'widget', $vars);
108106
}
109< 10000 /td>107

110108
protected function renderRow(FormView $view, array $vars = array())
111109
{
112-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'row', $vars);
110+
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'row', $vars);
113111
}
114112

115113
protected function renderRest(FormView $view, array $vars = array())
116114
{
117-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars);
115+
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'rest', $vars);
118116
}
119117

120118
protected function renderStart(FormView $view, array $vars = array())
121119
{
122-
return (string) $this->extension->renderer->renderBlock($view, 'form_start', $vars);
120+
return (string) $this->environment->getRuntime('form')->renderer->renderBlock($view, 'form_start', $vars);
123121
}
124122

125123
protected function renderEnd(FormView $view, array $vars = array())
126124
{
127-
return (string) $this->extension->renderer->renderBlock($view, 'form_end', $vars);
125+
return (string) $this->environment->getRuntime('form')->renderer->renderBlock($view, 'form_end', $vars);
128126
}
129127

130128
protected function setTheme(FormView $view, array $themes)
131129
{
132-
$this->extension->renderer->setTheme($view, $themes);
130+
$this->environment->getRuntime('form')->renderer->setTheme($view, $themes);
133131
}
134132
}

0 commit comments

Comments
 (0)
0