예제 #1
0
 /**
  * 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)
 {
     // tag handlers
     $this->handlers = $this->env->getTokenParsers();
     $this->handlers->setParser($this);
     // node visitors
     $this->visitors = $this->env->getNodeVisitors();
     if (null === $this->expressionParser) {
         $this->expressionParser = new Twig_ExpressionParser($this);
     }
     $this->stream = $stream;
     $this->parent = null;
     $this->blocks = array();
     $this->macros = array();
     $this->blockStack = array();
     try {
         $body = $this->subparse(null);
     } catch (Twig_SyntaxError $e) {
         if (is_null($e->getFilename())) {
             $e->setFilename($this->stream->getFilename());
         }
         throw $e;
     }
     if (null !== $this->parent) {
         $this->checkBodyNodes($body);
     }
     $node = new Twig_Node_Module($body, $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), $this->stream->getFilename());
     $traverser = new Twig_NodeTraverser($this->env, $this->visitors);
     return $traverser->traverse($node);
 }
예제 #2
0
 /**
  * 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);
     }
     $this->stream = $stream;
     $this->extends = null;
     $this->blocks = array();
     $this->macros = array();
     $this->blockStack = array();
     try {
         $body = $this->subparse(null);
     } catch (Twig_SyntaxError $e) {
         if (is_null($e->getFilename())) {
             $e->setFilename($this->stream->getFilename());
         }
         throw $e;
     }
     if (!is_null($this->extends)) {
         foreach ($this->blocks as $block) {
             $block->setParent($this->extends);
         }
     }
     $node = new Twig_Node_Module($body, $this->extends, $this->blocks, $this->macros, $this->stream->getFilename());
     $t = new Twig_NodeTraverser($this->env);
     foreach ($this->visitors as $visitor) {
         $node = $t->traverse($node, $visitor);
     }
     return $node;
 }
예제 #3
0
파일: Parser.php 프로젝트: n0way/Twig
 /**
  * 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->tmpVarCount = 0;
     // tag handlers
     $this->handlers = $this->env->getTokenParsers();
     $this->handlers->setParser($this);
     // node visitors
     $this->visitors = $this->env->getNodeVisitors();
     if (null === $this->expressionParser) {
         $this->expressionParser = new Twig_ExpressionParser($this, $this->env->getUnaryOperators(), $this->env->getBinaryOperators());
     }
     $this->stream = $stream;
     $this->parent = null;
     $this->blocks = array();
     $this->macros = array();
     $this->traits = array();
     $this->blockStack = array();
     $this->importedFunctions = array(array());
     try {
         $body = $this->subparse(null);
         if (null !== $this->parent) {
             if (null === ($body = $this->filterBodyNodes($body))) {
                 $body = new Twig_Node();
             }
         }
     } catch (Twig_Error_Syntax $e) {
         if (null === $e->getTemplateFile()) {
             $e->setTemplateFile($this->stream->getFilename());
         }
         throw $e;
     }
     $node = new Twig_Node_Module($body, $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), $this->stream->getFilename());
     $traverser = new Twig_NodeTraverser($this->env, $this->visitors);
     return $traverser->traverse($node);
 }
 public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
 {
     if ($node instanceof \Twig_Node_Module) {
         $parent = $node->getNode('parent');
         if ($parent instanceof \Twig_Node_Expression_Constant) {
             $templateName = $parent->getAttribute('value');
             $collector = new PlaceHolderNodeCollector();
             $traverser = new \Twig_NodeTraverser($env, array($collector));
             $stream = $env->tokenize($env->getLoader()->getSource($templateName));
             $traverser->traverse($env->getParser()->parse($stream));
             $this->names = array_merge($this->names, $collector->getCollectedNames());
         }
     } elseif ($node instanceof PlaceHolderNode) {
         $arguments = $node->getNode('arguments');
         if ($arguments->count() !== 1) {
             throw new \UnexpectedValueException('Expecting only name definition inside place holder function.');
         }
         $nameNode = $arguments->getIterator()->current();
         if (!$nameNode instanceof \Twig_Node_Expression_Constant) {
             throw new \UnexpectedValueException('Name definition should be constant expression only.');
         }
         $this->names[] = $nameNode->getAttribute('value');
     }
     return $node;
 }
예제 #5
0
 /**
  * {@inheritDoc}
  */
 public function getPlaces($layoutSrc)
 {
     $tokenStream = $this->twig->tokenize($this->twig->getLoader()->getSource($layoutSrc));
     $collector = new PlaceHolderNodeCollector();
     $traverser = new \Twig_NodeTraverser($this->twig, array($collector));
     $traverser->traverse($this->twig->parse($tokenStream));
     return $collector->getCollectedNames();
 }
