Project

General

Profile

1
<?php
2

    
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2009 Fabien Potencier
7
 * (c) 2009 Armin Ronacher
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
class Twig_Node_Expression_Filter extends Twig_Node_Expression
13
{
14
    public function __construct(Twig_NodeInterface $node, Twig_Node_Expression_Constant $filterName, Twig_NodeInterface $arguments, $lineno, $tag = null)
15
    {
16
        parent::__construct(array('node' => $node, 'filter' => $filterName, 'arguments' => $arguments), array(), $lineno, $tag);
17
    }
18

    
19
    public function compile(Twig_Compiler $compiler)
20
    {
21
        $name = $this->getNode('filter')->getAttribute('value');
22

    
23
        if (false === $filter = $compiler->getEnvironment()->getFilter($name)) {
24
            $message = sprintf('The filter "%s" does not exist', $name);
25
            if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getFilters()))) {
26
                $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
27
            }
28

    
29
            throw new Twig_Error_Syntax($message, $this->getLine(), $compiler->getFilename());
30
        }
31

    
32
        $this->compileFilter($compiler, $filter);
33
    }
34

    
35
    protected function compileFilter(Twig_Compiler $compiler, Twig_FilterInterface $filter)
36
    {
37
        $compiler
38
            ->raw($filter->compile().'(')
39
            ->raw($filter->needsEnvironment() ? '$this->env, ' : '')
40
            ->raw($filter->needsContext() ? '$context, ' : '')
41
        ;
42

    
43
        foreach ($filter->getArguments() as $argument) {
44
            $compiler
45
                ->string($argument)
46
                ->raw(', ')
47
            ;
48
        }
49

    
50
        $compiler->subcompile($this->getNode('node'));
51

    
52
        foreach ($this->getNode('arguments') as $node) {
53
            $compiler
54
                ->raw(', ')
55
                ->subcompile($node)
56
            ;
57
        }
58

    
59
        $compiler->raw(')');
60
    }
61
}
(8-8/16)