Skip to content

Commit

Permalink
Fixed guessing a template info for the Twig_Error_Loader exception
Browse files Browse the repository at this point in the history
  • Loading branch information
hason committed Oct 20, 2014
1 parent 5e4abad commit 8f617dc
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 34 deletions.
29 changes: 21 additions & 8 deletions lib/Twig/Node/Module.php
Expand Up @@ -77,17 +77,18 @@ protected function compileTemplate(Twig_Compiler $compiler)

protected function compileGetParent(Twig_Compiler $compiler)
{
if (null === $this->getNode('parent')) {
if (null === $parent = $this->getNode('parent')) {
return;
}

$compiler
->write("protected function doGetParent(array \$context)\n", "{\n")
->indent()
->addDebugInfo($parent)
->write("return ")
;

if ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) {
if ($parent instanceof Twig_Node_Expression_Constant) {
$compiler->subcompile($this->getNode('parent'));
} else {
$compiler
Expand All @@ -108,8 +109,9 @@ protected function compileDisplayBody(Twig_Compiler $compiler)
{
$compiler->subcompile($this->getNode('body'));

if (null !== $this->getNode('parent')) {
if ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) {
if (null !== $parent = $this->getNode('parent')) {
$compiler->addDebugInfo($parent);
if ($parent instanceof Twig_Node_Expression_Constant) {
$compiler->write("\$this->parent");
} else {
$compiler->write("\$this->getParent(\$context)");
Expand Down Expand Up @@ -140,13 +142,24 @@ protected function compileConstructor(Twig_Compiler $compiler)
;

// parent
if (null === $this->getNode('parent')) {
if (null === $parent = $this->getNode('parent')) {
$compiler->write("\$this->parent = false;\n\n");
} elseif ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) {
} elseif ($parent instanceof Twig_Node_Expression_Constant) {
$compiler
->addDebugInfo($parent)
->write("try {\n")
->indent()
->write("\$this->parent = \$this->env->loadTemplate(")
->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")
;
}

Expand Down Expand Up @@ -249,7 +262,7 @@ protected function compileConstructor(Twig_Compiler $compiler)
->outdent()
->write(");\n")
->outdent()
->write("}\n\n");
->write("}\n\n")
;
}

Expand Down
24 changes: 10 additions & 14 deletions lib/Twig/Template.php
Expand Up @@ -20,7 +20,6 @@ abstract class Twig_Template implements Twig_TemplateInterface
protected static $cache = array();

protected $parent;
protected $parents;
protected $env;
protected $blocks;
protected $traits;
Expand Down Expand Up @@ -62,22 +61,19 @@ public function getEnvironment()
*/
public function getParent(array $context)
{
if (null !== $this->parent) {
return $this->parent;
}
if (null === $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();

$parent = $this->doGetParent($context);
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);
throw $e;
}
}

return $this->parents[$parent];
return $this->parent;
}

protected function doGetParent(array $context)
Expand Down
8 changes: 8 additions & 0 deletions test/Twig/Tests/Fixtures/exceptions/undefined_parent.test
@@ -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.
34 changes: 24 additions & 10 deletions test/Twig/Tests/Node/ModuleTest.php
Expand Up @@ -96,7 +96,7 @@ public function getDebugInfo()
EOF
, $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));
$extends = new Twig_Node_Expression_Constant('layout.twig', 1);
Expand All @@ -112,7 +112,15 @@ public function __construct(Twig_Environment \$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(
);
Expand All @@ -125,8 +133,9 @@ protected function doGetParent(array \$context)
protected function doDisplay(array \$context, array \$blocks = array())
{
// line 1
// line 2
\$context["macro"] = \$this->env->loadTemplate("foo.twig");
// line 1
\$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks));
}
Expand All @@ -142,18 +151,19 @@ public function isTraitable()
public function getDebugInfo()
{
return array ( 24 => 1,);
return array ( 34 => 1, 32 => 2, 11 => 1,);
}
}
EOF
, $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(
new Twig_Node_Expression_Constant(true, 1),
new Twig_Node_Expression_Constant('foo', 1),
new Twig_Node_Expression_Constant('foo', 1),
0
new Twig_Node_Expression_Constant(true, 2),
new Twig_Node_Expression_Constant('foo', 2),
new Twig_Node_Expression_Constant('foo', 2),
2
);

$node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, new Twig_Node(array()), $filename);
Expand All @@ -165,11 +175,15 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863
{
protected function doGetParent(array \$context)
{
// line 2
return \$this->env->resolveTemplate(((true) ? ("foo") : ("foo")));
}
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));
}
Expand All @@ -185,7 +199,7 @@ public function isTraitable()
public function getDebugInfo()
{
return array ();
return array ( 17 => 2, 15 => 4, 9 => 2,);
}
}
EOF
Expand Down
12 changes: 10 additions & 2 deletions test/Twig/Tests/Node/SandboxedModuleTest.php
Expand Up @@ -142,7 +142,15 @@ public function __construct(Twig_Environment \$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(
);
Expand Down Expand Up @@ -198,7 +206,7 @@ public function isTraitable()
public function getDebugInfo()
{
return array ();
return array ( 11 => 1,);
}
}
EOF
Expand Down

0 comments on commit 8f617dc

Please sign in to comment.