예제 #6
0
 /**
  * If the current Twig Node has embedded templates, we want to travese these templates
  * in the same manner as we do the main twig template to ensure all translations are 
  * caught.
  * 
  * @param \Twig_Node $node
  */
 private function traverseEmbeddedTemplates(\Twig_Node $node)
 {
     $templates = $node->getAttribute('embedded_templates');
     foreach ($templates as $template) {
         $this->traverser->traverse($template);
         if ($template->hasAttribute('embedded_templates')) {
             $this->traverseEmbeddedTemplates($template);
         }
     }
 }
예제 #7
0
파일: Parser.php 프로젝트: nmcteam/Twig
 /**
  * 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();
     // 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);
     }
     $this->stream = $stream;
     $this->extends = null;
     $this->blocks = array();
     $this->macros = array();
     $this->blockStack = array();
     try {
         $body = $this->subparse(null);
     } catch (Twig_SyntaxError $e) {
         if (is_null($e->getFilename())) {
             $e->setFilename($this->stream->getFilename());
         }
         throw $e;
     }
     if (!is_null($this->extends)) {
         // check that the body only contains block references and empty text nodes
         foreach ($body->getNodes() as $node) {
             if ($node instanceof Twig_Node_Text && !preg_match('/^\\s*$/s', $node->getData()) || !$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference) {
                 throw new Twig_SyntaxError('A template that extends another one cannot have a body', $node->getLine(), $this->stream->getFilename());
             }
         }
         foreach ($this->blocks as $block) {
             $block->setParent($this->extends);
         }
     }
     $node = new Twig_Node_Module($body, $this->extends, $this->blocks, $this->macros, $this->stream->getFilename());
     $t = new Twig_NodeTraverser($this->env);
     foreach ($this->visitors as $visitor) {
         $node = $t->traverse($node, $visitor);
     }
     return $node;
 }
예제 #8
0
파일: Parser.php 프로젝트: vgrish/twiggy
 /**
  * {@inheritdoc}
  */
 public function parse(Twig_TokenStream $stream, $test = null, $dropNeedle = false)
 {
     // push all variables into the stack to keep the current state of the parser
     // using get_object_vars() instead of foreach would lead to https://bugs.php.net/71336
     $vars = array();
     foreach ($this as $k => $v) {
         $vars[$k] = $v;
     }
     unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser'], $vars['reservedMacroNames']);
     $this->stack[] = $vars;
     // tag handlers
     if (null === $this->handlers) {
         $this->handlers = array();
         foreach ($this->env->getTokenParsers() as $handler) {
             $handler->setParser($this);
             $this->handlers[$handler->getTag()] = $handler;
         }
     }
     // node visitors
     if (null === $this->visitors) {
         $this->visitors = $this->env->getNodeVisitors();
     }
     if (null === $this->expressionParser) {
         $this->expressionParser = new Twig_ExpressionParser($this, $this->env->getUnaryOperators(), $this->env->getBinaryOperators());
     }
     $this->stream = $stream;
     $this->parent = null;
     $this->blocks = array();
     $this->macros = array();
     $this->traits = array();
     $this->blockStack = array();
     $this->importedSymbols = array(array());
     $this->embeddedTemplates = array();
     try {
         $body = $this->subparse($test, $dropNeedle);
         if (null !== $this->parent && null === ($body = $this->filterBodyNodes($body))) {
             $body = new Twig_Node();
         }
     } catch (Twig_Error_Syntax $e) {
         if (!$e->getTemplateFile()) {
             $e->setTemplateFile($this->getFilename());
         }
         if (!$e->getTemplateLine()) {
             $e->setTemplateLine($this->stream->getCurrent()->getLine());
         }
         throw $e;
     }
     $node = new Twig_Node_Module(new Twig_Node_Body(array($body)), $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), $this->embeddedTemplates, $this->getFilename(), $stream->getSource());
     $traverser = new Twig_NodeTraverser($this->env, $this->visitors);
     $node = $traverser->traverse($node);
     // restore previous stack so previous parse() call can resume working
     foreach (array_pop($this->stack) as $key => $val) {
         $this->{$key} = $val;
     }
     return $node;
 }
