added the lexer, parser, and compiler as arguments to the Twig_Environment constructor

This commit is contained in:
Fabien Potencier
2010-05-26 13:52:49 +02:00
parent dc80fe13c8
commit c7fbd4307a
6 changed files with 31 additions and 33 deletions
+1
View File
@@ -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
+3 -1
View 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)
+6 -15
View 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;
}
+3 -1
View 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('{#', '#}'),
+17 -15
View 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,25 +23,14 @@ 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;
$this->handlers = array();
$this->visitors = array();
// tag handlers
foreach ($this->env->getTokenParsers() as $handler) {
$handler->setParser($this);
$this->handlers[$handler->getTag()] = $handler;
}
// node visitors
$this->visitors = $env->getNodeVisitors();
}
/**
@@ -53,6 +42,19 @@ class Twig_Parser
*/
public function parse(Twig_TokenStream $stream)
{
$this->handlers = array();
$this->visitors = array();
// tag handlers
foreach ($this->env->getTokenParsers() as $handler) {
$handler->setParser($this);
$this->handlers[$handler->getTag()] = $handler;
}
// node visitors
$this->visitors = $this->env->getNodeVisitors();
if (null === $this->expressionParser) {
$this->expressionParser = new Twig_ExpressionParser($this);
}
+1 -1
View 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);
}