1<?php 2 3/* 4 * This file is part of Twig. 5 * 6 * (c) Fabien Potencier 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Twig\TokenParser; 13 14use Twig\Node\Expression\AssignNameExpression; 15use Twig\Node\ImportNode; 16use Twig\Token; 17 18/** 19 * Imports macros. 20 * 21 * {% from 'forms.html' import forms %} 22 */ 23final class FromTokenParser extends AbstractTokenParser 24{ 25 public function parse(Token $token) 26 { 27 $macro = $this->parser->getExpressionParser()->parseExpression(); 28 $stream = $this->parser->getStream(); 29 $stream->expect(/* Token::NAME_TYPE */ 5, 'import'); 30 31 $targets = []; 32 do { 33 $name = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue(); 34 35 $alias = $name; 36 if ($stream->nextIf('as')) { 37 $alias = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue(); 38 } 39 40 $targets[$name] = $alias; 41 42 if (!$stream->nextIf(/* Token::PUNCTUATION_TYPE */ 9, ',')) { 43 break; 44 } 45 } while (true); 46 47 $stream->expect(/* Token::BLOCK_END_TYPE */ 3); 48 49 $var = new AssignNameExpression($this->parser->getVarName(), $token->getLine()); 50 $node = new ImportNode($macro, $var, $token->getLine(), $this->getTag(), $this->parser->isMainScope()); 51 52 foreach ($targets as $name => $alias) { 53 $this->parser->addImportedSymbol('function', $alias, 'macro_'.$name, $var); 54 } 55 56 return $node; 57 } 58 59 public function getTag() 60 { 61 return 'from'; 62 } 63} 64 65class_alias('Twig\TokenParser\FromTokenParser', 'Twig_TokenParser_From'); 66