moved Template::getParent() logic to the base class

This commit is contained in:
Fabien Potencier
2011-08-06 09:09:18 +02:00
parent 46eccb9ab5
commit c201137f21
5 changed files with 43 additions and 51 deletions
+9 -17
View File
@@ -62,28 +62,20 @@ class Twig_Node_Module extends Twig_Node
protected function compileGetParent(Twig_Compiler $compiler)
{
$compiler
->write("protected function doGetParent(array \$context)\n", "{\n")
->indent()
->write("return ")
;
if (null === $this->getNode('parent')) {
return;
$compiler->raw("false");
} else {
$compiler->subcompile($this->getNode('parent'));
}
$compiler
->write("public function getParent(array \$context)\n", "{\n")
->indent()
->write("\$parent = ")
->subcompile($this->getNode('parent'))
->raw(";\n")
->write("if (\$parent instanceof Twig_Template) {\n")
->indent()
->write("\$name = \$parent->getTemplateName();\n")
->write("\$this->parent[\$name] = \$parent;\n")
->write("\$parent = \$name;\n")
->outdent()
->write("} elseif (!isset(\$this->parent[\$parent])) {\n")
->indent()
->write("\$this->parent[\$parent] = \$this->env->loadTemplate(\$parent);\n")
->outdent()
->write("}\n\n")
->write("return \$this->parent[\$parent];\n")
->outdent()
->write("}\n\n")
;
+14 -1
View File
@@ -61,9 +61,22 @@ abstract class Twig_Template implements Twig_TemplateInterface
*/
public function getParent(array $context)
{
return false;
$parent = $this->doGetParent($context);
if (false === $parent) {
return false;
} elseif ($parent instanceof Twig_Template) {
$name = $parent->getTemplateName();
$this->parent[$name] = $parent;
$parent = $name;
} elseif (!isset($this->parent[$parent])) {
$this->parent[$parent] = $this->env->loadTemplate($parent);
}
return $this->parent[$parent];
}
abstract protected function doGetParent(array $context);
/**
* Displays a parent block.
*
+9 -22
View File
@@ -69,6 +69,11 @@ class Twig_Tests_Node_ModuleTest extends Twig_Tests_Node_TestCase
/* foo.twig */
class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
{
protected function doGetParent(array \$context)
{
return false;
}
protected function doDisplay(array \$context, array \$blocks = array())
{
\$context = array_merge(\$this->env->getGlobals(), \$context);
@@ -103,18 +108,9 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
{
protected \$parent;
public function getParent(array \$context)
protected function doGetParent(array \$context)
{
\$parent = "layout.twig";
if (\$parent instanceof Twig_Template) {
\$name = \$parent->getTemplateName();
\$this->parent[\$name] = \$parent;
\$parent = \$name;
} elseif (!isset(\$this->parent[\$parent])) {
\$this->parent[\$parent] = \$this->env->loadTemplate(\$parent);
}
return \$this->parent[\$parent];
return "layout.twig";
}
protected function doDisplay(array \$context, array \$blocks = array())
@@ -155,18 +151,9 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
{
protected \$parent;
public function getParent(array \$context)
protected function doGetParent(array \$context)
{
\$parent = ((true) ? ("foo") : ("foo"));
if (\$parent instanceof Twig_Template) {
\$name = \$parent->getTemplateName();
\$this->parent[\$name] = \$parent;
\$parent = \$name;
} elseif (!isset(\$this->parent[\$parent])) {
\$this->parent[\$parent] = \$this->env->loadTemplate(\$parent);
}
return \$this->parent[\$parent];
return ((true) ? ("foo") : ("foo"));
}
protected function doDisplay(array \$context, array \$blocks = array())
+7 -11
View File
@@ -67,6 +67,11 @@ class Twig_Tests_Node_SandboxedModuleTest extends Twig_Tests_Node_TestCase
/* foo.twig */
class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
{
protected function doGetParent(array \$context)
{
return false;
}
protected function doDisplay(array \$context, array \$blocks = array())
{
\$this->checkSecurity();
@@ -114,18 +119,9 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
{
protected \$parent;
public function getParent(array \$context)
protected function doGetParent(array \$context)
{
\$parent = "layout.twig";
if (\$parent instanceof Twig_Template) {
\$name = \$parent->getTemplateName();
\$this->parent[\$name] = \$parent;
\$parent = \$name;
} elseif (!isset(\$this->parent[\$parent])) {
\$this->parent[\$parent] = \$this->env->loadTemplate(\$parent);
}
return \$this->parent[\$parent];
return "layout.twig";
}
protected function doDisplay(array \$context, array \$blocks = array())
+4
View File
@@ -130,6 +130,10 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
class Twig_TemplateTest extends Twig_Template
{
protected function doGetParent(array $context)
{
}
protected function doDisplay(array $context, array $blocks = array())
{
}