Project

General

Profile

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

    
12
/**
13
 * Imports macros.
14
 *
15
 * <pre>
16
 *   {% from 'forms.html' import forms %}
17
 * </pre>
18
 */
19
class Twig_TokenParser_From extends Twig_TokenParser
20
{
21
    public function parse(Twig_Token $token)
22
    {
23
        $macro = $this->parser->getExpressionParser()->parseExpression();
24
        $stream = $this->parser->getStream();
25
        $stream->expect('import');
26

    
27
        $targets = array();
28
        do {
29
            $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
30

    
31
            $alias = $name;
32
            if ($stream->nextIf('as')) {
33
                $alias = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
34
            }
35

    
36
            $targets[$name] = $alias;
37

    
38
            if (!$stream->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
39
                break;
40
            }
41
        } while (true);
42

    
43
        $stream->expect(Twig_Token::BLOCK_END_TYPE);
44

    
45
        $node = new Twig_Node_Import($macro, new Twig_Node_Expression_AssignName($this->parser->getVarName(), $token->getLine()), $token->getLine(), $this->getTag());
46

    
47
        foreach ($targets as $name => $alias) {
48
            if ($this->parser->isReservedMacroName($name)) {
49
                throw new Twig_Error_Syntax(sprintf('"%s" cannot be an imported macro as it is a reserved keyword.', $name), $token->getLine(), $stream->getFilename());
50
            }
51

    
52
            $this->parser->addImportedSymbol('function', $alias, 'get'.$name, $node->getNode('var'));
53
        }
54

    
55
        return $node;
56
    }
57

    
58
    public function getTag()
59
    {
60
        return 'from';
61
    }
62
}
(9-9/17)