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
12namespace Twig;
13
14use Twig\Node\Node;
15
16/**
17 * Represents a template function.
18 *
19 * @final
20 *
21 * @author Fabien Potencier <fabien@symfony.com>
22 */
23class TwigFunction
24{
25    protected $name;
26    protected $callable;
27    protected $options;
28    protected $arguments = [];
29
30    public function __construct($name, $callable, array $options = [])
31    {
32        $this->name = $name;
33        $this->callable = $callable;
34        $this->options = array_merge([
35            'needs_environment' => false,
36            'needs_context' => false,
37            'is_variadic' => false,
38            'is_safe' => null,
39            'is_safe_callback' => null,
40            'node_class' => '\Twig\Node\Expression\FunctionExpression',
41            'deprecated' => false,
42            'alternative' => null,
43        ], $options);
44    }
45
46    public function getName()
47    {
48        return $this->name;
49    }
50
51    public function getCallable()
52    {
53        return $this->callable;
54    }
55
56    public function getNodeClass()
57    {
58        return $this->options['node_class'];
59    }
60
61    public function setArguments($arguments)
62    {
63        $this->arguments = $arguments;
64    }
65
66    public function getArguments()
67    {
68        return $this->arguments;
69    }
70
71    public function needsEnvironment()
72    {
73        return $this->options['needs_environment'];
74    }
75
76    public function needsContext()
77    {
78        return $this->options['needs_context'];
79    }
80
81    public function getSafe(Node $functionArgs)
82    {
83        if (null !== $this->options['is_safe']) {
84            return $this->options['is_safe'];
85        }
86
87        if (null !== $this->options['is_safe_callback']) {
88            return \call_user_func($this->options['is_safe_callback'], $functionArgs);
89        }
90
91        return [];
92    }
93
94    public function isVariadic()
95    {
96        return $this->options['is_variadic'];
97    }
98
99    public function isDeprecated()
100    {
101        return (bool) $this->options['deprecated'];
102    }
103
104    public function getDeprecatedVersion()
105    {
106        return $this->options['deprecated'];
107    }
108
109    public function getAlternative()
110    {
111        return $this->options['alternative'];
112    }
113}
114
115class_alias('Twig\TwigFunction', 'Twig_SimpleFunction');
116
117// Ensure that the aliased name is loaded to keep BC for classes implementing the typehint with the old aliased name.
118class_exists('Twig\Node\Node');
119