Skip to content

Commit

Permalink
[FrameworkBundle][Form] Adding a cache to FormHelper::lookupTemplate()
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Jun 22, 2011
1 parent f39ce67 commit 1cb2129
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php
Expand Up @@ -35,6 +35,8 @@ class FormHelper extends Helper

protected $themes;

protected $templates;

/**
* Constructor;
*
Expand All @@ -46,7 +48,8 @@ public function __construct(EngineInterface $engine, array $resources = array())
$this->engine = $engine;
$this->varStack = array();
$this->context = array();
$this->themes = new \SplObjectStorage();
$this->templates = array();
$this->themes = array();

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

Expand All @@ -72,7 +75,8 @@ public function isChoiceSelected(FormView $view, $choice)
*/
public function setTheme(FormView $view, $themes)
{
$this->themes[$view] = (array) $themes;
$this->themes[$view->get('id')] = (array) $themes;
$this->templates = array();
}

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

public function getName()
{
return 'form';
}

/**
* Returns the name of the template to use to render the block
*
* @param FormView $view The form view
* @param string $blockName The name of the block
* @param FormView $view The form view
* @param string $block The name of the block
*
* @return string|Boolean The template logical name or false when no template is found
*/
protected function lookupTemplate(FormView $view, $block)
{

$file = $block.'.html.php';
$id = $view->get('id');

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

if ($this->themes->contains($view)) {
$themes = array_merge($themes, $this->themes[$view]);
}
$themes = $view->hasParent() ? array() : $this->resources;

for ($i = count($themes) - 1; $i >= 0; --$i) {
if (isset($this->themes[$id])) {
$themes = array_merge($themes, $this->themes[$id]);
}

$template = $themes[$i].':'.$file;
for ($i = count($themes) - 1; $i >= 0; --$i) {
if ($this->engine->exists($templateName = $themes[$i].':'.$file)) {
$template = $templateName;
break;
}
}

if ($this->engine->exists($template)) {
return $template;
if (false === $template && $view->hasParent()) {
$template = $this->lookupTemplate($view->getParent(), $block);
}
}

if ($view->hasParent()) {
return $this->lookupTemplate($view->getParent(), $block);
$this->templates[$id][$block] = $template;
}

return false;
}

public function getName()
{
return 'form';
return $this->templates[$id][$block];
}
}

0 comments on commit 1cb2129

Please sign in to comment.