blob: 44cb4dad79dd68b10aebab62a6e3ebec8f8b2cdb [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001<?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\Node\Node;
17use Twig\Token;
18
19/**
20 * Imports macros.
21 *
22 * {% import 'forms.html' as forms %}
23 *
24 * @internal
25 */
26final 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}