8000 [Form] Removed TemplateContext::create() · ivanrey/symfony@ca6ae09 · GitHub
[go: up one dir, main page]

Skip to content

Commit ca6ae09

Browse files
committed
[Form] Removed TemplateContext::create()
1 parent 4907c7d commit ca6ae09

File tree

9 files changed

+125
-157
lines changed

9 files changed

+125
-157
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ public function renderEnctype(TemplateContext $context)
100100
*/
101101
public function renderRow(TemplateContext $context, array $variables = array())
102102
{
103-
$context->setRendered();
104-
105103
return $this->render($context, 'row', $variables);
106104
}
107105

@@ -132,8 +130,6 @@ public function renderRest(TemplateContext $context, array $variables = array())
132130
*/
133131
public function renderWidget(TemplateContext $context, array $variables = array(), $resources = null)
134132
{
135-
$context->setRendered();
136-
137133
if (null !== $resources && !is_array($resources)) {
138134
$resources = array($resources);
139135
}
@@ -180,8 +176,8 @@ protected function render(TemplateContext $context, $section, array $variables =
180176
$block = $block.'__'.$section;
181177

182178
if (isset($templates[$block])) {
183-
if ('widget' === $section) {
184-
$context->setVar('is_rendered', true);
179+
if ('widget' === $section || 'row' === $section) {
180+
$context->setRendered(true);
185181
}
186182

187183
return $templates[$block]->renderBlock($block, array_merge($context->getVars(), $variables));

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper;
1919
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTemplateNameParser;
2020
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTranslator;
21-
use Symfony\Component\Form\FormInterface;
21+
use Symfony\Component\Form\TemplateContext;
2222
use Symfony\Component\Templating\PhpEngine;
2323
use Symfony\Component\Templating\TemplateNameParser;
2424
use Symfony\Component\Templating\Loader\FilesystemLoader;
@@ -45,34 +45,34 @@ protected function setUp()
4545
));
4646
}
4747

48-
protected function renderEnctype(FormInterface $form)
48+
protected function renderEnctype(TemplateContext $context)
4949
{
50-
return (string)$this->helper->enctype($form->getContext());
50+
return (string)$this->helper->enctype($context);
5151
}
5252

53-
protected function renderLabel(FormInterface $form, $label = null)
53+
protected function renderLabel(TemplateContext $context, $label = null)
5454
{
55-
return (string)$this->helper->label($form->getContext(), $label);
55+
return (string)$this->helper->label($context, $label);
5656
}
5757

58-
protected function renderErrors(FormInterface $form)
58+
protected function renderErrors(TemplateContext $context)
5959
{
60-
return (string)$this->helper->errors($form->getContext());
60+
return (string)$this->helper->errors($context);
6161
}
6262

63-
protected function renderWidget(FormInterface $form, array $vars = array())
63+
protected function renderWidget(TemplateContext $context, array $vars = array())
6464
{
65-
return (string)$this->helper->widget($form->getContext(), $vars);
65+
return (string)$this->helper->widget($context, $vars);
6666
}
6767

68-
protected function renderRow(FormInterface $form, array $vars = array())
68+
protected function renderRow(TemplateContext $context, array $vars = array())
6969
{
70-
return (string)$this->helper->row($form->getContext(), $vars);
70+
return (string)$this->helper->row($context, $vars);
7171
}
7272

73-
protected function renderRest(FormInterface $form, array $vars = array())
73+
protected function renderRest(TemplateContext $context, array $vars = array())
7474
{
75-
return (string)$this->helper->rest($form->getContext(), $vars);
75+
return (string)$this->helper->rest($context, $vars);
7676
}
7777

7878
}

src/Symfony/Component/Form/Form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ private function appToNorm($value)
834834

835835
public function getContext()
836836
{
837-
return TemplateContext::create($this);
837+
return new TemplateContext($this);
838838
}
839839

840840
/**

src/Symfony/Component/Form/TemplateContext.php

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@
1616

1717
class TemplateContext implements \ArrayAccess, \IteratorAggregate
1818
{
19-
static $cache;
20-
2119
private $vars = array(
2220
'value' => null,
23-
'choices' => array(),
24-
'preferred_choices' => array(),
2521
'attr' => array(),
2622
);
2723

@@ -40,28 +36,7 @@ class TemplateContext implements \ArrayAccess, \IteratorAggregate
4036
*/
4137
private $rendered = false;
4238

