1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
|
4
|
|
|
Copyrights for code authored by Yahoo! Inc. is licensed under the following terms: |
5
|
|
|
MIT License |
6
|
|
|
Copyright (c) 2013-2015 Yahoo! Inc. All Rights Reserved. |
7
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
8
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
9
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
10
|
|
|
|
11
|
|
|
Origin: https://github.com/zordius/lightncandy |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* file to handle LightnCandy Context |
16
|
|
|
* |
17
|
|
|
* @package LightnCandy |
18
|
|
|
* @author Zordius <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace LightnCandy; |
22
|
|
|
|
23
|
|
|
use \LightnCandy\Flags; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* LightnCandy class to handle Context |
27
|
|
|
*/ |
28
|
|
|
class Context extends Flags |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Create a context from options |
32
|
|
|
* |
33
|
|
|
* @param array<string,array|string|integer> $options input options |
34
|
|
|
* |
35
|
|
|
* @return array<string,array|string|integer> Context from options |
36
|
|
|
*/ |
37
|
632 |
|
public static function create($options) { |
38
|
632 |
|
if (!is_array($options)) { |
39
|
9 |
|
$options = array(); |
40
|
9 |
|
} |
41
|
|
|
|
42
|
632 |
|
$flags = isset($options['flags']) ? $options['flags'] : static::FLAG_BESTPERFORMANCE; |
43
|
|
|
|
44
|
|
|
$context = array( |
45
|
|
|
'flags' => array( |
46
|
632 |
|
'errorlog' => $flags & static::FLAG_ERROR_LOG, |
47
|
632 |
|
'exception' => $flags & static::FLAG_ERROR_EXCEPTION, |
48
|
632 |
|
'skippartial' => $flags & static::FLAG_ERROR_SKIPPARTIAL, |
49
|
632 |
|
'standalone' => $flags & static::FLAG_STANDALONE, |
50
|
632 |
|
'bare' => $flags & static::FLAG_BARE, |
51
|
632 |
|
'noesc' => $flags & static::FLAG_NOESCAPE, |
52
|
632 |
|
'jstrue' => $flags & static::FLAG_JSTRUE, |
53
|
632 |
|
'jsobj' => $flags & static::FLAG_JSOBJECT, |
54
|
632 |
|
'hbesc' => $flags & static::FLAG_HBESCAPE, |
55
|
632 |
|
'this' => $flags & static::FLAG_THIS, |
56
|
632 |
|
'nohbh' => $flags & static::FLAG_NOHBHELPERS, |
57
|
632 |
|
'parent' => $flags & static::FLAG_PARENT, |
58
|
632 |
|
'echo' => $flags & static::FLAG_ECHO, |
59
|
632 |
|
'advar' => $flags & static::FLAG_ADVARNAME, |
60
|
632 |
|
'namev' => $flags & static::FLAG_NAMEDARG, |
61
|
632 |
|
'spvar' => $flags & static::FLAG_SPVARS, |
62
|
632 |
|
'slash' => $flags & static::FLAG_SLASH, |
63
|
632 |
|
'else' => $flags & static::FLAG_ELSE, |
64
|
632 |
|
'exhlp' => $flags & static::FLAG_EXTHELPER, |
65
|
632 |
|
'lambda' => $flags & static::FLAG_HANDLEBARSLAMBDA, |
66
|
632 |
|
'mustlok' => $flags & static::FLAG_MUSTACHELOOKUP, |
67
|
632 |
|
'mustlam' => $flags & static::FLAG_MUSTACHELAMBDA, |
68
|
632 |
|
'noind' => $flags & static::FLAG_PREVENTINDENT, |
69
|
632 |
|
'debug' => $flags & static::FLAG_RENDER_DEBUG, |
70
|
632 |
|
'prop' => $flags & static::FLAG_PROPERTY, |
71
|
632 |
|
'method' => $flags & static::FLAG_METHOD, |
72
|
632 |
|
'runpart' => $flags & static::FLAG_RUNTIMEPARTIAL, |
73
|
632 |
|
'rawblock' => $flags & static::FLAG_RAWBLOCK, |
74
|
632 |
|
'partnc' => $flags & static::FLAG_PARTIALNEWCONTEXT, |
75
|
632 |
|
), |
76
|
632 |
|
'level' => 0, |
77
|
632 |
|
'stack' => array(), |
78
|
632 |
|
'currentToken' => null, |
79
|
632 |
|
'error' => array(), |
80
|
632 |
|
'basedir' => static::prepareBasedir($options), |
81
|
632 |
|
'fileext' => static::prepareFileext($options), |
82
|
|
|
'tokens' => array( |
83
|
632 |
|
'standalone' => true, |
84
|
632 |
|
'ahead' => false, |
85
|
632 |
|
'current' => 0, |
86
|
632 |
|
'count' => 0, |
87
|
632 |
|
'partialind' => '', |
88
|
632 |
|
), |
89
|
632 |
|
'usedPartial' => array(), |
90
|
632 |
|
'partialStack' => array(), |
91
|
632 |
|
'partialCode' => '', |
92
|
|
|
'usedFeature' => array( |
93
|
632 |
|
'rootthis' => 0, |
94
|
632 |
|
'enc' => 0, |
95
|
632 |
|
'raw' => 0, |
96
|
632 |
|
'sec' => 0, |
97
|
632 |
|
'isec' => 0, |
98
|
632 |
|
'if' => 0, |
99
|
632 |
|
'else' => 0, |
100
|
632 |
|
'unless' => 0, |
101
|
632 |
|
'each' => 0, |
102
|
632 |
|
'this' => 0, |
103
|
632 |
|
'parent' => 0, |
104
|
632 |
|
'with' => 0, |
105
|
632 |
|
'comment' => 0, |
106
|
632 |
|
'partial' => 0, |
107
|
632 |
|
'dynpartial' => 0, |
108
|
632 |
|
'helper' => 0, |
109
|
632 |
|
'bhelper' => 0, |
110
|
632 |
|
'hbhelper' => 0, |
111
|
632 |
|
'delimiter' => 0, |
112
|
632 |
|
'subexp' => 0, |
113
|
632 |
|
'rawblock' => 0, |
114
|
632 |
|
'lookup' => 0, |
115
|
632 |
|
), |
116
|
|
|
'usedCount' => array( |
117
|
632 |
|
'var' => array(), |
118
|
632 |
|
'helpers' => array(), |
119
|
632 |
|
'blockhelpers' => array(), |
120
|
632 |
|
'hbhelpers' => array(), |
121
|
632 |
|
'runtime' => array(), |
122
|
632 |
|
), |
123
|
632 |
|
'parsed' => array(), |
124
|
632 |
|
'partials' => (isset($options['partials']) && is_array($options['partials'])) ? $options['partials'] : array(), |
125
|
632 |
|
'helpers' => array(), |
126
|
632 |
|
'blockhelpers' => array(), |
127
|
632 |
|
'hbhelpers' => array(), |
128
|
632 |
|
'renderex' => isset($options['renderex']) ? $options['renderex'] : '', |
129
|
632 |
|
'prepartial' => (isset($options['prepartial']) && is_callable($options['prepartial'])) ? $options['prepartial'] : false, |
130
|
632 |
|
'runtime' => isset($options['runtime']) ? $options['runtime'] : '\\LightnCandy\\Runtime', |
131
|
632 |
|
'rawblock' => false, |
132
|
632 |
|
); |
133
|
|
|
|
134
|
632 |
|
$context['ops'] = $context['flags']['echo'] ? array( |
135
|
27 |
|
'seperator' => ',', |
136
|
27 |
|
'f_start' => 'echo ', |
137
|
27 |
|
'f_end' => ';', |
138
|
27 |
|
'op_start' => 'ob_start();echo ', |
139
|
27 |
|
'op_end' => ';return ob_get_clean();', |
140
|
27 |
|
'cnd_start' => ';if ', |
141
|
27 |
|
'cnd_then' => '{echo ', |
142
|
27 |
|
'cnd_else' => ';}else{echo ', |
143
|
27 |
|
'cnd_end' => ';}echo ', |
144
|
27 |
|
) : array( |
145
|
605 |
|
'seperator' => '.', |
146
|
605 |
|
'f_start' => 'return ', |
147
|
605 |
|
'f_end' => ';', |
148
|
605 |
|
'op_start' => 'return ', |
149
|
605 |
|
'op_end' => ';', |
150
|
605 |
|
'cnd_start' => '.(', |
151
|
605 |
|
'cnd_then' => ' ? ', |
152
|
605 |
|
'cnd_else' => ' : ', |
153
|
605 |
|
'cnd_end' => ').', |
154
|
605 |
|
); |
155
|
|
|
|
156
|
632 |
|
$context['ops']['enc'] = $context['flags']['hbesc'] ? 'encq' : 'enc'; |
157
|
632 |
|
static::updateHelperTable($context, $options); |
158
|
632 |
|
static::updateHelperTable($context, $options, 'blockhelpers'); |
159
|
632 |
|
static::updateHelperTable($context, $options, 'hbhelpers'); |
160
|
|
|
|
161
|
632 |
|
if ($context['flags']['partnc'] && ($context['flags']['runpart'] == 0)) { |
162
|
|
|
$context['error'][] = 'The FLAG_PARTIALNEWCONTEXT requires FLAG_RUNTIMEPARTIAL! Fix your compile options please'; |
163
|
|
|
} |
164
|
|
|
|
165
|
632 |
|
return $context; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* prepare list of template file extensions from options |
170
|
|
|
* |
171
|
|
|
* @param array<string,array|string|integer> $options current compile option |
172
|
|
|
* |
173
|
|
|
* @return array<string> file extensions |
174
|
|
|
* |
175
|
|
|
* @expect array('.tmpl') when input array() |
176
|
|
|
* @expect array('test') when input array('fileext' => 'test') |
177
|
|
|
* @expect array('test1') when input array('fileext' => array('test1')) |
178
|
|
|
* @expect array('test2', 'test3') when input array('fileext' => array('test2', 'test3')) |
179
|
|
|
*/ |
180
|
633 |
|
protected static function prepareFileExt($options) { |
181
|
633 |
|
$exts = isset($options['fileext']) ? $options['fileext'] : '.tmpl'; |
182
|
633 |
|
return is_array($exts) ? $exts : array($exts); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* prepare list of base directory from options |
187
|
|
|
* |
188
|
|
|
* @param array<string,array|string|integer> $options current compile option |
189
|
|
|
* |
190
|
|
|
* @return array<string> base directories |
191
|
|
|
* |
192
|
|
|
* @expect array() when input array() |
193
|
|
|
* @expect array() when input array('basedir' => array()) |
194
|
|
|
* @expect array('src') when input array('basedir' => array('src')) |
195
|
|
|
* @expect array('src') when input array('basedir' => array('src', 'dir_not_found')) |
196
|
|
|
* @expect array('src', 'tests') when input array('basedir' => array('src', 'tests')) |
197
|
|
|
*/ |
198
|
633 |
|
protected static function prepareBaseDir($options) { |
199
|
633 |
|
$dirs = isset($options['basedir']) ? $options['basedir'] : 0; |
200
|
633 |
|
$dirs = is_array($dirs) ? $dirs : array($dirs); |
201
|
633 |
|
$ret = array(); |
202
|
|
|
|
203
|
633 |
|
foreach ($dirs as $dir) { |
204
|
633 |
|
if (is_string($dir) && is_dir($dir)) { |
205
|
424 |
|
$ret[] = $dir; |
206
|
424 |
|
} |
207
|
633 |
|
} |
208
|
|
|
|
209
|
633 |
|
return $ret; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* update specific custom helper table from options |
214
|
|
|
* |
215
|
|
|
* @param array<string,array|string|integer> $context prepared context |
216
|
|
|
* @param array<string,array|string|integer> $options input options |
217
|
|
|
* @param string $tname helper table name |
218
|
|
|
* |
219
|
|
|
* @return array<string,array|string|integer> context with generated helper table |
220
|
|
|
* |
221
|
|
|
* @expect array() when input array(), array() |
222
|
|
|
* @expect array('flags' => array('exhlp' => 1)) when input array('flags' => array('exhlp' => 1)), array('helpers' => array('abc')) |
223
|
|
|
* @expect array('error' => array('You provide a custom helper named as \'abc\' in options[\'helpers\'], but the function abc() is not defined!'), 'flags' => array('exhlp' => 0)) when input array('error' => array(), 'flags' => array('exhlp' => 0)), array('helpers' => array('abc')) |
224
|
|
|
* @expect array('flags' => array('exhlp' => 1), 'helpers' => array('\\LightnCandy\\Runtime::raw' => '\\LightnCandy\\Runtime::raw')) when input array('flags' => array('exhlp' => 1), 'helpers' => array()), array('helpers' => array('\\LightnCandy\\Runtime::raw')) |
225
|
|
|
* @expect array('flags' => array('exhlp' => 1), 'helpers' => array('test' => '\\LightnCandy\\Runtime::raw')) when input array('flags' => array('exhlp' => 1), 'helpers' => array()), array('helpers' => array('test' => '\\LightnCandy\\Runtime::raw')) |
226
|
|
|
*/ |
227
|
633 |
|
protected static function updateHelperTable(&$context, $options, $tname = 'helpers') { |
228
|
633 |
|
if (isset($options[$tname]) && is_array($options[$tname])) { |
229
|
374 |
|
foreach ($options[$tname] as $name => $func) { |
|
|
|
|
230
|
173 |
|
$tn = is_int($name) ? $func : $name; |
231
|
173 |
|
if (is_callable($func)) { |
232
|
171 |
|
$context[$tname][$tn] = $func; |
233
|
171 |
|
} else { |
234
|
4 |
|
if (is_array($func)) { |
235
|
1 |
|
$context['error'][] = "I found an array in $tname with key as $name, please fix it."; |
236
|
1 |
|
} else { |
237
|
3 |
|
if (!$context['flags']['exhlp']) { |
238
|
2 |
|
$context['error'][] = "You provide a custom helper named as '$tn' in options['$tname'], but the function $func() is not defined!"; |
239
|
2 |
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
374 |
|
} |
243
|
374 |
|
} |
244
|
633 |
|
return $context; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Merge a context into another |
249
|
|
|
* |
250
|
|
|
* @param array<string,array|string|integer> $context master context |
251
|
|
|
* @param array<string,array|string|integer> $tmp another context will be overwrited into master context |
252
|
|
|
*/ |
253
|
54 |
|
public static function merge(&$context, $tmp) { |
254
|
54 |
|
$context['usedFeature'] = $tmp['usedFeature']; |
255
|
54 |
|
$context['usedCount'] = $tmp['usedCount']; |
256
|
54 |
|
$context['partialStack'] = $tmp['partialStack']; |
257
|
54 |
|
$context['partialCode'] = $tmp['partialCode']; |
258
|
54 |
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.