8000 removed the form runtime class as it's not needed · symfony/symfony@53cb4b6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 53cb4b6

Browse files
committed
removed the form runtime class as it's not needed
1 parent 0b9457d commit 53cb4b6

11 files changed

+83
-147
lines changed

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

Lines changed: 0 additions & 92 deletions
This file was deleted.

src/Symfony/Bridge/Twig/Form/TwigRenderer.php

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

1414
use Symfony\Component\Form\FormRenderer;
15+
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
1516

1617
/**
1718
* @author Bernhard Schussek <bschussek@gmail.com>
@@ -31,4 +32,36 @@ public function __construct(TwigRendererEngineInterface $engine, $csrfTokenManag
3132
public function setEnvironment(\Twig_Environment $environment)
3233
{
3334
}
35+
36+
/**
37+
* Returns whether a choice is selected for a given form value.
38+
*
39+
* Unfortunately Twig does not support an efficient way to execute the
40+
* "is_selected" closure passed to the template by ChoiceType. It is faster
41+
* to implement the logic here (around 65ms for a specific form).
42+
*
43+
* Directly implementing the logic here is also faster than doing so in
44+
* ChoiceView (around 30ms).
45+
*
46+
* The worst option tested so far is to implement the logic in ChoiceView
47+
* and access the ChoiceView method directly in the template. Doing so is
48+
* around 220ms slower than doing the method call here in the filter. Twig
49+
* seems to be much more efficient at executing filters than at executing
50+
* methods of an object.
51+
*
52+
* @param ChoiceView $choice The choice to check.
53+
* @param string|array $selectedValue The selected value to compare.
54+
*
55+
* @return bool Whether the choice is selected.
56+
*
57+
* @see ChoiceView::isSelected()
58+
*/
59+
public function isSelectedChoice(ChoiceView $choice, $selectedValue)
60+
{
61+
if (is_array($selectedValue)) {
62+
return in_array($choice->value, $selectedValue, true);
63+
}
64+
65+
return $choice->value === $selectedValue;
66+
}
3467
}

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->getRuntime(\'form\')->renderer->setTheme(')
33+
->write('$this->env->getRuntime(\'form\')->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->getRuntime(\'form\')->renderer->renderBlock(');
28+
$compiler->write('$this->env->getRuntime(\'form\')->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->getRuntime(\'form\')->renderer->searchAndRenderBlock(');
22+
$compiler->raw('$this->env->getRuntime(\'form\')->searchAndRenderBlock(');
2323

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

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

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

1414
use Symfony\Bridge\Twig\Extension\FormExtension;
15-
use Symfony\Bridge\Twig\Extension\FormExtensionRuntime;
1615
use Symfony\Bridge\Twig\Form\TwigRenderer;
1716
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
1817
use Symfony\Bridge\Twig\Extension\TranslationExtension;
@@ -50,7 +49,7 @@ protected function setUp()
5049

5150
$loader = $this->getMock('Twig_RuntimeLoaderInterface');
5251
$loader->expects($this->any())->method('load')->will($this->returnValueMap(array(
53-
array('form', new FormExtensionRuntime($renderer)),
52+
array('form', $renderer),
5453
array('translator', null),
5554
)));
5655

@@ -83,12 +82,12 @@ public function testStartTagHasActionAttributeWhenActionIsZero()
8382

8483
protected function renderForm(FormView $view, array $vars = array())
8584
{
86-
return (string) $this->environment->getRuntime('form')->renderer->renderBlock($view, 'form', $vars);
85+
return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form', $vars);
8786
}
8887

8988
protected function renderEnctype(FormView $view)
9089
{
91-
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'enctype');
90+
return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'enctype');
9291
}
9392

9493
protected function renderLabel(FormView $view, $label = null, array $vars = array())
@@ -97,41 +96,41 @@ protected function renderLabel(FormView $view, $label = null, array $vars = arra
9796
$vars += array('label' => $label);
9897
}
9998

100-
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'label', $vars);
99+
return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'label', $vars);
101100
}
102101

103102
protected function renderErrors(FormView $view)
104103
{
105-
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'errors');
104+
return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'errors');
106105
}
107106

108107
protected function renderWidget(FormView $view, array $vars = array())
109108
{
110-
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'widget', $vars);
109+
return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'widget', $vars);
111110
}
112111

113112
protected function renderRow(FormView $view, array $vars = array())
114113
{
115-
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'row', $vars);
114+
return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'row', $vars);
116115
}
117116