43-
static public function create(FormInterface $form)
44-
{
45-
if (null === self::$cache) {
46-
self::$cache = new \SplObjectStorage();
47-
}
48-
49-
if (isset(self::$cache[$form])) {
50-
return self::$cache[$form];
51-
}
52-
53-
// populate the cache for the root form
54-
$root = $form;
55-
while ($root->getParent()) {
56-
$root = $root->getParent();
57-
}
58-
59-
self::$cache[$root] = new self($root);
60-
61-
return self::$cache[$form];
62-
}
63-
64-
private function __construct(FormInterface $form, self $parent = null)
39+
public function __construct(FormInterface $form, self $parent = null)
6540
{
6641
$this->parent = $parent;
6742

@@ -75,7 +50,7 @@ private function __construct(FormInterface $form, self $parent = null)
7550
}
7651

7752
foreach ($form as $key => $child) {
78-
$children[$key] = self::$cache[$child] = new self($child, $this);
53+
$children[$key] = new self($child, $this);
7954
}
8055

8156
$this->setChildren($children);
@@ -185,16 +160,6 @@ public function getIterator()
185160
return new \ArrayIterator(array());
186161
}
187162

188-
public function getChoiceLabel($choice)
189-
{
190-
return isset($this->vars['choices'][$choice])
191-
? $this->vars['choices'][$choice]
192-
: (isset($this->vars['preferred_choices'][$choice])
193-
? $this->vars['preferred_choices'][$choice]
194-
: null
195-
);
196-
}
197-
198163
public function isChoiceGroup($choice)
199164
{
200165
return is_array($choice) || $choice instanceof \Traversable;

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
require_once __DIR__.'/Fixtures/StubTranslator.php';
1515
require_once __DIR__.'/Fixtures/StubFilesystemLoader.php';
1616

17-
use Symfony\Component\Form\FormInterface;
17+
use Symfony\Component\Form\TemplateContext;
1818
use Symfony\Bridge\Twig\Extension\FormExtension;
1919
use Symfony\Bridge\Twig\Extension\TranslationExtension;
2020
use Symfony\Tests\Component\Form\AbstractDivLayoutTest;
@@ -40,33 +40,33 @@ protected function setUp()
4040
$this->extension->initRuntime($environment);
4141
}
4242

43-
protected function renderEnctype(FormInterface $form)
43+
protected function renderEnctype(TemplateContext $context)
4444
{
45-
return (string)$this->extension->renderEnctype($form->getContext());
45+
return (string)$this->extension->renderEnctype($context);
4646
}
4747

48-
protected function renderLabel(FormInterface $form, $label = null)
48+
protected function renderLabel(TemplateContext $context, $label = null)
4949
{
50-
return (string)$this->extension->renderLabel($form->getContext(), $label);
50+
return (string)$this->extension->renderLabel($context, $label);
5151
}
5252

53-
protected function renderErrors(FormInterface $form)
53+
protected function renderErrors(TemplateContext $context)
5454
{
55-
return (string)$this->extension->renderErrors($form->getContext());
55+
return (string)$this->extension->renderErrors($context);
5656
}
5757

58-
protected function renderWidget(FormInterface $form, array $vars = array())
58+
protected function renderWidget(TemplateContext $context, array $vars = array())
5959
{
60-
return (string)$this->extension->renderWidget($form->getContext(), $vars);
60+
return (string)$this->extension->renderWidget($context, $vars);
6161
}
6262

63-
protected function renderRow(FormInterface $form, array $vars = array())
63+
protected function renderRow(TemplateContext $context, array $vars = array())
6464
{
65-
return (string)$this->extension->renderRow($form->getContext(), $vars);
65+
return (string)$this->extension->renderRow($context, $vars);
6666
}
6767

68-
protected function renderRest(FormInterface $form, array $vars = array())
68+
protected function renderRest(TemplateContext $context, array $vars = array())
6969
{
70-
return (string)$this->extension->renderRest($form->getContext(), $vars);
70+
return (string)$this->extension->renderRest($context, $vars);
7171
}
7272
}

tests/Symfony/Tests/Bridge/Twig/Extension/FormExtensionTableLayoutTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
require_once __DIR__.'/Fixtures/StubTranslator.php';
1515
require_once __DIR__.'/Fixtures/StubFilesystemLoader.php';
1616

17-
use Symfony\Component\Form\FormInterface;
17+
use Symfony\Component\Form\TemplateContext;
1818
use Symfony\Bridge\Twig\Extension\FormExtension;
1919
use Symfony\Bridge\Twig\Extension\TranslationExtension;
2020
use Symfony\Tests\Component\Form\AbstractTableLayoutTest;
@@ -40,33 +40,33 @@ protected function setUp()
4040
$this->extension->initRuntime($environment);
4141
}
4242

