Skip to content

Commit

Permalink
feature #2119 deprecated some more methods on Twig_Environment (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.x branch.

Discussion
----------

deprecated some more methods on Twig_Environment

These methods are sometimes misused (#2020) when called from a template context (Twig_Template::getEnv() has been deprecated as well, but #2106 might re-introduce it).

Anyway, Twig_Environment should not act as a container, so, being able to change the defaults make sense, but retrieving them probably not.

Commits
-------

e548cbe deprecated some more methods on Twig_Environment
  • Loading branch information
fabpot committed Sep 19, 2016
2 parents f38f6bf + e548cbe commit ccb7981
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,5 +1,6 @@
* 1.25.0 (2016-XX-XX)

* deprecated Twig_Environment::getLexer(), Twig_Environment::getParser(), Twig_Environment::getCompiler()
* deprecated Twig_Compiler::getFilename()

* 1.24.2 (2016-09-01)
Expand Down
30 changes: 27 additions & 3 deletions lib/Twig/Environment.php
Expand Up @@ -546,9 +546,13 @@ public function clearCacheFiles()
* Gets the Lexer instance.
*
* @return Twig_LexerInterface A Twig_LexerInterface instance
*
* @deprecated since 1.25 (to be removed in 2.0)
*/
public function getLexer()
{
@trigger_error(sprintf('The %s() method is deprecated since version 1.25 and will be removed in 2.0.', __FUNCTION__), E_USER_DEPRECATED);

if (null === $this->lexer) {
$this->lexer = new Twig_Lexer($this);
}
Expand Down Expand Up @@ -578,16 +582,24 @@ public function setLexer(Twig_LexerInterface $lexer)
*/
public function tokenize($source, $name = null)
{
return $this->getLexer()->tokenize($source, $name);
if (null === $this->lexer) {
$this->lexer = new Twig_Lexer($this);
}

return $this->lexer->tokenize($source, $name);
}

/**
* Gets the Parser instance.
*
* @return Twig_ParserInterface A Twig_ParserInterface instance
*
* @deprecated since 1.25 (to be removed in 2.0)
*/
public function getParser()
{
@trigger_error(sprintf('The %s() method is deprecated since version 1.25 and will be removed in 2.0.', __FUNCTION__), E_USER_DEPRECATED);

if (null === $this->parser) {
$this->parser = new Twig_Parser($this);
}
Expand Down Expand Up @@ -616,16 +628,24 @@ public function setParser(Twig_ParserInterface $parser)
*/
public function parse(Twig_TokenStream $stream)
{
return $this->getParser()->parse($stream);
if (null === $this->parser) {
$this->parser = new Twig_Parser($this);
}

return $this->parser->parse($stream);
}

/**
* Gets the Compiler instance.
*
* @return Twig_CompilerInterface A Twig_CompilerInterface instance
*
* @deprecated since 1.25 (to be removed in 2.0)
*/
public function getCompiler()
{
@trigger_error(sprintf('The %s() method is deprecated since version 1.25 and will be removed in 2.0.', __FUNCTION__), E_USER_DEPRECATED);

if (null === $this->compiler) {
$this->compiler = new Twig_Compiler($this);
}
Expand All @@ -652,7 +672,11 @@ public function setCompiler(Twig_CompilerInterface $compiler)
*/
public function compile(Twig_NodeInterface $node)
{
return $this->getCompiler()->compile($node)->getSource();
if (null === $this->compiler) {
$this->compiler = new Twig_Compiler($this);
}

return $this->compiler->compile($node)->getSource();
}

/**
Expand Down

0 comments on commit ccb7981

Please sign in to comment.