8000 [TwigBridge] decoupled Form extension definitions from its runtime · symfony/symfony@c07fc58 · GitHub
[go: up one dir, main page]

Skip to content

Commit c07fc58

Browse files
committed
[TwigBridge] decoupled Form extension definitions from its runtime
1 parent 8de92ab commit c07fc58

File tree

6 files changed

+119
-162
lines changed

6 files changed

+119
-162
lines changed

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

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function getFunctions()
6969
new \Twig_SimpleFunction('form', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
7070
new \Twig_SimpleFunction('form_start', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
7171
new \Twig_SimpleFunction('form_end', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
72-
new \Twig_SimpleFunction('csrf_token', array($this, 'renderCsrfToken')),
72+
new \Twig_SimpleFunction('csrf_token', array('Symfony\Bridge\Twig\Form\TwigRenderer', 'renderCsrfToken')),
7373
);
7474
}
7575

@@ -79,7 +79,7 @@ public function getFunctions()
7979
public function getFilters()
8080
{
8181
return array(
82-
new \Twig_SimpleFilter('humanize', array($this, 'humanize')),
82+
new \Twig_SimpleFilter('humanize', array('Symfony\Bridge\Twig\Form\TwigRenderer', 'humanize')),
8383
);
8484
}
8585

@@ -89,67 +89,35 @@ public function getFilters()
8989
public function getTests()
9090
{
9191
return array(
92-
new \Twig_SimpleTest('selectedchoice', array($this, 'isSelectedChoice')),
92+
new \Twig_SimpleTest('selectedchoice', 'Symfony\Bridge\Twig\Extension\twig_is_selected_choice'),
9393
);
9494
}
9595

9696
/**
9797
* {@inheritdoc}
9898
*/
99-
public function renderCsrfToken($tokenId)
100-
{
101-
return $this->renderer->renderCsrfToken($tokenId);
102-
}
103-
104-
/**
105-
* Makes a technical name human readable.
106-
*
107-
* @param string $text The text to humanize
108-
*
109-
* @return string The humanized text
110-
*/
111-
public function humanize($text)
99+
public function getName()
112100
{
113-
return $this->renderer->humanize($text);
101+
return 'form';
114102
}
103+
}
115104

116-
/**
117-
* Returns whether a choice is selected for a given form value.
118-
*
119-
* Unfortunately Twig does not support an efficient way to execute the
120-
* "is_selected" closure passed to the template by ChoiceType. It is faster
121-
* to implement the logic here (around 65ms for a specific form).
122-
*
123-
* Directly implementing the logic here is also faster than doing so in
124-
* ChoiceView (around 30ms).
125-
*
126-
* The worst option tested so far is to implement the logic in ChoiceView
127-
* and access the ChoiceView method directly in the template. Doing so is
128-
* around 220ms slower than doing the method call here in the filter. Twig
129-
* seems to be much more efficient at executing filters than at executing
130-
* methods of an object.
131-
*
132-
* @param ChoiceView $choice The choice to check
133-
* @param string|array $selectedValue The selected value to compare
134-
*
135-
* @return bool Whether the choice is selected
136-
*
137-
* @see ChoiceView::isSelected()
138-
*/
139-
public function isSelectedChoice(ChoiceView $choice, $selectedValue)
140-
{
141-
if (is_array($selectedValue)) {
142-
return in_array($choice->value, $selectedValue, true);
143-
}
144-
145-
return $choice->value === $selectedValue;
105+
/**
106+
* Returns whether a choice is selected for a given form value.
107+
*
108+
* This is a function and not callable due to performance reasons.
109+
*
110+
* @param string|array $selectedValue The selected value to compare
111+
*
112+
* @return bool Whether the choice is selected
113+
*
114+
* @see ChoiceView::isSelected()
115+
*/
116+
function twig_is_selected_choice(ChoiceView $choice, $selectedValue)
117+
{
118+
if (is_array($selectedValue)) {
119+
return in_array($choice->value, $selectedValue, true);
146120
}
147121

148-
/**
149-
* {@inheritdoc}
150-
*/
151-
public function getName()
152-
{
153-
return 'form';
154-
}
122+
return $choice->value === $selectedValue;
155123
}

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

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@
2222

2323
class FormExtensionBootstrap3HorizontalLayoutTest extends AbstractBootstrap3HorizontalLayoutTest
2424
{
25-
/**
26-
* @var FormExtension
27-
*/
28-
protected $extension;
25+
use RuntimeLoaderProvider;
2926

3027
protected $testableFeatures = array(
3128
'choice_attr',
3229
);
3330

31+
private $renderer;
32+
3433
protected function setUp()
3534
{
3635
parent::setUp();
@@ -39,9 +38,8 @@ protected function setUp()
3938
'bootstrap_3_horizontal_layout.html.twig',
4039
'custom_widgets.html.twig',
4140
));
42-
$renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'));
43-
44-
$this->extension = new FormExtension($renderer);
41+
$this->renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'));
42+
$extension = new FormExtension($this->renderer);
4543

4644
$loader = new StubFilesystemLoader(array(
4745
__DIR__.'/../../Resources/views/Form',
@@ -50,21 +48,14 @@ protected function setUp()
5048

5149
$environment = new \Twig_Environment($loader, array('strict_variables' => true));
5250
$environment->addExtension(new TranslationExtension(new StubTranslator()));
53-
$environment->addExtension($this->extension);
54-
55-
$this->extension->initRuntime($environment);
56-
}
57-
58-
protected function tearDown()
59-
{
60-
parent::tearDown();
61-
62-
$this->extension = null;
51+
$environment->addExtension($extension);
52+
$extension->initRuntime($environment);
53+
$this->registerTwigRuntimeLoader($environment, $this->renderer);
6354
}
6455

6556
protected function renderForm(FormView $view, array $vars = array())
6657
{
67-
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
58+
return (string) $this->renderer->renderBlock($view, 'form', $vars);
6859
}
6960

7061
protected function renderLabel(FormView $view, $label = null, array $vars = array())
@@ -73,41 +64,41 @@ protected function renderLabel(FormView $view, $label = null, array $vars = arra
7364
$vars += array('label' => $label);
7465
}
7566

76-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'label', $vars);
67+
return (string) $this->renderer->searchAndRenderBlock($view, 'label', $vars);
7768
}
7869