43-
protected function renderEnctype(FormInterface $form)
43+
protected function renderEnctype(TemplateContext $context)
4444
{
45-
return (string)$this->extension->renderEnctype($form->getContext());
45+
return (string)$this->extension->renderEnctype($context);
4646
}
4747

48-
protected function renderLabel(FormInterface $form, $label = null)
48+
protected function renderLabel(TemplateContext $context, $label = null)
4949
{
50-
return (string)$this->extension->renderLabel($form->getContext(), $label);
50+
return (string)$this->extension->renderLabel($context, $label);
5151
}
5252

53-
protected function renderErrors(FormInterface $form)
53+
protected function renderErrors(TemplateContext $context)
5454
{
55-
return (string)$this->extension->renderErrors($form->getContext());
55+
return (string)$this->extension->renderErrors($context);
5656
}
5757

58-
protected function renderWidget(FormInterface $form, array $vars = array())
58+
protected function renderWidget(TemplateContext $context, array $vars = array())
5959
{
60-
return (string)$this->extension->renderWidget($form->getContext(), $vars);
60+
return (string)$this->extension->renderWidget($context, $vars);
6161
}
6262

63-
protected function renderRow(FormInterface $form, array $vars = array())
63+
protected function renderRow(TemplateContext $context, array $vars = array())
6464
{
65-
return (string)$this->extension->renderRow($form->getContext(), $vars);
65+
return (string)$this->extension->renderRow($context, $vars);
6666
}
6767

68-
protected function renderRest(FormInterface $form, array $vars = array())
68+
protected function renderRest(TemplateContext $context, array $vars = array())
6969
{
70-
return (string)$this->extension->renderRest($form->getContext(), $vars);
70+
return (string)$this->extension->renderRest($context, $vars);
7171
}
7272
}

tests/Symfony/Tests/Component/Form/AbstractDivLayoutTest.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public function testRow()
1919
{
2020
$form = $this->factory->create('text', 'name');
2121
$form->addError(new FormError('Error!'));
22-
$html = $this->renderRow($form);
22+
$context = $form->getContext();
23+
$html = $this->renderRow($context);
2324

2425
$this->assertMatchesXpath($html,
2526
'/div
@@ -38,7 +39,8 @@ public function testRepeatedRow()
3839
{
3940
$form = $this->factory->create('repeated', 'name');
4041
$form->addError(new FormError('Error!'));
41-
$html = $this->renderRow($form);
42+
$context = $form->getContext();
43+
$html = $this->renderRow($context);
4244

4345
$this->assertMatchesXpath($html,
4446
'/ul
@@ -60,22 +62,23 @@ public function testRepeatedRow()
6062

6163
public function testRest()
6264
{
63-
$form = $this->factory->createBuilder('form', 'name')
65+
$context = $this->factory->createBuilder('form', 'name')
6466
->add('field1', 'text')
6567
->add('field2', 'repeated')
6668
->add('field3', 'text')
6769
->add('field4', 'text')
68-
->getForm();
70+
->getForm()
71+
->getContext();
6972

7073
// Render field2 row -> does not implicitely call renderWidget because
7174
// it is a repeated field!
72-
$this->renderRow($form['field2']);
75+
$this->renderRow($context['field2']);
7376

7477
// Render field3 widget
75-
$this->renderWidget($form['field3']);
78+
$this->renderWidget($context['field3']);
7679

7780
// Rest should only contain field1 and field4
78-
$html = $this->renderRest($form);
81+
$html = $this->renderRest($context);
7982

8083
$this->assertMatchesXpath($html,
8184
'/input
@@ -105,7 +108,7 @@ public function testCollection()
105108
'data' => array('a', 'b'),
106109
));
107110

108-
$this->assertWidgetMatchesXpath($form, array(),
111+
$this->assertWidgetMatchesXpath($form->getContext(), array(),
109112
'/div
110113
[
111114
./div[./input[@type="text"][@value="a"]]
@@ -123,7 +126,7 @@ public function testForm()
123126
->add('lastName', 'text')
124127
->getForm();
125128

126-
$this->assertWidgetMatchesXpath($form, array(),
129+
$this->assertWidgetMatchesXpath($form->getContext(), array(),
127130
'/div
128131
[
129132
./input[@type="hidden"][@id="name__token"]
@@ -150,7 +153,7 @@ public function testRepeated()
150153
'data' => 'foobar',
151154
));
152155

153-
$this->assertWidgetMatchesXpath($form, array(),
156+
$this->assertWidgetMatchesXpath($form->getContext(), array(),
154157
'/div
155158
[
156159
./div

0 commit comments

Comments
 (0)
0