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 Exporter |
16
|
|
|
* |
17
|
|
|
* @package LightnCandy |
18
|
|
|
* @author Zordius <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace LightnCandy; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* LightnCandy major static class |
25
|
|
|
*/ |
26
|
|
|
class Exporter |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Get PHP code string from a closure of function as string |
30
|
|
|
* |
31
|
|
|
* @param object $closure Closure object |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
* |
35
|
|
|
* @expect 'function($a) {return;}' when input function ($a) {return;} |
36
|
|
|
* @expect 'function($a) {return;}' when input function ($a) {return;} |
37
|
|
|
*/ |
38
|
64 |
|
protected static function closure($closure) { |
39
|
64 |
|
if (is_string($closure) && preg_match('/(.+)::(.+)/', $closure, $matched)) { |
40
|
2 |
|
$ref = new \ReflectionMethod($matched[1], $matched[2]); |
41
|
|
|
} else { |
42
|
63 |
|
$ref = new \ReflectionFunction($closure); |
43
|
|
|
} |
44
|
64 |
|
$fname = $ref->getFileName(); |
45
|
|
|
|
46
|
64 |
|
$lines = file_get_contents($fname); |
47
|
64 |
|
$file = new \SplFileObject($fname); |
48
|
64 |
|
$file->seek($ref->getStartLine() - 2); |
49
|
64 |
|
$spos = $file->ftell(); |
50
|
64 |
|
$file->seek($ref->getEndLine() - 1); |
51
|
64 |
|
$epos = $file->ftell(); |
52
|
|
|
|
53
|
64 |
|
return preg_replace('/^.*?function(\s+[^\s\\(]+?)?\s*?\\((.+?)\\}[,\\s]*;?$/s', 'function($2}', substr($lines, $spos, $epos - $spos)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Export required custom helper functions |
58
|
|
|
* |
59
|
|
|
* @param array<string,array|string|integer> $context current compile context |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
648 |
|
public static function helpers($context) { |
64
|
648 |
|
$ret = ''; |
65
|
648 |
|
foreach ($context['helpers'] as $name => $func) { |
|
|
|
|
66
|
185 |
|
if (!isset($context['usedCount']['helpers'][$name])) { |
67
|
33 |
|
continue; |
68
|
|
|
} |
69
|
171 |
|
if ((is_object($func) && ($func instanceof \Closure)) || ($context['flags']['exhlp'] == 0)) { |
70
|
63 |
|
$ret .= (" '$name' => " . static::closure($func) . ",\n"); |
71
|
63 |
|
continue; |
72
|
|
|
} |
73
|
108 |
|
$ret .= " '$name' => '$func',\n"; |
74
|
|
|
} |
75
|
|
|
|
76
|
648 |
|
return "array($ret)"; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Export required standalone Runtime methods |
81
|
|
|
* |
82
|
|
|
* @param array<string,array|string|integer> $context current compile context |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
516 |
|
public static function runtime($context) { |
87
|
516 |
|
$class = new \ReflectionClass($context['runtime']); |
88
|
516 |
|
$methods = array(); |
89
|
516 |
|
$ret = ''; |
90
|
|
|
|
91
|
516 |
|
foreach ($class->getMethods() as $method) { |
92
|
516 |
|
$C = $method->getDeclaringClass(); |
93
|
516 |
|
$fname = $C->getFileName(); |
94
|
516 |
|
$lines = file_get_contents($fname); |
95
|
516 |
|
$file = new \SplFileObject($fname); |
96
|
516 |
|
$name = $method->getName(); |
97
|
516 |
|
$file->seek($method->getStartLine() - 2); |
98
|
516 |
|
$spos = $file->ftell(); |
99
|
516 |
|
$file->seek($method->getEndLine() - 2); |
100
|
516 |
|
$epos = $file->ftell(); |
101
|
516 |
|
$methods[$name] = static::scanDependency($context, preg_replace('/public static function (.+)\\(/', "function {$context['funcprefix']}\$1(", substr($lines, $spos, $epos - $spos))); |
102
|
|
|
} |
103
|
516 |
|
unset($file); |
104
|
|
|
|
105
|
516 |
|
$exports = array_keys($context['usedCount']['runtime']); |
106
|
|
|
|
107
|
516 |
|
while (true) { |
108
|
|
|
if (array_sum(array_map(function ($name) use (&$exports, $methods) { |
109
|
475 |
|
$n = 0; |
110
|
475 |
|
foreach ($methods[$name][1] as $child => $count) { |
111
|
474 |
|
if (!in_array($child, $exports)) { |
112
|
474 |
|
$exports[] = $child; |
113
|
474 |
|
$n++; |
114
|
|
|
} |
115
|
|
|
} |
116
|
475 |
|
return $n; |
117
|
516 |
|
}, $exports)) == 0) { |
118
|
516 |
|
break; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
516 |
|
foreach ($exports as $export) { |
123
|
475 |
|
$ret .= ($methods[$export][0] . " }\n"); |
124
|
|
|
} |
125
|
|
|
|
126
|
516 |
|
return $ret; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Export Runtime constants |
131
|
|
|
* |
132
|
|
|
* @param array<string,array|string|integer> $context current compile context |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
648 |
|
public static function constants($context) { |
137
|
648 |
|
if ($context['flags']['standalone'] == 0) { |
138
|
622 |
|
return 'array()'; |
139
|
|
|
} |
140
|
|
|
|
141
|
516 |
|
$class = new \ReflectionClass($context['runtime']); |
142
|
516 |
|
$constants = $class->getConstants(); |
143
|
516 |
|
$ret = " array(\n"; |
144
|
516 |
|
foreach($constants as $name => $value) { |
145
|
516 |
|
$ret .= " '$name' => ". (is_string($value) ? "'$value'" : $value ) . ",\n"; |
146
|
|
|
} |
147
|
516 |
|
$ret .= " )"; |
148
|
516 |
|
return $ret; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Scan for required standalone functions |
153
|
|
|
* |
154
|
|
|
* @param array<string,array|string|integer> $context current compile context |
155
|
|
|
* @param string $code PHP code string of the method |
156
|
|
|
* |
157
|
|
|
* @return array<string|array> list of converted code and children array |
158
|
|
|
*/ |
159
|
516 |
|
protected static function scanDependency($context, $code) { |
160
|
516 |
|
$child = array(); |
161
|
|
|
|
162
|
516 |
|
$code = preg_replace_callback('/static::(\w+?)\s*\(/', function ($matches) use ($context, &$child) { |
163
|
516 |
|
if (!isset($child[$matches[1]])) { |
164
|
516 |
|
$child[$matches[1]] = 0; |
165
|
|
|
} |
166
|
516 |
|
$child[$matches[1]]++; |
167
|
|
|
|
168
|
516 |
|
return "{$context['funcprefix']}{$matches[1]}("; |
169
|
516 |
|
}, $code); |
170
|
|
|
|
171
|
|
|
// replace the constants |
172
|
516 |
|
$code = preg_replace('/static::([A-Z0-9_]+)/', "\$cx['constants']['$1']", $code); |
173
|
|
|
|
174
|
|
|
// compress space |
175
|
516 |
|
$code = preg_replace('/ /', ' ', $code); |
176
|
|
|
|
177
|
516 |
|
return array($code, $child); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
|
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.