reduced the number of times error information should be guessed

This commit is contained in:
Fabien Potencier
2015-01-19 03:08:49 +01:00
parent 6b434f4521
commit 651613c32c
8 changed files with 74 additions and 40 deletions
+5 -1
View File
@@ -28,9 +28,13 @@ class Twig_Node_Embed extends Twig_Node_Include
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(")")
;
+5 -1
View File
@@ -39,8 +39,12 @@ class Twig_Node_Import extends Twig_Node
$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(")")
;
}
+8 -5
View File
@@ -60,12 +60,15 @@ class Twig_Node_Include extends Twig_Node implements Twig_NodeOutputInterface
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)
+21 -14
View File
@@ -114,8 +114,12 @@ class Twig_Node_Module extends Twig_Node
$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(")")
;
}
@@ -155,19 +159,13 @@ class Twig_Node_Module extends Twig_Node
} 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")
;
}
@@ -395,8 +393,12 @@ class Twig_Node_Module extends Twig_Node
{
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 {
@@ -407,7 +409,12 @@ class Twig_Node_Module extends Twig_Node
->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")
;
+25 -1
View File
@@ -78,7 +78,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
}
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);
@@ -240,6 +240,30 @@ abstract class Twig_Template implements Twig_TemplateInterface
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.
*
+1 -1
View File
@@ -31,7 +31,7 @@ class Twig_Tests_Node_ImportTest extends Twig_Test_NodeTestCase
$tests[] = array($node, <<<EOF
// line 1
\$context["macro"] = \$this->env->loadTemplate("foo.twig");
\$context["macro"] = \$this->loadTemplate("foo.twig", null, 1);
EOF
);
+5 -5
View File
@@ -34,7 +34,7 @@ class Twig_Tests_Node_IncludeTest extends Twig_Test_NodeTestCase
$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
);
@@ -47,7 +47,7 @@ EOF
$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
);
@@ -56,14 +56,14 @@ EOF
$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
);
@@ -71,7 +71,7 @@ EOF
$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
}
+4 -12
View File
@@ -94,15 +94,7 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863
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(
);
}
@@ -115,7 +107,7 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863
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));
}
@@ -132,7 +124,7 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863
public function getDebugInfo()
{
return array ( 34 => 1, 32 => 2, 11 => 1,);
return array ( 26 => 1, 24 => 2, 11 => 1,);
}
}
EOF
@@ -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())