PluginProbe
Elementor Website Builder – more than just a page builder / 3.28.0-dev1
Elementor Website Builder – more than just a page builder v3.28.0-dev1
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / vendor_prefixed / twig / src / Node / Expression / CallExpression.php

CallExpression.php in Elementor Website Builder – more than just a page builder 3.28.0-dev1, at vendor_prefixed/twig/src/Node/Expression/CallExpression.php

256 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 namespace ElementorDeps\Twig\Node\Expression;
12
13 use ElementorDeps\Twig\Compiler;
14 use ElementorDeps\Twig\Error\SyntaxError;
15 use ElementorDeps\Twig\Extension\ExtensionInterface;
16 use ElementorDeps\Twig\Node\Node;
17 use ElementorDeps\Twig\Util\ReflectionCallable;
18 abstract class CallExpression extends AbstractExpression
19 {
20 private $reflector = null;
21 protected function compileCallable(Compiler $compiler)
22 {
23 $callable = $this->getAttribute('callable');
24 if (\is_string($callable) && !\str_contains($callable, '::')) {
25 $compiler->raw($callable);
26 } else {
27 $rc = $this->reflectCallable($callable);
28 $r = $rc->getReflector();
29 $callable = $rc->getCallable();
30 if (\is_string($callable)) {
31 $compiler->raw($callable);
32 } elseif (\is_array($callable) && \is_string($callable[0])) {
33 if (!$r instanceof \ReflectionMethod || $r->isStatic()) {
34 $compiler->raw(\sprintf('%s::%s', $callable[0], $callable[1]));
35 } else {
36 $compiler->raw(\sprintf('$this->env->getRuntime(\'%s\')->%s', $callable[0], $callable[1]));
37 }
38 } elseif (\is_array($callable) && $callable[0] instanceof ExtensionInterface) {
39 $class = \get_class($callable[0]);
40 if (!$compiler->getEnvironment()->hasExtension($class)) {
41 // Compile a non-optimized call to trigger a \Twig\Error\RuntimeError, which cannot be a compile-time error
42 $compiler->raw(\sprintf('$this->env->getExtension(\'%s\')', $class));
43 } else {
44 $compiler->raw(\sprintf('$this->extensions[\'%s\']', \ltrim($class, '\\')));
45 }
46 $compiler->raw(\sprintf('->%s', $callable[1]));
47 } else {
48 $compiler->raw(\sprintf('$this->env->get%s(\'%s\')->getCallable()', \ucfirst($this->getAttribute('type')), $this->getAttribute('name')));
49 }
50 }
51 $this->compileArguments($compiler);
52 }
53 protected function compileArguments(Compiler $compiler, $isArray = \false) : void
54 {
55 if (\func_num_args() >= 2) {
56 trigger_deprecation('twig/twig', '3.11', 'Passing a second argument to "%s()" is deprecated.', __METHOD__);
57 }
58 $compiler->raw($isArray ? '[' : '(');
59 $first = \true;
60 if ($this->hasAttribute('needs_charset') && $this->getAttribute('needs_charset')) {
61 $compiler->raw('$this->env->getCharset()');
62 $first = \false;
63 }
64 if ($this->hasAttribute('needs_environment') && $this->getAttribute('needs_environment')) {
65 if (!$first) {
66 $compiler->raw(', ');
67 }
68 $compiler->raw('$this->env');
69 $first = \false;
70 }
71 if ($this->hasAttribute('needs_context') && $this->getAttribute('needs_context')) {
72 if (!$first) {
73 $compiler->raw(', ');
74 }
75 $compiler->raw('$context');
76 $first = \false;
77 }
78 if ($this->hasAttribute('arguments')) {
79 foreach ($this->getAttribute('arguments') as $argument) {
80 if (!$first) {
81 $compiler->raw(', ');
82 }
83 $compiler->string($argument);
84 $first = \false;
85 }
86 }
87 if ($this->hasNode('node')) {
88 if (!$first) {
89 $compiler->raw(', ');
90 }
91 $compiler->subcompile($this->getNode('node'));
92 $first = \false;
93 }
94 if ($this->hasNode('arguments')) {
95 $callable = $this->getAttribute('callable');
96 $arguments = $this->getArguments($callable, $this->getNode('arguments'));
97 foreach ($arguments as $node) {
98 if (!$first) {
99 $compiler->raw(', ');
100 }
101 $compiler->subcompile($node);
102 $first = \false;
103 }
104 }
105 $compiler->raw($isArray ? ']' : ')');
106 }
107 protected function getArguments($callable, $arguments)
108 {
109 $callType = $this->getAttribute('type');
110 $callName = $this->getAttribute('name');
111 $parameters = [];
112 $named = \false;
113 foreach ($arguments as $name => $node) {
114 if (!\is_int($name)) {
115 $named = \true;
116 $name = $this->normalizeName($name);
117 } elseif ($named) {
118 throw new SyntaxError(\sprintf('Positional arguments cannot be used after named arguments for %s "%s".', $callType, $callName), $this->getTemplateLine(), $this->getSourceContext());
119 }
120 $parameters[$name] = $node;
121 }
122 $isVariadic = $this->hasAttribute('is_variadic') && $this->getAttribute('is_variadic');
123 if (!$named && !$isVariadic) {
124 return $parameters;
125 }
126 if (!$callable) {
127 if ($named) {
128 $message = \sprintf('Named arguments are not supported for %s "%s".', $callType, $callName);
129 } else {
130 $message = \sprintf('Arbitrary positional arguments are not supported for %s "%s".', $callType, $callName);
131 }
132 throw new \LogicException($message);
133 }
134 [$callableParameters, $isPhpVariadic] = $this->getCallableParameters($callable, $isVariadic);
135 $arguments = [];
136 $names = [];
137 $missingArguments = [];
138 $optionalArguments = [];
139 $pos = 0;
140 foreach ($callableParameters as $callableParameter) {
141 $name = $this->normalizeName($callableParameter->name);
142 if (\PHP_VERSION_ID >= 80000 && 'range' === $callable) {
143 if ('start' === $name) {
144 $name = 'low';
145 } elseif ('end' === $name) {
146 $name = 'high';
147 }
148 }
149 $names[] = $name;
150 if (\array_key_exists($name, $parameters)) {
151 if (\array_key_exists($pos, $parameters)) {
152 throw new SyntaxError(\sprintf('Argument "%s" is defined twice for %s "%s".', $name, $callType, $callName), $this->getTemplateLine(), $this->getSourceContext());
153 }
154 if (\count($missingArguments)) {
155 throw new SyntaxError(\sprintf('Argument "%s" could not be assigned for %s "%s(%s)" because it is mapped to an internal PHP function which cannot determine default value for optional argument%s "%s".', $name, $callType, $callName, \implode(', ', $names), \count($missingArguments) > 1 ? 's' : '', \implode('", "', $missingArguments)), $this->getTemplateLine(), $this->getSourceContext());
156 }
157 $arguments = \array_merge($arguments, $optionalArguments);
158 $arguments[] = $parameters[$name];
159 unset($parameters[$name]);
160 $optionalArguments = [];
161 } elseif (\array_key_exists($pos, $parameters)) {
162 $arguments = \array_merge($arguments, $optionalArguments);
163 $arguments[] = $parameters[$pos];
164 unset($parameters[$pos]);
165 $optionalArguments = [];
166 ++$pos;
167 } elseif ($callableParameter->isDefaultValueAvailable()) {
168 $optionalArguments[] = new ConstantExpression($callableParameter->getDefaultValue(), -1);
169 } elseif ($callableParameter->isOptional()) {
170 if (empty($parameters)) {
171 break;
172 } else {
173 $missingArguments[] = $name;
174 }
175 } else {
176 throw new SyntaxError(\sprintf('Value for argument "%s" is required for %s "%s".', $name, $callType, $callName), $this->getTemplateLine(), $this->getSourceContext());
177 }
178 }
179 if ($isVariadic) {
180 $arbitraryArguments = $isPhpVariadic ? new VariadicExpression([], -1) : new ArrayExpression([], -1);
181 foreach ($parameters as $key => $value) {
182 if (\is_int($key)) {
183 $arbitraryArguments->addElement($value);
184 } else {
185 $arbitraryArguments->addElement($value, new ConstantExpression($key, -1));
186 }
187 unset($parameters[$key]);
188 }
189 if ($arbitraryArguments->count()) {
190 $arguments = \array_merge($arguments, $optionalArguments);
191 $arguments[] = $arbitraryArguments;
192 }
193 }
194 if (!empty($parameters)) {
195 $unknownParameter = null;
196 foreach ($parameters as $parameter) {
197 if ($parameter instanceof Node) {
198 $unknownParameter = $parameter;
199 break;
200 }
201 }
202 throw new SyntaxError(\sprintf('Unknown argument%s "%s" for %s "%s(%s)".', \count($parameters) > 1 ? 's' : '', \implode('", "', \array_keys($parameters)), $callType, $callName, \implode(', ', $names)), $unknownParameter ? $unknownParameter->getTemplateLine() : $this->getTemplateLine(), $unknownParameter ? $unknownParameter->getSourceContext() : $this->getSourceContext());
203 }
204 return $arguments;
205 }
206 protected function normalizeName(string $name) : string
207 {
208 return \strtolower(\preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\\d])([A-Z])/'], ['\\1_\\2', '\\1_\\2'], $name));
209 }
210 private function getCallableParameters($callable, bool $isVariadic) : array
211 {
212 $rc = $this->reflectCallable($callable);
213 $r = $rc->getReflector();
214 $callableName = $rc->getName();
215 $parameters = $r->getParameters();
216 if ($this->hasNode('node')) {
217 \array_shift($parameters);
218 }
219 if ($this->hasAttribute('needs_charset') && $this->getAttribute('needs_charset')) {
220 \array_shift($parameters);
221 }
222 if ($this->hasAttribute('needs_environment') && $this->getAttribute('needs_environment')) {
223 \array_shift($parameters);
224 }
225 if ($this->hasAttribute('needs_context') && $this->getAttribute('needs_context')) {
226 \array_shift($parameters);
227 }
228 if ($this->hasAttribute('arguments') && null !== $this->getAttribute('arguments')) {
229 foreach ($this->getAttribute('arguments') as $argument) {
230 \array_shift($parameters);
231 }
232 }
233 $isPhpVariadic = \false;
234 if ($isVariadic) {
235 $argument = \end($parameters);
236 $isArray = $argument && $argument->hasType() && $argument->getType() instanceof \ReflectionNamedType && 'array' === $argument->getType()->getName();
237 if ($isArray && $argument->isDefaultValueAvailable() && [] === $argument->getDefaultValue()) {
238 \array_pop($parameters);
239 } elseif ($argument && $argument->isVariadic()) {
240 \array_pop($parameters);
241 $isPhpVariadic = \true;
242 } else {
243 throw new \LogicException(\sprintf('The last parameter of "%s" for %s "%s" must be an array with default value, eg. "array $arg = []".', $callableName, $this->getAttribute('type'), $this->getAttribute('name')));
244 }
245 }
246 return [$parameters, $isPhpVariadic];
247 }
248 private function reflectCallable($callable) : ReflectionCallable
249 {
250 if (!$this->reflector) {
251 $this->reflector = new ReflectionCallable($callable, $this->getAttribute('type'), $this->getAttribute('name'));
252 }
253 return $this->reflector;
254 }
255 }
256