From c201137f2191adefc9f1d9d4ad0a8df8bda5410b Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 6 Aug 2011 09:09:18 +0200 Subject: [PATCH] moved Template::getParent() logic to the base class --- lib/Twig/Node/Module.php | 26 +++++++-------------- lib/Twig/Template.php | 15 +++++++++++- test/Twig/Tests/Node/ModuleTest.php | 31 +++++++------------------ test/Twig/Tests/Node/SandboxedModuleTest.php | 18 ++++++--------- test/Twig/Tests/TemplateTest.php | 4 +++ 5 files changed, 43 insertions(+), 51 deletions(-) diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php index 56248ea..be9d834 100644 --- a/lib/Twig/Node/Module.php +++ b/lib/Twig/Node/Module.php @@ -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") ; diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index a27d23f..3c6522a 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -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. * diff --git a/test/Twig/Tests/Node/ModuleTest.php b/test/Twig/Tests/Node/ModuleTest.php index 9c8fe85..dcaf27c 100644 --- a/test/Twig/Tests/Node/ModuleTest.php +++ b/test/Twig/Tests/Node/ModuleTest.php @@ -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()) diff --git a/test/Twig/Tests/Node/SandboxedModuleTest.php b/test/Twig/Tests/Node/SandboxedModuleTest.php index 32dae4d..d859283 100644 --- a/test/Twig/Tests/Node/SandboxedModuleTest.php +++ b/test/Twig/Tests/Node/SandboxedModuleTest.php @@ -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()) diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 0089f9b..dde383b 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -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()) { } -- 1.7.2.5