Skip to content

Commit 1cb2129

Browse files
committed
[FrameworkBundle][Form] Adding a cache to FormHelper::lookupTemplate()
1 parent f39ce67 commit 1cb2129

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class FormHelper extends Helper
3535

3636
protected $themes;
3737

38+
protected $templates;
39+
3840
/**
3941
* Constructor;
4042
*
@@ -46,7 +48,8 @@ public function __construct(EngineInterface $engine, array $resources = array())
4648
$this->engine = $engine;
4749
$this->varStack = array();
4850
$this->context = array();
49-
$this->themes = new \SplObjectStorage();
51+
$this->templates = array();
52+
$this->themes = array();
5053

5154
$this->resources = 0 == count($resources) ? array('FrameworkBundle:Form') : $resources;
5255

@@ -72,7 +75,8 @@ public function isChoiceSelected(FormView $view, $choice)
7275
*/
7376
public function setTheme(FormView $view, $themes)
7477
{
75-
$this->themes[$view] = (array) $themes;
78+
$this->themes[$view->get('id')] = (array) $themes;
79+
$this->templates = array();
7680
}
7781

7882
/**
@@ -274,43 +278,47 @@ public function renderBlock($name, $variables = array())
274278
return $this->engine->render($template, $variables);
275279
}
276280

281+
public function getName()
282+
{
283+
return 'form';
284+
}
285+
277286
/**
278287
* Returns the name of the template to use to render the block
279288
*
280-
* @param FormView $view The form view
281-
* @param string $blockName The name of the block
289+
* @param FormView $view The form view
290+
* @param string $block The name of the block
282291
*
283292
* @return string|Boolean The template logical name or false when no template is found
284293
*/
285294
protected function lookupTemplate(FormView $view, $block)
286295
{
287-
288296
$file = $block.'.html.php';
297+
$id = $view->get('id');
289298

290-
$themes = $view->hasParent() ? array() : $this->resources;
299+
if (!isset($this->templates[$id][$block])) {
300+
$template = false;
291301

292-
if ($this->themes->contains($view)) {
293-
$themes = array_merge($themes, $this->themes[$view]);
294-
}
302+
$themes = $view->hasParent() ? array() : $this->resources;
295303

296-
for ($i = count($themes) - 1; $i >= 0; --$i) {
304+
if (isset($this->themes[$id])) {
305+
$themes = array_merge($themes, $this->themes[$id]);
306+
}
297307

298-
$template = $themes[$i].':'.$file;
308+
for ($i = count($themes) - 1; $i >= 0; --$i) {
309+
if ($this->engine->exists($templateName = $themes[$i].':'.$file)) {
310+
$template = $templateName;
311+
break;
312+
}
313+
}
299314

300-
if ($this->engine->exists($template)) {
301-
return $template;
315+
if (false === $template && $view->hasParent()) {
316+
$template = $this->lookupTemplate($view->getParent(), $block);
302317
}
303-
}
304318

305-
if ($view->hasParent()) {
306-
return $this->lookupTemplate($view->getParent(), $block);
319+
$this->templates[$id][$block] = $template;
307320
}
308321

309-
return false;
310-
}
311-
312-
public function getName()
313-
{
314-
return 'form';
322+
return $this->templates[$id][$block];
315323
}
316324
}

0 commit comments

Comments
 (0)