1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * This file is part of Twig. |
---|
5 | * |
---|
6 | * (c) 2010 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 | class Twig_TokenParser_From extends Twig_TokenParser |
---|
12 | { |
---|
13 | /** |
---|
14 | * Parses a token and returns a node. |
---|
15 | * |
---|
16 | * @param Twig_Token $token A Twig_Token instance |
---|
17 | * |
---|
18 | * @return Twig_NodeInterface A Twig_NodeInterface instance |
---|
19 | */ |
---|
20 | public function parse(Twig_Token $token) |
---|
21 | { |
---|
22 | $macro = $this->parser->getExpressionParser()->parseExpression(); |
---|
23 | $stream = $this->parser->getStream(); |
---|
24 | $stream->expect('import'); |
---|
25 | |
---|
26 | $targets = array(); |
---|
27 | do { |
---|
28 | $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); |
---|
29 | |
---|
30 | $alias = $name; |
---|
31 | if ($stream->test('as')) { |
---|
32 | $stream->next(); |
---|
33 | |
---|
34 | $alias = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); |
---|
35 | } |
---|
36 | |
---|
37 | $targets[$name] = $alias; |
---|
38 | |
---|
39 | if (!$stream->test(Twig_Token::PUNCTUATION_TYPE, ',')) { |
---|
40 | break; |
---|
41 | } |
---|
42 | |
---|
43 | $stream->next(); |
---|
44 | } while (true); |
---|
45 | |
---|
46 | $stream->expect(Twig_Token::BLOCK_END_TYPE); |
---|
47 | |
---|
48 | $node = new Twig_Node_Import($macro, new Twig_Node_Expression_AssignName($this->parser->getVarName(), $token->getLine()), $token->getLine(), $this->getTag()); |
---|
49 | |
---|
50 | foreach($targets as $name => $alias) { |
---|
51 | $this->parser->addImportedFunction($alias, $name, $node->getNode('var')); |
---|
52 | } |
---|
53 | |
---|
54 | return $node; |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * Gets the tag name associated with this token parser. |
---|
59 | * |
---|
60 | * @param string The tag name |
---|
61 | */ |
---|
62 | public function getTag() |
---|
63 | { |
---|
64 | return 'from'; |
---|
65 | } |
---|
66 | } |
---|