mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-13 19:06:40 +00:00
moved filters/functions/tests syntax errors to the parser
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
* 1.12.0 (2012-XX-XX)
|
||||
|
||||
* moved filters/functions/tests syntax errors to the parser
|
||||
* added support for extended ternary operator syntaxes
|
||||
|
||||
* 1.11.1 (2012-11-11)
|
||||
|
||||
@@ -330,7 +330,7 @@ class Twig_ExpressionParser
|
||||
return $node;
|
||||
}
|
||||
|
||||
$class = $this->getFunctionNodeClass($name);
|
||||
$class = $this->getFunctionNodeClass($name, $line);
|
||||
|
||||
return new $class($name, $args, $line);
|
||||
}
|
||||
@@ -378,7 +378,7 @@ class Twig_ExpressionParser
|
||||
$length = $this->parseExpression();
|
||||
}
|
||||
|
||||
$class = $this->getFilterNodeClass('slice');
|
||||
$class = $this->getFilterNodeClass('slice', $token->getLine());
|
||||
$arguments = new Twig_Node(array($arg, $length));
|
||||
$filter = new $class($node, new Twig_Node_Expression_Constant('slice', $token->getLine()), $arguments, $token->getLine());
|
||||
|
||||
@@ -419,7 +419,7 @@ class Twig_ExpressionParser
|
||||
$arguments = $this->parseArguments();
|
||||
}
|
||||
|
||||
$class = $this->getFilterNodeClass($name->getAttribute('value'));
|
||||
$class = $this->getFilterNodeClass($name->getAttribute('value'), $token->getLine());
|
||||
|
||||
$node = new $class($node, $name, $arguments, $token->getLine(), $tag);
|
||||
|
||||
@@ -483,23 +483,35 @@ class Twig_ExpressionParser
|
||||
return new Twig_Node($targets);
|
||||
}
|
||||
|
||||
protected function getFunctionNodeClass($name)
|
||||
protected function getFunctionNodeClass($name, $line)
|
||||
{
|
||||
$functionMap = $this->parser->getEnvironment()->getFunctions();
|
||||
if (isset($functionMap[$name]) && $functionMap[$name] instanceof Twig_Function_Node) {
|
||||
return $functionMap[$name]->getClass();
|
||||
$env = $this->parser->getEnvironment();
|
||||
|
||||
if (false === $function = $env->getFunction($name)) {
|
||||
$message = sprintf('The function "%s" does not exist', $name);
|
||||
if ($alternatives = $env->computeAlternatives($name, array_keys($env->getFunctions()))) {
|
||||
$message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
|
||||
}
|
||||
|
||||
throw new Twig_Error_Syntax($message, $line, $this->parser->getFilename());
|
||||
}
|
||||
|
||||
return 'Twig_Node_Expression_Function';
|
||||
return $function instanceof Twig_Function_Node ? $function->getClass() : 'Twig_Node_Expression_Function';
|
||||
}
|
||||
|
||||
protected function getFilterNodeClass($name)
|
||||
protected function getFilterNodeClass($name, $line)
|
||||
{
|
||||
$filterMap = $this->parser->getEnvironment()->getFilters();
|
||||
if (isset($filterMap[$name]) && $filterMap[$name] instanceof Twig_Filter_Node) {
|
||||
return $filterMap[$name]->getClass();
|
||||
$env = $this->parser->getEnvironment();
|
||||
|
||||
if (false === $filter = $env->getFilter($name)) {
|
||||
$message = sprintf('The filter "%s" does not exist', $name);
|
||||
if ($alternatives = $env->computeAlternatives($name, array_keys($env->getFilters()))) {
|
||||
$message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
|
||||
}
|
||||
|
||||
throw new Twig_Error_Syntax($message, $line, $this->parser->getFilename());
|
||||
}
|
||||
|
||||
return 'Twig_Node_Expression_Filter';
|
||||
return $filter instanceof Twig_Filter_Node ? $filter->getClass() : 'Twig_Node_Expression_Filter';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -270,19 +270,25 @@ class Twig_Extension_Core extends Twig_Extension
|
||||
$arguments = $parser->getExpressionParser()->parseArguments();
|
||||
}
|
||||
|
||||
$class = $this->getTestNodeClass($parser->getEnvironment(), $name);
|
||||
$class = $this->getTestNodeClass($parser, $name, $node->getLine());
|
||||
|
||||
return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine());
|
||||
}
|
||||
|
||||
protected function getTestNodeClass(Twig_Environment $env, $name)
|
||||
protected function getTestNodeClass(Twig_Parser $parser, $name, $line)
|
||||
{
|
||||
$env = $parser->getEnvironment();
|
||||
$testMap = $env->getTests();
|
||||
if (isset($testMap[$name]) && $testMap[$name] instanceof Twig_Test_Node) {
|
||||
return $testMap[$name]->getClass();
|
||||
if (!isset($testMap[$name])) {
|
||||
$message = sprintf('The test "%s" does not exist', $name);
|
||||
if ($alternatives = $env->computeAlternatives($name, array_keys($env->getTests()))) {
|
||||
$message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
|
||||
}
|
||||
|
||||
throw new Twig_Error_Syntax($message, $line, $parser->getFilename());
|
||||
}
|
||||
|
||||
return 'Twig_Node_Expression_Test';
|
||||
return $testMap[$name] instanceof Twig_Test_Node ? $testMap[$name]->getClass() : 'Twig_Node_Expression_Test';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,16 +18,7 @@ class Twig_Node_Expression_Filter extends Twig_Node_Expression
|
||||
|
||||
public function compile(Twig_Compiler $compiler)
|
||||
{
|
||||
$name = $this->getNode('filter')->getAttribute('value');
|
||||
|
||||
if (false === $filter = $compiler->getEnvironment()->getFilter($name)) {
|
||||
$message = sprintf('The filter "%s" does not exist', $name);
|
||||
if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getFilters()))) {
|
||||
$message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
|
||||
}
|
||||
|
||||
throw new Twig_Error_Syntax($message, $this->getLine(), $compiler->getFilename());
|
||||
}
|
||||
$filter = $compiler->getEnvironment()->getFilter($this->getNode('filter')->getAttribute('value'));
|
||||
|
||||
$this->compileFilter($compiler, $filter);
|
||||
}
|
||||
|
||||
@@ -17,16 +17,7 @@ class Twig_Node_Expression_Function extends Twig_Node_Expression
|
||||
|
||||
public function compile(Twig_Compiler $compiler)
|
||||
{
|
||||
$name = $this->getAttribute('name');
|
||||
|
||||
if (false === $function = $compiler->getEnvironment()->getFunction($name)) {
|
||||
$message = sprintf('The function "%s" does not exist', $name);
|
||||
if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getFunctions()))) {
|
||||
$message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
|
||||
}
|
||||
|
||||
throw new Twig_Error_Syntax($message, $this->getLine(), $compiler->getFilename());
|
||||
}
|
||||
$function = $compiler->getEnvironment()->getFunction($this->getAttribute('name'));
|
||||
|
||||
$compiler->raw($function->compile().'(');
|
||||
|
||||
|
||||
@@ -19,14 +19,6 @@ class Twig_Node_Expression_Test extends Twig_Node_Expression
|
||||
{
|
||||
$name = $this->getAttribute('name');
|
||||
$testMap = $compiler->getEnvironment()->getTests();
|
||||
if (!isset($testMap[$name])) {
|
||||
$message = sprintf('The test "%s" does not exist', $name);
|
||||
if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getTests()))) {
|
||||
$message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
|
||||
}
|
||||
|
||||
throw new Twig_Error_Syntax($message, $this->getLine(), $compiler->getFilename());
|
||||
}
|
||||
|
||||
$name = $this->getAttribute('name');
|
||||
$node = $this->getNode('node');
|
||||
|
||||
@@ -214,4 +214,40 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Twig_Error_Syntax
|
||||
* @expectedExceptionMessage The function "cycl" does not exist. Did you mean "cycle" in "index" at line 1
|
||||
*/
|
||||
public function testUnknownFunction()
|
||||
{
|
||||
$env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
|
||||
$parser = new Twig_Parser($env);
|
||||
|
||||
$parser->parse($env->tokenize('{{ cycl() }}', 'index'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Twig_Error_Syntax
|
||||
* @expectedExceptionMessage The filter "lowe" does not exist. Did you mean "lower" in "index" at line 1
|
||||
*/
|
||||
public function testUnknownFilter()
|
||||
{
|
||||
$env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
|
||||
$parser = new Twig_Parser($env);
|
||||
|
||||
$parser->parse($env->tokenize('{{ 1|lowe }}', 'index'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Twig_Error_Syntax
|
||||
* @expectedExceptionMessage The test "nul" does not exist. Did you mean "null" in "index" at line 1
|
||||
*/
|
||||
public function testUnknownTest()
|
||||
{
|
||||
$env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
|
||||
$parser = new Twig_Parser($env);
|
||||
|
||||
$parser->parse($env->tokenize('{{ 1 is nul }}', 'index'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,19 +35,6 @@ class Twig_Tests_Node_Expression_FilterTest extends Twig_Test_NodeTestCase
|
||||
parent::testCompile($node, $source, $environment);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Twig_Node_Expression_Filter::compile
|
||||
* @expectedException Twig_Error_Syntax
|
||||
* @expectedExceptionMessage The filter "lowe" does not exist. Did you mean "lower" at line 1
|
||||
*/
|
||||
public function testCompileUnknownFilter()
|
||||
{
|
||||
$expr = new Twig_Node_Expression_Constant('foo', 1);
|
||||
$node = $this->createFilter($expr, 'lowe', array(new Twig_Node_Expression_Constant('bar', 1), new Twig_Node_Expression_Constant('foobar', 1)));
|
||||
|
||||
$node->compile($this->getCompiler());
|
||||
}
|
||||
|
||||
public function getTests()
|
||||
{
|
||||
$tests = array();
|
||||
@@ -65,17 +52,6 @@ class Twig_Tests_Node_Expression_FilterTest extends Twig_Test_NodeTestCase
|
||||
return $tests;
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Twig_Node_Expression_Filter::compile
|
||||
* @expectedException Twig_Error_Syntax
|
||||
* @expectedExceptionMessage The filter "uppe" does not exist. Did you mean "upper" at line 1
|
||||
*/
|
||||
public function testUnknownFilter()
|
||||
{
|
||||
$node = $this->createFilter(new Twig_Node_Expression_Constant('foo', 1), 'uppe');
|
||||
$node->compile($this->getCompiler());
|
||||
}
|
||||
|
||||
protected function createFilter($node, $name, array $arguments = array())
|
||||
{
|
||||
$name = new Twig_Node_Expression_Constant($name, 1);
|
||||
|
||||
@@ -33,17 +33,6 @@ class Twig_Tests_Node_Expression_FunctionTest extends Twig_Test_NodeTestCase
|
||||
parent::testCompile($node, $source, $environment);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Twig_Node_Expression_Filter::compile
|
||||
* @expectedException Twig_Error_Syntax
|
||||
* @expectedExceptionMessage The function "cycl" does not exist. Did you mean "cycle" at line 1
|
||||
*/
|
||||
public function testUnknownFunction()
|
||||
{
|
||||
$node = $this->createFunction('cycl', array());
|
||||
$node->compile($this->getCompiler());
|
||||
}
|
||||
|
||||
public function getTests()
|
||||
{
|
||||
$environment = new Twig_Environment();
|
||||
|
||||
@@ -47,17 +47,6 @@ class Twig_Tests_Node_Expression_TestTest extends Twig_Test_NodeTestCase
|
||||
return $tests;
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Twig_Node_Expression_Filter::compile
|
||||
* @expectedException Twig_Error_Syntax
|
||||
* @expectedExceptionMessage The test "nul" does not exist. Did you mean "null" at line 1
|
||||
*/
|
||||
public function testUnknownTest()
|
||||
{
|
||||
$node = $this->createTest(new Twig_Node_Expression_Constant('foo', 1), 'nul');
|
||||
$node->compile($this->getCompiler());
|
||||
}
|
||||
|
||||
protected function createTest($node, $name, array $arguments = array())
|
||||
{
|
||||
return new Twig_Node_Expression_Test($node, $name, new Twig_Node($arguments), 1);
|
||||
|
||||
Reference in New Issue
Block a user