deprecated Twig_Parser::getEnvironment()

This commit is contained in:
Fabien Potencier
2016-10-24 16:11:10 -07:00
parent 0d6686ec30
commit 4deb03aeb1
4 changed files with 33 additions and 16 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.27.0 (2016-XX-XX) * 1.27.0 (2016-XX-XX)
* deprecated Twig_Parser::getEnvironment()
* deprecated Twig_Parser::addHandler() and Twig_Parser::addNodeVisitor() * deprecated Twig_Parser::addHandler() and Twig_Parser::addNodeVisitor()
* deprecated Twig_Compiler::addIndentation() * deprecated Twig_Compiler::addIndentation()
* fixed regression when registering two extensions having the same class name * fixed regression when registering two extensions having the same class name
+2
View File
@@ -23,6 +23,8 @@ Token Parsers
* As of Twig 1.27, ``Twig_Parser::getFilename()`` is deprecated. From a token * As of Twig 1.27, ``Twig_Parser::getFilename()`` is deprecated. From a token
parser, use ``$this->parser->getStream()->getSourceContext()->getPath()`` instead. parser, use ``$this->parser->getStream()->getSourceContext()->getPath()`` instead.
* As of Twig 1.27, ``Twig_Parser::getEnvironment()`` is deprecated.
Extensions Extensions
---------- ----------
+24 -15
View File
@@ -19,6 +19,8 @@
* @see http://en.wikipedia.org/wiki/Operator-precedence_parser * @see http://en.wikipedia.org/wiki/Operator-precedence_parser
* *
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
*
* @internal
*/ */
class Twig_ExpressionParser class Twig_ExpressionParser
{ {
@@ -29,11 +31,23 @@ class Twig_ExpressionParser
protected $unaryOperators; protected $unaryOperators;
protected $binaryOperators; protected $binaryOperators;
public function __construct(Twig_Parser $parser, array $unaryOperators, array $binaryOperators) private $env;
public function __construct(Twig_Parser $parser, Twig_Environment $env = null)
{ {
$this->parser = $parser; $this->parser = $parser;
$this->unaryOperators = $unaryOperators;
$this->binaryOperators = $binaryOperators; if ($env instanceof Twig_Environment) {
$this->env = $env;
$this->unaryOperators = $env->getUnaryOperators();
$this->binaryOperators = $env->getBinaryOperators();
} else {
@trigger_error('Passing the operators as constructor arguments to '.__METHOD__.' is deprecated since version 1.27. Pass the environment instead.', E_USER_DEPRECATED);
$this->env = $parser->getEnvironment();
$this->unaryOperators = func_get_arg(1);
$this->binaryOperators = func_get_arg(2);
}
} }
public function parseExpression($precedence = 0) public function parseExpression($precedence = 0)
@@ -594,9 +608,8 @@ class Twig_ExpressionParser
{ {
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
$env = $this->parser->getEnvironment();
if ($test = $env->getTest($name)) { if ($test = $this->env->getTest($name)) {
return array($name, $test); return array($name, $test);
} }
@@ -604,7 +617,7 @@ class Twig_ExpressionParser
// try 2-words tests // try 2-words tests
$name = $name.' '.$this->parser->getCurrentToken()->getValue(); $name = $name.' '.$this->parser->getCurrentToken()->getValue();
if ($test = $env->getTest($name)) { if ($test = $this->env->getTest($name)) {
$stream->next(); $stream->next();
return array($name, $test); return array($name, $test);
@@ -612,7 +625,7 @@ class Twig_ExpressionParser
} }
$e = new Twig_Error_Syntax(sprintf('Unknown "%s" test.', $name), $line, $stream->getSourceContext()->getName()); $e = new Twig_Error_Syntax(sprintf('Unknown "%s" test.', $name), $line, $stream->getSourceContext()->getName());
$e->addSuggestions($name, array_keys($env->getTests())); $e->addSuggestions($name, array_keys($this->env->getTests()));
throw $e; throw $e;
} }
@@ -641,11 +654,9 @@ class Twig_ExpressionParser
protected function getFunctionNodeClass($name, $line) protected function getFunctionNodeClass($name, $line)
{ {
$env = $this->parser->getEnvironment(); if (false === $function = $this->env->getFunction($name)) {
if (false === $function = $env->getFunction($name)) {
$e = new Twig_Error_Syntax(sprintf('Unknown "%s" function.', $name), $line, $this->parser->getStream()->getSourceContext()->getName()); $e = new Twig_Error_Syntax(sprintf('Unknown "%s" function.', $name), $line, $this->parser->getStream()->getSourceContext()->getName());
$e->addSuggestions($name, array_keys($env->getFunctions())); $e->addSuggestions($name, array_keys($this->env->getFunctions()));
throw $e; throw $e;
} }
@@ -672,11 +683,9 @@ class Twig_ExpressionParser
protected function getFilterNodeClass($name, $line) protected function getFilterNodeClass($name, $line)
{ {
$env = $this->parser->getEnvironment(); if (false === $filter = $this->env->getFilter($name)) {
if (false === $filter = $env->getFilter($name)) {
$e = new Twig_Error_Syntax(sprintf('Unknown "%s" filter.', $name), $line, $this->parser->getStream()->getSourceContext()->getName()); $e = new Twig_Error_Syntax(sprintf('Unknown "%s" filter.', $name), $line, $this->parser->getStream()->getSourceContext()->getName());
$e->addSuggestions($name, array_keys($env->getFilters())); $e->addSuggestions($name, array_keys($this->env->getFilters()));
throw $e; throw $e;
} }
+6 -1
View File
@@ -42,8 +42,13 @@ class Twig_Parser implements Twig_ParserInterface
$this->env = $env; $this->env = $env;
} }
/**
* @deprecated since 1.27 (to be removed in 2.0)
*/
public function getEnvironment() public function getEnvironment()
{ {
@trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0.', E_USER_DEPRECATED);
return $this->env; return $this->env;
} }
@@ -90,7 +95,7 @@ class Twig_Parser implements Twig_ParserInterface
} }
if (null === $this->expressionParser) { if (null === $this->expressionParser) {
$this->expressionParser = new Twig_ExpressionParser($this, $this->env->getUnaryOperators(), $this->env->getBinaryOperators()); $this->expressionParser = new Twig_ExpressionParser($this, $this->env);
} }
$this->stream = $stream; $this->stream = $stream;