Project

General

Profile

1 1686 darkviper
<?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_Node_Expression_Function extends Twig_Node_Expression
12
{
13
    public function __construct($name, Twig_NodeInterface $arguments, $lineno)
14
    {
15
        parent::__construct(array('arguments' => $arguments), array('name' => $name), $lineno);
16
    }
17
18
    public function compile(Twig_Compiler $compiler)
19
    {
20
        $name = $this->getAttribute('name');
21
22
        if (false === $function = $compiler->getEnvironment()->getFunction($name)) {
23
            $message = sprintf('The function "%s" does not exist', $name);
24
            if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getFunctions()))) {
25
                $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
26
            }
27
28
            throw new Twig_Error_Syntax($message, $this->getLine());
29
        }
30
31
        $compiler->raw($function->compile().'(');
32
33
        $first = true;
34
35
        if ($function->needsEnvironment()) {
36
            $compiler->raw('$this->env');
37
            $first = false;
38
        }
39
40
        if ($function->needsContext()) {
41
            if (!$first) {
42
                $compiler->raw(', ');
43
            }
44
            $compiler->raw('$context');
45
            $first = false;
46
        }
47
48
        foreach ($function->getArguments() as $argument) {
49
            if (!$first) {
50
                $compiler->raw(', ');
51
            }
52
            $compiler->string($argument);
53
            $first = false;
54
        }
55
56
        foreach ($this->getNode('arguments') as $node) {
57
            if (!$first) {
58
                $compiler->raw(', ');
59
            }
60
            $compiler->subcompile($node);
61
            $first = false;
62
        }
63
64
        $compiler->raw(')');
65
    }
66
}