Skip to content

Commit

Permalink
reduced the number of times error information should be guessed
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Feb 27, 2015
1 parent 6b434f4 commit 651613c
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 40 deletions.
6 changes: 5 additions & 1 deletion lib/Twig/Node/Embed.php
Expand Up @@ -28,9 +28,13 @@ public function __construct($filename, $index, Twig_Node_Expression $variables =
protected function addGetTemplate(Twig_Compiler $compiler)
{
$compiler
->write("\$this->env->loadTemplate(")
->write("\$this->loadTemplate(")
->string($this->getAttribute('filename'))
->raw(', ')
->repr($compiler->getFilename())
->raw(', ')
->repr($this->getLine())
->raw(', ')
->string($this->getAttribute('index'))
->raw(")")
;
Expand Down
6 changes: 5 additions & 1 deletion lib/Twig/Node/Import.php
Expand Up @@ -39,8 +39,12 @@ public function compile(Twig_Compiler $compiler)
$compiler->raw("\$this");
} else {
$compiler
->raw('$this->env->loadTemplate(')
->raw('$this->loadTemplate(')
->subcompile($this->getNode('expr'))
->raw(', ')
->repr($compiler->getFilename())
->raw(', ')
->repr($this->getLine())
->raw(")")
;
}
Expand Down
13 changes: 8 additions & 5 deletions lib/Twig/Node/Include.php
Expand Up @@ -60,12 +60,15 @@ public function compile(Twig_Compiler $compiler)

protected function addGetTemplate(Twig_Compiler $compiler)
{
$method = $this->getNode('expr') instanceof Twig_Node_Expression_Constant ? 'loadTemplate' : 'resolveTemplate';
$compiler
->write(sprintf('$this->env->%s(', $method))
->subcompile($this->getNode('expr'))
->raw(')')
;
->write("\$this->loadTemplate(")
->subcompile($this->getNode('expr'))
->raw(', ')
->repr($compiler->getFilename())
->raw(', ')
->repr($this->getLine())
->raw(")")
;
}

protected function addTemplateArguments(Twig_Compiler $compiler)
Expand Down
35 changes: 21 additions & 14 deletions lib/Twig/Node/Module.php
Expand Up @@ -114,8 +114,12 @@ protected function compileGetParent(Twig_Compiler $compiler)
$compiler->subcompile($parent);
} else {
$compiler
->raw("\$this->env->resolveTemplate(")
->raw("\$this->loadTemplate(")
->subcompile($parent)
->raw(', ')
->repr($compiler->getFilename())
->raw(', ')
->repr($this->getNode('parent')->getLine())
->raw(")")
;
}
Expand Down Expand Up @@ -155,19 +159,13 @@ protected function compileConstructor(Twig_Compiler $compiler)
} elseif ($parent instanceof Twig_Node_Expression_Constant) {
$compiler
->addDebugInfo($parent)
->write("try {\n")
->indent()
->write("\$this->parent = \$this->env->loadTemplate(")
->write("\$this->parent = \$this->loadTemplate(")
->subcompile($parent)
->raw(', ')
->repr($compiler->getFilename())
->raw(', ')
->repr($this->getNode('parent')->getLine())
->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 @@ -395,8 +393,12 @@ protected function compileLoadTemplate(Twig_Compiler $compiler, $node, $var)
{
if ($node instanceof Twig_Node_Expression_Constant) {
$compiler
->write(sprintf("%s = \$this->env->loadTemplate(", $var))
->write(sprintf("%s = \$this->loadTemplate(", $var))
->subcompile($node)
->raw(', ')
->repr($compiler->getFilename())
->raw(', ')
->repr($node->getLine())
->raw(");\n")
;
} else {
Expand All @@ -407,7 +409,12 @@ protected function compileLoadTemplate(Twig_Compiler $compiler, $node, $var)
->write(sprintf("if (!%s", $var))
->raw(" instanceof Twig_Template) {\n")
->indent()
->write(sprintf("%s = \$this->env->loadTemplate(%s);\n", $var, $var))
->write(sprintf("%s = \$this->loadTemplate(%s")
->raw(', ')
->repr($compiler->getFilename())
->raw(', ')
->repr($node->getLine())
->raw(");\n", $var, $var))
->outdent()
->write("}\n")
;
Expand Down
26 changes: 25 additions & 1 deletion lib/Twig/Template.php
Expand Up @@ -78,7 +78,7 @@ public function getParent(array $context)
}

if (!isset($this->parents[$parent])) {
$this->parents[$parent] = $this->env->loadTemplate($parent);
$this->parents[$parent] = $this->loadTemplate($parent);
}
} catch (Twig_Error_Loader $e) {
$e->setTemplateFile(null);
Expand Down Expand Up @@ -240,6 +240,30 @@ public function getBlockNames()
return array_keys($this->blocks);
}

protected function loadTemplate($template, $templateName = null, $line = null, $index = null)
{
try {
if (is_array($template)) {
return $this->env->resolveTemplate($template);
}

if ($template instanceof Twig_Template) {
return $template;
}

return $this->env->loadTemplate($template, $index);
} catch (Twig_Error $e) {
$e->setTemplateFile($templateName ? $templateName : $this->getTemplateName());
if (!$line) {
$e->guess();
} else {
$e->setTemplateLine($line);
}

throw $e;
}
}

/**
* Returns all blocks.
*
Expand Down
2 changes: 1 addition & 1 deletion test/Twig/Tests/Node/ImportTest.php
Expand Up @@ -31,7 +31,7 @@ public function getTests()

$tests[] = array($node, <<<EOF
// line 1
\$context["macro"] = \$this->env->loadTemplate("foo.twig");
\$context["macro"] = \$this->loadTemplate("foo.twig", null, 1);
EOF
);

Expand Down
10 changes: 5 additions & 5 deletions test/Twig/Tests/Node/IncludeTest.php
Expand Up @@ -34,7 +34,7 @@ public function getTests()
$node = new Twig_Node_Include($expr, null, false, false, 1);
$tests[] = array($node, <<<EOF
// line 1
\$this->env->loadTemplate("foo.twig")->display(\$context);
\$this->loadTemplate("foo.twig", null, 1)->display(\$context);
EOF
);

Expand All @@ -47,7 +47,7 @@ public function getTests()
$node = new Twig_Node_Include($expr, null, false, false, 1);
$tests[] = array($node, <<<EOF
// line 1
\$this->env->resolveTemplate(((true) ? ("foo") : ("foo")))->display(\$context);
\$this->loadTemplate(((true) ? ("foo") : ("foo")), null, 1)->display(\$context);
EOF
);

Expand All @@ -56,22 +56,22 @@ public function getTests()
$node = new Twig_Node_Include($expr, $vars, false, false, 1);
$tests[] = array($node, <<<EOF
// line 1
\$this->env->loadTemplate("foo.twig")->display(array_merge(\$context, array("foo" => true)));
\$this->loadTemplate("foo.twig", null, 1)->display(array_merge(\$context, array("foo" => true)));
EOF
);

$node = new Twig_Node_Include($expr, $vars, true, false, 1);
$tests[] = array($node, <<<EOF
// line 1
\$this->env->loadTemplate("foo.twig")->display(array("foo" => true));
\$this->loadTemplate("foo.twig", null, 1)->display(array("foo" => true));
EOF
);

$node = new Twig_Node_Include($expr, $vars, true, true, 1);
$tests[] = array($node, <<<EOF
// line 1
try {
\$this->env->loadTemplate("foo.twig")->display(array("foo" => true));
\$this->loadTemplate("foo.twig", null, 1)->display(array("foo" => true));
} catch (Twig_Error_Loader \$e) {
// ignore missing template
}
Expand Down
16 changes: 4 additions & 12 deletions test/Twig/Tests/Node/ModuleTest.php
Expand Up @@ -94,15 +94,7 @@ public function __construct(Twig_Environment \$env)
parent::__construct(\$env);
// 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->parent = \$this->loadTemplate("layout.twig", "foo.twig", 1);
\$this->blocks = array(
);
}
Expand All @@ -115,7 +107,7 @@ protected function doGetParent(array \$context)
protected function doDisplay(array \$context, array \$blocks = array())
{
// line 2
\$context["macro"] = \$this->env->loadTemplate("foo.twig");
\$context["macro"] = \$this->loadTemplate("foo.twig", "foo.twig", 2);
// line 1
\$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks));
}
Expand All @@ -132,7 +124,7 @@ public function isTraitable()
public function getDebugInfo()
{
return array ( 34 => 1, 32 => 2, 11 => 1,);
return array ( 26 => 1, 24 => 2, 11 => 1,);
}
}
EOF
Expand All @@ -157,7 +149,7 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863
protected function doGetParent(array \$context)
{
// line 2
return \$this->env->resolveTemplate(((true) ? ("foo") : ("foo")));
return \$this->loadTemplate(((true) ? ("foo") : ("foo")), "foo.twig", 2);
}
protected function doDisplay(array \$context, array \$blocks = array())
Expand Down

0 comments on commit 651613c

Please sign in to comment.