Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 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 | |
| 12 | namespace Twig\TokenParser; |
| 13 | |
| 14 | use Twig\Node\Expression\AssignNameExpression; |
| 15 | use Twig\Node\ImportNode; |
| 16 | use Twig\Node\Node; |
| 17 | use Twig\Token; |
| 18 | |
| 19 | /** |
| 20 | * Imports macros. |
| 21 | * |
| 22 | * {% import 'forms.html' as forms %} |
| 23 | * |
| 24 | * @internal |
| 25 | */ |
| 26 | final class ImportTokenParser extends AbstractTokenParser |
| 27 | { |
| 28 | public function parse(Token $token): Node |
| 29 | { |
| 30 | $macro = $this->parser->getExpressionParser()->parseExpression(); |
| 31 | $this->parser->getStream()->expect(/* Token::NAME_TYPE */ 5, 'as'); |
| 32 | $var = new AssignNameExpression($this->parser->getStream()->expect(/* Token::NAME_TYPE */ 5)->getValue(), $token->getLine()); |
| 33 | $this->parser->getStream()->expect(/* Token::BLOCK_END_TYPE */ 3); |
| 34 | |
| 35 | $this->parser->addImportedSymbol('template', $var->getAttribute('name')); |
| 36 | |
| 37 | return new ImportNode($macro, $var, $token->getLine(), $this->getTag(), $this->parser->isMainScope()); |
| 38 | } |
| 39 | |
| 40 | public function getTag(): string |
| 41 | { |
| 42 | return 'import'; |
| 43 | } |
| 44 | } |