Yaffs site version 1.1
[yaffs-website] / vendor / twig / twig / lib / Twig / TokenParser / From.php
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 /**
13  * Imports macros.
14  *
15  * <pre>
16  *   {% from 'forms.html' import forms %}
17  * </pre>
18  *
19  * @final
20  */
21 class Twig_TokenParser_From extends Twig_TokenParser
22 {
23     public function parse(Twig_Token $token)
24     {
25         $macro = $this->parser->getExpressionParser()->parseExpression();
26         $stream = $this->parser->getStream();
27         $stream->expect('import');
28
29         $targets = array();
30         do {
31             $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
32
33             $alias = $name;
34             if ($stream->nextIf('as')) {
35                 $alias = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
36             }
37
38             $targets[$name] = $alias;
39
40             if (!$stream->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
41                 break;
42             }
43         } while (true);
44
45         $stream->expect(Twig_Token::BLOCK_END_TYPE);
46
47         $node = new Twig_Node_Import($macro, new Twig_Node_Expression_AssignName($this->parser->getVarName(), $token->getLine()), $token->getLine(), $this->getTag());
48
49         foreach ($targets as $name => $alias) {
50             if ($this->parser->isReservedMacroName($name)) {
51                 throw new Twig_Error_Syntax(sprintf('"%s" cannot be an imported macro as it is a reserved keyword.', $name), $token->getLine(), $stream->getSourceContext());
52             }
53
54             $this->parser->addImportedSymbol('function', $alias, 'get'.$name, $node->getNode('var'));
55         }
56
57         return $node;
58     }
59
60     public function getTag()
61     {
62         return 'from';
63     }
64 }
65
66 class_alias('Twig_TokenParser_From', 'Twig\TokenParser\FromTokenParser', false);