1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace TwigJs\Compiler\Expression; |
20
|
|
|
|
21
|
|
|
use TwigJs\JsCompiler; |
22
|
|
|
use TwigJs\TypeCompilerInterface; |
23
|
|
|
|
24
|
|
|
class FunctionCompiler implements TypeCompilerInterface |
25
|
|
|
{ |
26
|
|
|
public function getType() |
27
|
|
|
{ |
28
|
|
|
return 'Twig_Node_Expression_Function'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) |
32
|
|
|
{ |
33
|
|
|
if (!$node instanceof \Twig_Node_Expression_Function) { |
34
|
|
|
throw new \RuntimeException( |
35
|
|
|
sprintf( |
36
|
|
|
'$node must be an instanceof of \Expression_Function, but got "%s".', |
37
|
|
|
get_class($node) |
38
|
|
|
) |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$function = $compiler->getEnvironment()->getFunction($node->getAttribute('name')); |
43
|
|
|
if (false === $function) { |
44
|
|
|
throw new \Twig_Error_Syntax( |
45
|
|
|
sprintf( |
46
|
|
|
'The function "%s" does not exist', |
47
|
|
|
$node->getAttribute('name') |
48
|
|
|
), |
49
|
|
|
$node->getLine() |
|
|
|
|
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($jsFunction = $compiler->getJsFunction($node->getAttribute('name'))) { |
54
|
|
|
$compiler->raw($jsFunction.'('); |
55
|
|
|
} else { |
56
|
|
|
$compiler |
57
|
|
|
->raw('this.env_.invoke(') |
58
|
|
|
->string($node->getAttribute('name')) |
59
|
|
|
->raw(', ') |
60
|
|
|
; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$compiler |
64
|
|
|
->raw($function->needsEnvironment() ? 'this.env_' : '') |
65
|
|
|
; |
66
|
|
|
|
67
|
|
|
if ($function->needsContext()) { |
68
|
|
|
$compiler->raw($function->needsEnvironment() ? ', context' : 'context'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$first = true; |
72
|
|
|
foreach ($node->getNode('arguments') as $argNode) { |
73
|
|
|
if (!$first) { |
74
|
|
|
$compiler->raw(', '); |
75
|
|
|
} else { |
76
|
|
|
if ($function->needsEnvironment() || $function->needsContext()) { |
77
|
|
|
$compiler->raw(', '); |
78
|
|
|
} |
79
|
|
|
$first = false; |
80
|
|
|
} |
81
|
|
|
$compiler->subcompile($argNode); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$compiler->raw(')'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.