7970
protected function renderErrors(FormView $view)
8071
{
81-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'errors');
72+
return (string) $this->renderer->searchAndRenderBlock($view, 'errors');
8273
}
8374

8475
protected function renderWidget(FormView $view, array $vars = array())
8576
{
86-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars);
77+
return (string) $this->renderer->searchAndRenderBlock($view, 'widget', $vars);
8778
}
8879

8980
protected function renderRow(FormView $view, array $vars = array())
9081
{
91-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'row', $vars);
82+
return (string) $this->renderer->searchAndRenderBlock($view, 'row', $vars);
9283
}
9384

9485
protected function renderRest(FormView $view, array $vars = array())
9586
{
96-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars);
87+
return (string) $this->renderer->searchAndRenderBlock($view, 'rest', $vars);
9788
}
9889

9990
protected function renderStart(FormView $view, array $vars = array())
10091
{
101-
return (string) $this->extension->renderer->renderBlock($view, 'form_start', $vars);
92+
return (string) $this->renderer->renderBlock($view, 'form_start', $vars);
10293
}
10394

10495
protected function renderEnd(FormView $view, array $vars = array())
10596
{
106-
return (string) $this->extension->renderer->renderBlock($view, 'form_end', $vars);
97+
return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
10798
}
10899

109100
protected function setTheme(FormView $view, array $themes)
110101
{
111-
$this->extension->renderer->setTheme($view, $themes);
102+
$this->renderer->setTheme($view, $themes);
112103
}
113104
}

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

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222

2323
class FormExtensionBootstrap3LayoutTest extends AbstractBootstrap3LayoutTest
2424
{
25-
/**
26-
* @var FormExtension
27-
*/
28-
protected $extension;
25+
use RuntimeLoaderProvider;
26+
27+
private $renderer;
2928

3029
protected function setUp()
3130
{
@@ -35,9 +34,8 @@ protected function setUp()
3534
'bootstrap_3_layout.html.twig',
3635
'custom_widgets.html.twig',
3736
));
38-
$renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'));
39-
40-
$this->extension = new FormExtension($renderer);
37+
$this->renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'));
38+
$extension = new FormExtension($this->renderer);
4139

4240
$loader = new StubFilesystemLoader(array(
4341
__DIR__.'/../../Resources/views/Form',
@@ -46,16 +44,9 @@ protected function setUp()
4644

4745
$environment = new \Twig_Environment($loader, array('strict_variables' => true));
4846
$environment->addExtension(new TranslationExtension(new StubTranslator()));
49-
$environment->addExtension($this->extension);
50-
51-
$this->extension->initRuntime($environment);
52-
}
53-
54-
protected function tearDown()
55-
{
56-
parent::tearDown();
57-
58-
$this->extension = null;
47+
$environment->addExtension($extension);
48+
$extension->initRuntime($environment);
49+
$this->registerTwigRuntimeLoader($environment, $this->renderer);
5950
}
6051

6152
public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
@@ -84,7 +75,7 @@ public function testStartTagHasActionAttributeWhenActionIsZero()
8475

8576
protected function renderForm(FormView $view, array $vars = array())
8677
{
87-
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
78+
return (string) $this->renderer->renderBlock($view, 'form', $vars);
8879
}
8980

9081
protected function renderLabel(FormView $view, $label = null, array $vars = array())
@@ -93,41 +84,41 @@ protected function renderLabel(FormView $view, $label = null, array $vars = arra
9384
$vars += array('label' => $label);
9485
}
9586

96-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'label', $vars);
87+
return (string) $this->renderer->searchAndRenderBlock($view, 'label', $vars);
9788
}
9889

9990
protected function renderErrors(FormView $view)
10091
{
101-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'errors');
92+
return (string) $this->renderer->searchAndRenderBlock($view, 'errors');
10293
}
10394

10495
protected function renderWidget(FormView $view, array $vars = array())
10596
{
106-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars);
97+
return (string) $this->renderer->searchAndRenderBlock($view, 'widget', $vars);
10798
}
10899

109100
protected function renderRow(FormView $view, array $vars = array())
110101
{
111-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'row', $vars);
102+
return (string) $this->renderer->searchAndRenderBlock($view, 'row', $vars);
112103
}
113104

114105
protected function renderRest(FormView $view, array $vars = array())
115106
{
116-
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars);
107+
return (string) $this->renderer->searchAndRenderBlock($view, 'rest', $vars);
117108
}
118109

119110
protected function renderStart(FormView $view, array $vars = array())
120111
{
121-
return (string) $this->extension->renderer->renderBlock($view, 'form_start', $vars);
112+
return (string) $this->renderer->renderBlock($view, 'form_start', $vars);
122113
}
123114

124115
protected function renderEnd(FormView $view, array $vars = array())
125116
{
126-
return (string) $this->extension->renderer->renderBlock($view, 'form_end', $vars);
117+
return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
127118
}
128119

129120
protected function setTheme(FormView $view, array $themes)
130121
{
131-
$this->extension->renderer->setTheme($view, $themes);
122+
$this->renderer->setTheme($view, $themes);
132123
}
133124
}

0 commit comments

Comments
 (0)
0