From: Fabien Potencier Date: Wed, 26 May 2010 11:52:49 +0000 (+0200) Subject: added the lexer, parser, and compiler as arguments to the Twig_Environment constructor X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=c7fbd4307a02776cc81d8dc4d3f7c9a09b897aa0;p=web%2Fkonrad%2Ftwig.git added the lexer, parser, and compiler as arguments to the Twig_Environment constructor --- diff --git a/CHANGELOG b/CHANGELOG index 85d4f51..d139d02 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,7 @@ Backward incompatibilities: * The short notation of the `block` tag changed. + * added the lexer, parser, and compiler as arguments to the Twig_Environment constructor * changed the cache option to only accepts an explicit path to a cache directory or false * added a way to add token parsers, filters, and visitors without creating an extension * added three interfaces: Twig_NodeInterface, Twig_TokenParserInterface, and Twig_FilterInterface diff --git a/lib/Twig/Compiler.php b/lib/Twig/Compiler.php index ce3525f..989f190 100644 --- a/lib/Twig/Compiler.php +++ b/lib/Twig/Compiler.php @@ -31,7 +31,9 @@ class Twig_Compiler implements Twig_CompilerInterface */ public function __construct(Twig_Environment $env = null) { - $this->env = $env; + if (null !== $env) { + $this->setEnvironment($env); + } } public function setEnvironment(Twig_Environment $env) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index b4a8f65..a05a251 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -58,12 +58,16 @@ class Twig_Environment * @param Twig_LoaderInterface $loader A Twig_LoaderInterface instance * @param array $options An array of options */ - public function __construct(Twig_LoaderInterface $loader = null, $options = array()) + public function __construct(Twig_LoaderInterface $loader = null, $options = array(), Twig_LexerInterface $lexer = null, Twig_ParserInterface $parser = null, Twig_CompilerInterface $compiler = null) { if (null !== $loader) { $this->setLoader($loader); } + $this->setLexer(null !== $lexer ? $lexer : new Twig_Lexer()); + $this->setParser(null !== $parser ? $parser : new Twig_Parser()); + $this->setCompiler(null !== $compiler ? $compiler : new Twig_Compiler()); + $this->debug = isset($options['debug']) ? (bool) $options['debug'] : false; $this->trimBlocks = isset($options['trim_blocks']) ? (bool) $options['trim_blocks'] : false; $this->charset = isset($options['charset']) ? $options['charset'] : 'UTF-8'; @@ -71,8 +75,7 @@ class Twig_Environment $this->autoReload = isset($options['auto_reload']) ? (bool) $options['auto_reload'] : $this->debug; $this->extensions = array('core' => new Twig_Extension_Core()); $this->runtimeInitialized = false; - if (isset($options['cache']) && $options['cache']) - { + if (isset($options['cache']) && $options['cache']) { $this->setCache($options['cache']); } } @@ -202,10 +205,6 @@ class Twig_Environment public function getLexer() { - if (null === $this->lexer) { - $this->lexer = new Twig_Lexer($this); - } - return $this->lexer; } @@ -222,10 +221,6 @@ class Twig_Environment public function getParser() { - if (null === $this->parser) { - $this->parser = new Twig_Parser($this); - } - return $this->parser; } @@ -242,10 +237,6 @@ class Twig_Environment public function getCompiler() { - if (null === $this->compiler) { - $this->compiler = new Twig_Compiler($this); - } - return $this->compiler; } diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index ce3dfcf..dc995a4 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -40,7 +40,9 @@ class Twig_Lexer implements Twig_LexerInterface public function __construct(Twig_Environment $env = null, array $options = array()) { - $this->env = $env; + if (null !== $env) { + $this->setEnvironment($env); + } $this->options = array_merge(array( 'tag_comment' => array('{#', '#}'), diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index d3dd1d7..3d07dd9 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -9,7 +9,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -class Twig_Parser +class Twig_Parser implements Twig_ParserInterface { protected $stream; protected $extends; @@ -23,13 +23,25 @@ class Twig_Parser public function __construct(Twig_Environment $env = null) { - $this->setEnvironment($env); + if (null !== $env) { + $this->setEnvironment($env); + } } public function setEnvironment(Twig_Environment $env) { $this->env = $env; + } + /** + * Converts a token stream to a node tree. + * + * @param Twig_TokenStream $stream A token stream instance + * + * @return Twig_Node_Module A node tree + */ + public function parse(Twig_TokenStream $stream) + { $this->handlers = array(); $this->visitors = array(); @@ -41,18 +53,8 @@ class Twig_Parser } // node visitors - $this->visitors = $env->getNodeVisitors(); - } + $this->visitors = $this->env->getNodeVisitors(); - /** - * Converts a token stream to a node tree. - * - * @param Twig_TokenStream $stream A token stream instance - * - * @return Twig_Node_Module A node tree - */ - public function parse(Twig_TokenStream $stream) - { if (null === $this->expressionParser) { $this->expressionParser = new Twig_ExpressionParser($this); } diff --git a/lib/Twig/ParserInterface.php b/lib/Twig/ParserInterface.php index 1bbcd50..14871c3 100644 --- a/lib/Twig/ParserInterface.php +++ b/lib/Twig/ParserInterface.php @@ -25,5 +25,5 @@ interface Twig_ParserInterface * * @return Twig_Node_Module A node tree */ - public function parser(Twig_TokenStream $code); + public function parse(Twig_TokenStream $code); }