118117
protected function renderRest(FormView $view, array $vars = array())
119118
{
120-
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'rest', $vars);
119+
return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'rest', $vars);
121120
}
122121

123122
protected function renderStart(FormView $view, array $vars = array())
124123
{
125-
return (string) $this->environment->getRuntime('form')->renderer->renderBlock($view, 'form_start', $vars);
124+
return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form_start', $vars);
126125
}
127126

128127
protected function renderEnd(FormView $view, array $vars = array())
129128
{
130-
return (string) $this->environment->getRuntime('form')->renderer->renderBlock($view, 'form_end', $vars);
129+
return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form_end', $vars);
131130
}
132131

133132
protected function setTheme(FormView $view, array $themes)
134133
{
135-
$this->environment->getRuntime('form')->renderer->setTheme($view, $themes);
134+
$this->environment->getRuntime('form')->setTheme($view, $themes);
136135
}
137136
}

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

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

1414
use Symfony\Bridge\Twig\Extension\FormExtension;
15-
use Symfony\Bridge\Twig\Extension\FormExtensionRuntime;
1615
use Symfony\Bridge\Twig\Form\TwigRenderer;
1716
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
1817
use Symfony\Bridge\Twig\Extension\TranslationExtension;
@@ -54,7 +53,7 @@ protected function setUp()
5453

5554
$loader = $this->getMock('Twig_RuntimeLoaderInterface');
5655
$loader->expects($this->any())->method('load')->will($this->returnValueMap(array(
57-
array('form', new FormExtensionRuntime($renderer)),
56+
array('form', $renderer),
5857
array('translator', null),
5958
)));
6059

@@ -98,7 +97,7 @@ public function testThemeBlockInheritanceUsingDynamicExtend()
9897
->createView()
9998
;
10099

101-
$renderer = $this->environment->getRuntime('form')->renderer;
100+
$renderer = $this->environment->getRuntime('form');
102101
$renderer->setTheme($view, array('page_dynamic_extends.html.twig'));
103102
$renderer->searchAndRenderBlock($view, 'row');
104103
}
@@ -162,12 +161,12 @@ public function testStartTagHasActionAttributeWhenActionIsZero()
162161

163162
protected function renderForm(FormView $view, array $vars = array())
164163
{
165-
return (string) $this->environment->getRuntime('form')->renderer->renderBlock($view, 'form', $vars);
164+
return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form', $vars);
166165
}
167166

168167
protected function renderEnctype(FormView $view)
169168
{
170-
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'enctype');
169+
return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'enctype');
171170
}
172171

173172
protected function renderLabel(FormView $view, $label = null, array $vars = array())
@@ -176,42 +175,42 @@ protected function renderLabel(FormView $view, $label = null, array $vars = arra
176175
$vars += array('label' => $label);
177176
}
178177

179-
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'label', $vars);
178+
return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'label', $vars);
180179
}
181180

182181
protected function renderErrors(FormView $view)
183182
{
184-
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'errors');
183+
return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'errors');
185184
}
186185

187186
protected function renderWidget(FormView $view, array $vars = array())
188187
{
189-
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'widget', $vars);
188+
return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'widget', $vars);
190189
}
191190

192191
protected function renderRow(FormView $view, array $vars = array())
193192
{
194-
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'row', $vars);
193+
return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'row', $vars);
195194
}
196195

197196
protected function renderRest(FormView $view, array $vars = array())
198197
{
199-
return (string) $this->environment->getRuntime('form')->renderer->searchAndRenderBlock($view, 'rest', $vars);
198+
return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'rest', $vars);
200199
}
201200

202201
protected function renderStart(FormView $view, array $vars = array())
203202
{
204-
return (string) $this->environment->getRuntime('form')->renderer->renderBlock($view, 'form_start', $vars);
203+
return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form_start', $vars);
205204
}
206205

207206
protected function renderEnd(FormView $view, array $vars = array())
208207
{
209-
return (string) $this->environment->getRuntime('form')->renderer->renderBlock($view, 'form_end', $vars);
208+
return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form_end', $vars);
210209
}
211210

212211
protected function setTheme(FormView $view, array $themes)
213212
{
214-
$this->environment->getRuntime('form')->renderer->setTheme($view, $themes);
213+
$this->environment->getRuntime('form')->setTheme($view, $themes);
215214
}
216215

217216
public static function themeBlockInheritanceProvider()

0 commit comments

Comments
 (0)
0