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 keep LightnCandy partial methods |
16
|
|
|
* |
17
|
|
|
* @package LightnCandy |
18
|
|
|
* @author Zordius <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace LightnCandy; |
22
|
|
|
|
23
|
|
|
use \LightnCandy\Compiler; |
24
|
|
|
use \LightnCandy\SafeString; |
25
|
|
|
use \LightnCandy\Context; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* LightnCandy Partial handler |
29
|
|
|
*/ |
30
|
|
|
class Partial |
31
|
|
|
{ |
32
|
|
|
public static $TMP_JS_FUNCTION_STR = "!!\aFuNcTiOn\a!!"; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Include all partials when using dynamic partials |
36
|
|
|
*/ |
37
|
634 |
|
public static function handleDynamicPartial(&$context) { |
38
|
634 |
|
if ($context['usedFeature']['dynpartial'] == 0) { |
39
|
630 |
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
4 |
|
foreach ($context['partials'] as $name => $code) { |
43
|
4 |
|
static::readPartial($name, $context); |
44
|
|
|
} |
45
|
4 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Read partial file content as string and store in context |
49
|
|
|
* |
50
|
|
|
* @param string $name partial name |
51
|
|
|
* @param array<string,array|string|integer> $context Current context of compiler progress. |
52
|
|
|
*/ |
53
|
75 |
|
public static function readPartial($name, &$context) { |
54
|
75 |
|
$context['usedFeature']['partial']++; |
55
|
|
|
|
56
|
75 |
|
if (isset($context['usedPartial'][$name])) { |
57
|
15 |
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
75 |
|
if (isset($context['inlines'][$name])) { |
61
|
|
|
return; |
62
|
75 |
|
} |
63
|
70 |
|
|
64
|
70 |
|
$cnt = static::resolvePartial($name, $context); |
65
|
|
|
|
66
|
|
|
if ($cnt !== null) { |
67
|
5 |
|
$context['usedPartial'][$name] = SafeString::escapeTemplate($cnt); |
68
|
4 |
|
return static::compileDynamic($name, $context); |
69
|
|
|
} |
70
|
5 |
|
|
71
|
|
|
if (!$context['flags']['skippartial']) { |
72
|
|
|
$context['error'][] = "Can not find partial file for '$name', you should set correct basedir and fileext in options"; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* preprocess partial template before it be stored into context |
78
|
|
|
* |
79
|
|
|
* @param string $tmpl partial template |
80
|
|
|
* @param string $name partial name |
81
|
|
|
* @param array<string,array|string|integer> $context Current context of compiler progress. |
82
|
|
|
* |
83
|
|
|
* @return string|null $content processed partial template |
84
|
71 |
|
* |
85
|
71 |
|
* @expect 'hey' when input 'hey', 'haha', Array('prepartial' => false) |
86
|
|
|
* @expect 'haha-hoho' when input 'hoho', 'haha', Array('prepartial' => function ($tmpl, $name) {return "$name-$tmpl";}) |
87
|
|
|
*/ |
88
|
|
|
protected static function prePartial($tmpl, &$name, &$context) { |
89
|
|
|
return $context['prepartial'] ? $context['prepartial']($tmpl, $name, $context) : $tmpl; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* locate partial file, return the file name |
94
|
|
|
* |
95
|
|
|
* @param string $name partial name |
96
|
78 |
|
* @param array<string,array|string|integer> $context Current context of compiler progress. |
97
|
78 |
|
* |
98
|
63 |
|
* @return string|null $content partial content |
99
|
|
|
*/ |
100
|
|
|
public static function resolvePartial(&$name, &$context) { |
101
|
15 |
|
if (isset($context['partials'][$name])) { |
102
|
14 |
|
return static::prePartial($context['partials'][$name], $name, $context); |
103
|
14 |
|
} |
104
|
14 |
|
|
105
|
14 |
|
foreach ($context['basedir'] as $dir) { |
|
|
|
|
106
|
|
|
foreach ($context['fileext'] as $ext) { |
|
|
|
|
107
|
|
|
$fn = "$dir/$name$ext"; |
108
|
|
|
if (file_exists($fn)) { |
109
|
8 |
|
return static::prePartial(file_get_contents($fn), $name, $context); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* compile a partial to static embed PHP code |
118
|
|
|
* |
119
|
|
|
* @param array<string,array|string|integer> $context Current context of compiler progress. |
120
|
7 |
|
* @param string $name partial name |
121
|
|
|
* |
122
|
7 |
|
* @return string|null $code PHP code string |
123
|
7 |
|
*/ |
124
|
7 |
|
public static function compileStatic(&$context, $name) { |
125
|
7 |
|
// Check for recursive partial |
126
|
1 |
|
if (!$context['flags']['runpart']) { |
127
|
|
|
$context['partialStack'][] = $name; |
128
|
|
|
$diff = count($context['partialStack']) - count(array_unique($context['partialStack'])); |
129
|
|
|
if ($diff) { |
130
|
7 |
|
$context['error'][] = 'I found recursive partial includes as the path: ' . implode(' -> ', $context['partialStack']) . '! You should fix your template or compile with LightnCandy::FLAG_RUNTIMEPARTIAL flag.'; |
131
|
|
|
} |
132
|
7 |
|
} |
133
|
7 |
|
|
134
|
|
|
$code = Compiler::compileTemplate($context, preg_replace('/^/m', $context['tokens']['partialind'], $context['usedPartial'][$name])); |
135
|
|
|
|
136
|
7 |
|
if (!$context['flags']['runpart']) { |
137
|
|
|
array_pop($context['partialStack']); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $code; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* compile partial file, stored in context |
145
|
73 |
|
* |
146
|
73 |
|
* @param string $name partial name |
147
|
8 |
|
* @param array<string,array|string|integer> $context Current context of compiler progress. |
148
|
|
|
*/ |
149
|
|
|
public static function compileDynamic($name, &$context) { |
150
|
65 |
|
if (!$context['flags']['runpart']) { |
151
|
65 |
|
return; |
152
|
65 |
|
} |
153
|
65 |
|
|
154
|
60 |
|
$func = static::compileLocal($context, $context['usedPartial'][$name]); |
155
|
60 |
|
$context['partialCode'] .= "'$name' => $func,"; |
156
|
|
|
} |
157
|
60 |
|
|
158
|
|
|
/** |
159
|
5 |
|
* compile a template into a closure function |
160
|
|
|
* |
161
|
65 |
|
* @param array<string,array|string|integer> $context Current context of compiler progress. |
162
|
65 |
|
* @param string $template template string |
163
|
65 |
|
* |
164
|
|
|
* @return array<string> $content array of code and space param string |
165
|
|
|
*/ |
166
|
|
|
public static function compileLocal(&$context, $template) { |
167
|
|
|
$tmpContext = $context; |
168
|
|
|
$tmpContext['inlinepartial'] = array(); |
169
|
|
|
$tmpContext['partialblock'] = array(); |
170
|
|
|
$code = Compiler::compileTemplate($tmpContext, str_replace('function', static::$TMP_JS_FUNCTION_STR, $template)); |
171
|
|
|
Context::merge($context, $tmpContext); |
172
|
|
|
if (!$context['flags']['noind']) { |
173
|
|
|
$sp = ', $sp'; |
174
|
|
|
$code = preg_replace('/^/m', "'{$context['ops']['seperator']}\$sp{$context['ops']['seperator']}'", $code); |
175
|
|
|
// callbacks inside partial should be aware of $sp |
176
|
|
|
$code = preg_replace('/\bfunction\s*\((.*?)\)\s*{/', 'function(\\1)use($sp){', $code); |
177
|
|
|
} else { |
178
|
|
|
$sp = ''; |
179
|
|
|
} |
180
|
|
|
$code = str_replace(static::$TMP_JS_FUNCTION_STR, 'function', $code); |
181
|
|
|
return "function (\$cx, \$in{$sp}) {{$context['ops']['op_start']}'$code'{$context['ops']['op_end']}}"; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
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.