/**
  * Parses a token and returns a node.
  *
  * @param Twig_Token $token A Twig_Token instance
  *
  * @return Twig_NodeInterface A Twig_NodeInterface instance
  */
 public function parse(Twig_Token $token)
 {
     $macro = $this->parser->getExpressionParser()->parseExpression();
     $stream = $this->parser->getStream();
     $stream->expect('import');
     $targets = array();
     do {
         $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
         $alias = $name;
         if ($stream->test('as')) {
             $stream->next();
             $alias = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
         }
         $targets[$name] = $alias;
         if (!$stream->test(Twig_Token::PUNCTUATION_TYPE, ',')) {
             break;
         }
         $stream->next();
     } while (true);
     $stream->expect(Twig_Token::BLOCK_END_TYPE);
     $node = new Twig_Node_Import($macro, new Twig_Node_Expression_AssignName($this->parser->getVarName(), $token->getLine()), $token->getLine(), $this->getTag());
     foreach ($targets as $name => $alias) {
         $this->parser->addImportedFunction($alias, $name, $node->getNode('var'));
     }
     return $node;
 }
Esempio n. 2
0
 public function parse(Twig_Token $token)
 {
     $macro = $this->parser->getExpressionParser()->parseExpression();
     $stream = $this->parser->getStream();
     $stream->expect('import');
     $targets = array();
     do {
         $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
         $alias = $name;
         if ($stream->nextIf('as')) {
             $alias = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
         }
         $targets[$name] = $alias;
         if (!$stream->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
             break;
         }
     } while (true);
     $stream->expect(Twig_Token::BLOCK_END_TYPE);
     $node = new Twig_Node_Import($macro, new Twig_Node_Expression_AssignName($this->parser->getVarName(), $token->getLine()), $token->getLine(), $this->getTag());
     foreach ($targets as $name => $alias) {
         if ($this->parser->isReservedMacroName($name)) {
             throw new Twig_Error_Syntax(sprintf('"%s" cannot be an imported macro as it is a reserved keyword.', $name), $token->getLine(), $stream->getFilename());
         }
         $this->parser->addImportedSymbol('function', $alias, 'get' . $name, $node->getNode('var'));
     }
     return $node;
 }
Esempio n. 3
0
 /**
  * @covers Twig_Node_Import::__construct
  */
 public function testConstructor()
 {
     $macro = new Twig_Node_Expression_Constant('foo.twig', 1);
     $var = new Twig_Node_Expression_AssignName('macro', 1);
     $node = new Twig_Node_Import($macro, $var, 1);
     $this->assertEquals($macro, $node->getNode('expr'));
     $this->assertEquals($var, $node->getNode('var'));
 }
Esempio n. 4
0
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile($compiler)
 {
     parent::compile($compiler);
     foreach ($this->getAttribute('imports') as $name => $alias) {
         $compiler->write('$context[')->repr('fn_' . $alias)->raw('] = new Twig_Function(')->subcompile($this->getNode('var'))->raw(', ')->repr($name)->raw(");\n");
     }
 }
Esempio n. 5
0
 public function __construct(Twig_Node_Expression $expr, $lineno, $tag = null)
 {
     parent::__construct($expr, new Twig_Node_Expression_AssignName(str_replace('.', '_', uniqid('_imported_', true)), $lineno), $lineno, $tag);
 }