Fixed guessing a template info for the Twig_Error_Loader exception

This commit is contained in:
Martin Hasoň
2014-10-10 11:56:52 +02:00
parent 5e4abadb4a
commit 8f617dc6b5
5 changed files with 74 additions and 35 deletions
+21 -8
View File
@@ -77,17 +77,18 @@ class Twig_Node_Module extends Twig_Node
protected function compileGetParent(Twig_Compiler $compiler) protected function compileGetParent(Twig_Compiler $compiler)
{ {
if (null === $this->getNode('parent')) { if (null === $parent = $this->getNode('parent')) {
return; return;
} }
$compiler $compiler
->write("protected function doGetParent(array \$context)\n", "{\n") ->write("protected function doGetParent(array \$context)\n", "{\n")
->indent() ->indent()
->addDebugInfo($parent)
->write("return ") ->write("return ")
; ;
if ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) { if ($parent instanceof Twig_Node_Expression_Constant) {
$compiler->subcompile($this->getNode('parent')); $compiler->subcompile($this->getNode('parent'));
} else { } else {
$compiler $compiler
@@ -108,8 +109,9 @@ class Twig_Node_Module extends Twig_Node
{ {
$compiler->subcompile($this->getNode('body')); $compiler->subcompile($this->getNode('body'));
if (null !== $this->getNode('parent')) { if (null !== $parent = $this->getNode('parent')) {
if ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) { $compiler->addDebugInfo($parent);
if ($parent instanceof Twig_Node_Expression_Constant) {
$compiler->write("\$this->parent"); $compiler->write("\$this->parent");
} else { } else {
$compiler->write("\$this->getParent(\$context)"); $compiler->write("\$this->getParent(\$context)");
@@ -140,13 +142,24 @@ class Twig_Node_Module extends Twig_Node
; ;
// parent // parent
if (null === $this->getNode('parent')) { if (null === $parent = $this->getNode('parent')) {
$compiler->write("\$this->parent = false;\n\n"); $compiler->write("\$this->parent = false;\n\n");
} elseif ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) { } elseif ($parent instanceof Twig_Node_Expression_Constant) {
$compiler $compiler
->addDebugInfo($parent)
->write("try {\n")
->indent()
->write("\$this->parent = \$this->env->loadTemplate(") ->write("\$this->parent = \$this->env->loadTemplate(")
->subcompile($this->getNode('parent')) ->subcompile($this->getNode('parent'))
->raw(");\n\n") ->raw(");\n")
->outdent()
->write("} catch (Twig_Error_Loader \$e) {\n")
->indent()
->write("\$e->setTemplateFile(\$this->getTemplateName());\n")
->write(sprintf("\$e->setTemplateLine(%d);\n\n", $parent->getLine()))
->write("throw \$e;\n")
->outdent()
->write("}\n\n")
; ;
} }
@@ -249,7 +262,7 @@ class Twig_Node_Module extends Twig_Node
->outdent() ->outdent()
->write(");\n") ->write(");\n")
->outdent() ->outdent()
->write("}\n\n"); ->write("}\n\n")
; ;
} }
+11 -15
View File
@@ -20,7 +20,6 @@ abstract class Twig_Template implements Twig_TemplateInterface
protected static $cache = array(); protected static $cache = array();
protected $parent; protected $parent;
protected $parents;
protected $env; protected $env;
protected $blocks; protected $blocks;
protected $traits; protected $traits;
@@ -62,22 +61,19 @@ abstract class Twig_Template implements Twig_TemplateInterface
*/ */
public function getParent(array $context) public function getParent(array $context)
{ {
if (null !== $this->parent) { if (null === $this->parent) {
return $this->parent; try {
$parent = $this->doGetParent($context);
$this->parent = false === $parent ? false : $this->env->resolveTemplate($parent);
} catch (Twig_Error_Loader $e) {
$e->setTemplateFile(null);
$e->guess();
throw $e;
}
} }
$parent = $this->doGetParent($context); return $this->parent;
if (false === $parent) {
return false;
} elseif ($parent instanceof Twig_Template) {
$name = $parent->getTemplateName();
$this->parents[$name] = $parent;
$parent = $name;
} elseif (!isset($this->parents[$parent])) {
$this->parents[$parent] = $this->env->loadTemplate($parent);
}
return $this->parents[$parent];
} }
protected function doGetParent(array $context) protected function doGetParent(array $context)
@@ -0,0 +1,8 @@
--TEST--
Exception for an undefined parent
--TEMPLATE--
{% extends 'foo.html' %}
{% set foo = "foo" %}
--EXCEPTION--
Twig_Error_Loader: Template "foo.html" is not defined in "index.twig" at line 2.
+24 -10
View File
@@ -96,7 +96,7 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863
EOF EOF
, $twig); , $twig);
$import = new Twig_Node_Import(new Twig_Node_Expression_Constant('foo.twig', 1), new Twig_Node_Expression_AssignName('macro', 1), 1); $import = new Twig_Node_Import(new Twig_Node_Expression_Constant('foo.twig', 1), new Twig_Node_Expression_AssignName('macro', 1), 2);
$body = new Twig_Node(array($import)); $body = new Twig_Node(array($import));
$extends = new Twig_Node_Expression_Constant('layout.twig', 1); $extends = new Twig_Node_Expression_Constant('layout.twig', 1);
@@ -112,7 +112,15 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863
{ {
parent::__construct(\$env); parent::__construct(\$env);
\$this->parent = \$this->env->loadTemplate("layout.twig"); // line 1
try {
\$this->parent = \$this->env->loadTemplate("layout.twig");
} catch (Twig_Error_Loader \$e) {
\$e->setTemplateFile(\$this->getTemplateName());
\$e->setTemplateLine(1);
throw \$e;
}
\$this->blocks = array( \$this->blocks = array(
); );
@@ -125,8 +133,9 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863
protected function doDisplay(array \$context, array \$blocks = array()) protected function doDisplay(array \$context, array \$blocks = array())
{ {
// line 1 // line 2
\$context["macro"] = \$this->env->loadTemplate("foo.twig"); \$context["macro"] = \$this->env->loadTemplate("foo.twig");
// line 1
\$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks)); \$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks));
} }
@@ -142,18 +151,19 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863
public function getDebugInfo() public function getDebugInfo()
{ {
return array ( 24 => 1,); return array ( 34 => 1, 32 => 2, 11 => 1,);
} }
} }
EOF EOF
, $twig); , $twig);
$body = new Twig_Node(); $set = new Twig_Node_Set(false, new Twig_Node(array(new Twig_Node_Expression_AssignName('foo', 4))), new Twig_Node(array(new Twig_Node_Expression_Constant("foo", 4))), 4);
$body = new Twig_Node(array($set));
$extends = new Twig_Node_Expression_Conditional( $extends = new Twig_Node_Expression_Conditional(
new Twig_Node_Expression_Constant(true, 1), new Twig_Node_Expression_Constant(true, 2),
new Twig_Node_Expression_Constant('foo', 1), new Twig_Node_Expression_Constant('foo', 2),
new Twig_Node_Expression_Constant('foo', 1), new Twig_Node_Expression_Constant('foo', 2),
0 2
); );
$node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, new Twig_Node(array()), $filename); $node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, new Twig_Node(array()), $filename);
@@ -165,11 +175,15 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863
{ {
protected function doGetParent(array \$context) protected function doGetParent(array \$context)
{ {
// line 2
return \$this->env->resolveTemplate(((true) ? ("foo") : ("foo"))); return \$this->env->resolveTemplate(((true) ? ("foo") : ("foo")));
} }
protected function doDisplay(array \$context, array \$blocks = array()) protected function doDisplay(array \$context, array \$blocks = array())
{ {
// line 4
\$context["foo"] = "foo";
// line 2
\$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks)); \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
} }
@@ -185,7 +199,7 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863
public function getDebugInfo() public function getDebugInfo()
{ {
return array (); return array ( 17 => 2, 15 => 4, 9 => 2,);
} }
} }
EOF EOF
+10 -2
View File
@@ -142,7 +142,15 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863
{ {
parent::__construct(\$env); parent::__construct(\$env);
\$this->parent = \$this->env->loadTemplate("layout.twig"); // line 1
try {
\$this->parent = \$this->env->loadTemplate("layout.twig");
} catch (Twig_Error_Loader \$e) {
\$e->setTemplateFile(\$this->getTemplateName());
\$e->setTemplateLine(1);
throw \$e;
}
\$this->blocks = array( \$this->blocks = array(
); );
@@ -198,7 +206,7 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863
public function getDebugInfo() public function getDebugInfo()
{ {
return array (); return array ( 11 => 1,);
} }
} }
EOF EOF