From: Fabien Potencier Date: Sat, 27 Nov 2010 10:15:27 +0000 (+0100) Subject: made error message for strict variables more useful X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=c66ab823541647d562eda81457c7e52a866fb7e9;p=web%2Fkonrad%2Ftwig.git made error message for strict variables more useful --- diff --git a/CHANGELOG b/CHANGELOG index 771243a..4ca63c0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -15,6 +15,7 @@ Backward incompatibilities: * removed support for {{ 1 < i < 3 }} (use {{ i > 1 and i < 3 }} instead) Changes: + * made error message for strict variables more useful * changed trans tag to accept any variable for the plural count * fixed sandbox mode (__toString() method check was not enforced if called implicitly from complex statements) * added the ** (power) operator diff --git a/lib/Twig/Node/Expression/Name.php b/lib/Twig/Node/Expression/Name.php index 37cd727..ed3fe60 100644 --- a/lib/Twig/Node/Expression/Name.php +++ b/lib/Twig/Node/Expression/Name.php @@ -25,7 +25,7 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression } elseif ('_charset' === $this->getAttribute('name')) { $compiler->raw('$this->getEnvironment()->getCharset()'); } elseif ($compiler->getEnvironment()->isStrictVariables()) { - $compiler->raw(sprintf('$this->getContext($context, \'%s\')', $this->getAttribute('name'), $this->getAttribute('name'))); + $compiler->raw(sprintf('$this->getContext($context, \'%s\', \'%s\')', $this->getAttribute('name'), $this->lineno)); } else { $compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $this->getAttribute('name'), $this->getAttribute('name'))); } diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php index 8fcc2f2..aef28fb 100644 --- a/lib/Twig/Node/Module.php +++ b/lib/Twig/Node/Module.php @@ -53,6 +53,8 @@ class Twig_Node_Module extends Twig_Node $this->compileMacros($compiler); + $this->compileGetTemplateName($compiler); + $this->compileClassFooter($compiler); } @@ -185,4 +187,17 @@ class Twig_Node_Module extends Twig_Node { $compiler->subcompile($this->getNode('macros')); } + + protected function compileGetTemplateName($compiler) + { + $compiler + ->write("public function getTemplateName()\n", "{\n") + ->indent() + ->write('return ') + ->repr($this->getAttribute('filename')) + ->raw(";\n") + ->outdent() + ->write("}\n") + ; + } } diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 3fad2f8..c761685 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -22,6 +22,11 @@ abstract class Twig_Template implements Twig_TemplateInterface $this->blocks = array(); } + public function getTemplateName() + { + return 'n/a'; + } + public function getEnvironment() { return $this->env; @@ -87,10 +92,10 @@ abstract class Twig_Template implements Twig_TemplateInterface return ob_get_clean(); } - protected function getContext($context, $item) + protected function getContext($context, $item, $line = -1) { if (!array_key_exists($item, $context)) { - throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist.', $item)); + throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist in "%s" at line %s.', $item, $this->getTemplateName(), $line)); } return $context[$item]; diff --git a/test/Twig/Tests/Node/Expression/NameTest.php b/test/Twig/Tests/Node/Expression/NameTest.php index 3d8e03a..70b6654 100644 --- a/test/Twig/Tests/Node/Expression/NameTest.php +++ b/test/Twig/Tests/Node/Expression/NameTest.php @@ -41,7 +41,7 @@ class Twig_Tests_Node_Expression_NameTest extends Twig_Tests_Node_TestCase $env = new Twig_Environment(null, array('strict_variables' => true)); return array( - array($node, '$this->getContext($context, \'foo\')', $env), + array($node, '$this->getContext($context, \'foo\', \'0\')', $env), array($node, '(isset($context[\'foo\']) ? $context[\'foo\'] : null)'), array($self, '$this'), array($context, '$context'), diff --git a/test/Twig/Tests/Node/ModuleTest.php b/test/Twig/Tests/Node/ModuleTest.php index 9c2bd88..edf9d2c 100644 --- a/test/Twig/Tests/Node/ModuleTest.php +++ b/test/Twig/Tests/Node/ModuleTest.php @@ -72,6 +72,10 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template echo "foo"; } + public function getTemplateName() + { + return "foo.twig"; + } } EOF , $twig); @@ -105,6 +109,10 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks)); } + public function getTemplateName() + { + return "foo.twig"; + } } EOF , $twig); @@ -143,6 +151,10 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks)); } + public function getTemplateName() + { + return "foo.twig"; + } } EOF , $twig); diff --git a/test/Twig/Tests/Node/SandboxedModuleTest.php b/test/Twig/Tests/Node/SandboxedModuleTest.php index 91e54ae..b8a8196 100644 --- a/test/Twig/Tests/Node/SandboxedModuleTest.php +++ b/test/Twig/Tests/Node/SandboxedModuleTest.php @@ -78,6 +78,10 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template ); } + public function getTemplateName() + { + return "foo.twig"; + } } EOF , $twig); @@ -122,6 +126,10 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template \$this->parent->checkSecurity(); } + public function getTemplateName() + { + return "foo.twig"; + } } EOF , $twig);