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

Skip to content
10000

Commit 270c970

Browse files
committed
Bumped Twig version to 1.24 and get rid of initRuntime
1 parent cfdd446 commit 270c970

15 files changed

+235
-182
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.3.9",
2020
"doctrine/common": "~2.4",
21-
"twig/twig": "~1.23|~2.0",
21+
"twig/twig": "~1.24|~2.0",
2222
"psr/log": "~1.0",
2323
"symfony/security-acl": "~2.7",
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_RuntimeExtensionLoaderInterface
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 & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace Symfony\Bridge\Twig\Extension;
1313

1414
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
15-
use Symfony\Bridge\Twig\Form\TwigRenderer;
16-
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
1715

1816
/**
1917
* FormExtension extends Twig with form capabilities.
@@ -23,19 +21,6 @@
2321
*/
2422
class FormExtension extends \Twig_Extension
2523
{
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-
3924
/**
4025
* {@inheritdoc}
4126
*/
@@ -86,62 +71,6 @@ public function getTests()
8671
);
8772
}
8873

89-
/**
90-
* Renders a CSRF token.
91-
*
92-
* @param string $intention The intention of the protected action.
93-
*
94-
* @return string A CSRF token.
95-
*/
96-
public function renderCsrfToken($intention)
97-
{
98-
return $this->renderer->renderCsrfToken($intention);
99-
}
100-
101-
/**
102-
* Makes a technical name human readable.
103-
*
104-
* @param string $text The text to humanize.
105-
*
106-
* @return string The humanized text.
107-
*/
108-
public function humanize($text)
109-
{
110-
return $this->renderer->humanize($text);
111-
}
112-
113-
/**
114-
* Returns whether a choice is selected for a given form value.
115-
*
116-
* Unfortunately Twig does not support an efficient way to execute the
117-
* "is_selected" closure passed to the template by ChoiceType. It is faster
118-
* to implement the logic here (around 65ms for a specific form).
119-
*
120-
* Directly implementing the logic here is also faster than doing so in
121-
* ChoiceView (around 30ms).
122-
*
123-
* The worst option tested so far is to implement the logic in ChoiceView
124-
* and access the ChoiceView method directly in the template. Doing so is
125-
* around 220ms slower than doing the method call here in the filter. Twig
126-
* seems to be much more efficient at executing filters than at executing
127-
* methods of an object.
128-
*
129-
* @param ChoiceView $choice The choice to check.
130-
* @param string|array $selectedValue The selected value to compare.
131-
*
132-
* @return bool Whether the choice is selected.
133-
*
134-
* @see ChoiceView::isSelected()
135-
*/
136-
public function isSelectedChoice(ChoiceView $choice, $selectedValue)
137-
{
138-
if (is_array($selectedValue)) {
139-
return in_array($choice->value, $selectedValue, true);
140-
}
141-
142-
return $choice->value === $selectedValue;
143-
}
144-
14574
/**
14675
* {@inheritdoc}
14776
*/
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 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->getRuntimeExtension(\'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->getRuntimeExtension(\'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->getRuntimeExtension(\'form\')->renderer->searchAndRenderBlock(');
2323

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

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

Lines changed: 23 additions & 29 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,15 +23,12 @@
2223

2324
class FormExtensionBootstrap3LayoutTest extends AbstractBootstrap3LayoutTest
2425
{
25-
/**
26-
* @var FormExtension
27-
*/
28-
protected $extension;
29-
3026
protected $testableFeatures = array(
3127
'choice_attr',
3228
);
3329

30+
private $environment;
31+
3432
protected function setUp()
3533
{
3634
parent::setUp();
@@ -40,27 +38,23 @@ protected function setUp()
4038
__DIR__.'/Fixtures/templates/form',
4139
));
4240

43-
$environment = new \Twig_Environment($loader, array('strict_variables' => true));
44-
$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());
4544

4645
$rendererEngine = new TwigRendererEngine(array(
4746
'bootstrap_3_layout.html.twig',
4847
'custom_widgets.html.twig',
49-
), $environment);
48+
), $this->environment);
5049
$renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'));
5150

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

63-
$this->extension = null;
57+
$this->environment->registerExtensionRuntimeLoader($loader);
6458
}
6559

6660
public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
@@ -89,12 +83,12 @@ public function testStartTagHasActionAttributeWhenActionIsZero()
8983

9084
protected function renderForm(FormView $view, array $vars = array())
9185
{
92-
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
86+
return (string) $this->environment->getRuntimeExtension('form')->renderer->renderBlock($view, 'form', $vars);
9387
}
9488

9589
protected function renderEnctype(FormView $view)
9690
{
97-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'enctype');
91+
return (string) $this->environment->getRuntimeExtension('form')->renderer->searchAndRenderBlock($view, 'enctype');
9892
}
9993

10094
protected function renderLabel(FormView $view, $label = null, array $vars = array())
@@ -103,41 +97,41 @@ protected function renderLabel(FormView $view, $label = null, array $vars = arra
10397
$vars += array('label' => $label);
10498
}
10599

106-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'label', $vars);
100+
return (string) $this->environment->getRuntimeExtension('form')->renderer->searchAndRenderBlock($view, 'label', $vars);
107101
}
108102

109103
protected function renderErrors(FormView $view)
110104
{
111-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'errors');
105+
return (string) $this->environment->getRuntimeExtension('form')->renderer->searchAndRenderBlock($view, 'errors');
112106
}
113107

114108
protected function renderWidget(FormView $view, array $vars = array())
115109
{
116-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars);
110+
return (string) $this->environment->getRuntimeExtension('form')->renderer->searchAndRenderBlock($view, 'widget', $vars);
117111
}
118112

119113
protected function renderRow(FormView $view, array $vars = array())
120114
{
121-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'row'< F7E5 /span>, $vars);
115+
return (string) $this->environment->getRuntimeExtension('form')->renderer->searchAndRenderBlock($view, 'row', $vars);
122116
}
123117

124118
protected function renderRest(FormView $view, array $vars = array())
125119
{
126-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars);
120+
return (string) $this->environment->getRuntimeExtension('form')->renderer->searchAndRenderBlock($view, 'rest', $vars);
127121
}
128122

129123
protected function renderStart(FormView $view, array $vars = array())
130124
{
131-
return (string) $this->extension->renderer->renderBlock($view, 'form_start', $vars);
125+
return (string) $this->environment->getRuntimeExtension('form')->renderer->renderBlock($view, 'form_start', $vars);
132126
}
133127

134128
protected function renderEnd(FormView $view, array $vars = array())
135129
{
136-
return (string) $this->extension->renderer->renderBlock($view, 'form_end', $vars);
130+
return (string) $this->environment->getRuntimeExtension('form')->renderer->renderBlock($view, 'form_end', $vars);
137131
}
138132

139133
protected function setTheme(FormView $view, array $themes)
140134
{
141-
$this->extension->renderer->setTheme($view, $themes);
135+
$this->environment->getRuntimeExtension('form')->renderer->setTheme($view, $themes);
142136
}
143137
}

0 commit comments

Comments
 (0)
0