예제 #9
0
 /**
  * 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, $test = null, $dropNeedle = false)
 {
     // push all variables into the stack to keep the current state of the parser
     $vars = get_object_vars($this);
     unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser']);
     $this->stack[] = $vars;
     $this->tmpVarCount = 0;
     // tag handlers
     if (null === $this->handlers) {
         $this->handlers = $this->env->getTokenParsers();
         $this->handlers->setParser($this);
     }
     // node visitors
     if (null === $this->visitors) {
         $this->visitors = $this->env->getNodeVisitors();
     }
     if (null === $this->expressionParser) {
         $this->expressionParser = new Twig_ExpressionParser($this, $this->env->getUnaryOperators(), $this->env->getBinaryOperators());
     }
     $this->stream = $stream;
     $this->parent = null;
     $this->blocks = array();
     $this->macros = array();
     $this->traits = array();
     $this->blockStack = array();
     $this->importedFunctions = array(array());
     $this->embeddedTemplates = array();
     try {
         $body = $this->subparse($test, $dropNeedle);
         if (null !== $this->parent) {
             if (null === ($body = $this->filterBodyNodes($body))) {
                 $body = new Twig_Node();
             }
         }
     } catch (Twig_Error_Syntax $e) {
         if (null === $e->getTemplateFile()) {
             $e->setTemplateFile($this->stream->getFilename());
         }
         throw $e;
     }
     $node = new Twig_Node_Module(new Twig_Node_Body(array($body)), $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), $this->embeddedTemplates, $this->stream->getFilename());
     $traverser = new Twig_NodeTraverser($this->env, $this->visitors);
     $node = $traverser->traverse($node);
     // restore previous stack so previous parse() call can resume working
     foreach (array_pop($this->stack) as $key => $val) {
         $this->{$key} = $val;
     }
     return $node;
 }
예제 #10
0
 /**
  * @param \SplFileInfo $file
  * @param $catalogue
  * @param \Twig_Node $ast
  */
 public function visitFile(\SplFileInfo $file, $catalogue, \Twig_Node $ast)
 {
     $this->file = $file;
     $this->catalogue = $catalogue;
     $this->traverser->traverse($ast);
 }
예제 #11
0
 /**
  * @param \Twig_Environment $twig
  * @throws \LogicException
  */
 public function initialize(\Twig_Environment $twig)
 {
     if ($this->initialized === true) {
         throw new \LogicException('Is initialized already.');
     }
     $this->initialized = true;
     $this->configureAttributes(new AttributeMapper($this));
     $this->configureProperties(new PropertyMapper($this));
     $this->configureCache(new CacheMapper($this));
     if ($this->autoDiscoverProperties) {
         $tokenStream = $twig->tokenize($twig->getLoader()->getSource($this->getTemplateName()));
         $traverser = new \Twig_NodeTraverser($twig);
         $nodeVisitor = new BlockPropertyNodeVisitor(new PropertyMapper($this));
         $traverser->addVisitor($nodeVisitor);
         $traverser->traverse($twig->parse($tokenStream));
     }
 }