added the lexer, parser, and compiler as arguments to the Twig_Environment constructor
authorFabien Potencier <fabien.potencier@gmail.com>
Wed, 26 May 2010 11:52:49 +0000 (13:52 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Wed, 26 May 2010 11:52:49 +0000 (13:52 +0200)
CHANGELOG
lib/Twig/Compiler.php
lib/Twig/Environment.php
lib/Twig/Lexer.php
lib/Twig/Parser.php
lib/Twig/ParserInterface.php

index 85d4f51..d139d02 100644 (file)
--- 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
index ce3525f..989f190 100644 (file)
@@ -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)
index b4a8f65..a05a251 100644 (file)
@@ -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;
     }
 
index ce3dfcf..dc995a4 100644 (file)
@@ -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('{#', '#}'),
index d3dd1d7..3d07dd9 100644 (file)
@@ -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);
         }
index 1bbcd50..14871c3 100644 (file)
@@ -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);
 }