1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the humbug/php-scoper package. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2017 Théo FIDRY <[email protected]>, |
9
|
|
|
* Pádraic Brady <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Humbug\PhpScoper\Autoload; |
16
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\Whitelist; |
18
|
|
|
use PhpParser\Node\Name\FullyQualified; |
19
|
|
|
use const PHP_EOL; |
20
|
|
|
use function array_column; |
21
|
|
|
use function array_map; |
22
|
|
|
use function array_unshift; |
23
|
|
|
use function sprintf; |
24
|
|
|
use function str_repeat; |
25
|
|
|
use function str_replace; |
26
|
|
|
use function strpos; |
27
|
|
|
|
28
|
|
|
final class ScoperAutoloadGenerator |
29
|
|
|
{ |
30
|
|
|
private $whitelist; |
31
|
|
|
|
32
|
7 |
|
public function __construct(Whitelist $whitelist) |
33
|
|
|
{ |
34
|
7 |
|
$this->whitelist = $whitelist; |
35
|
|
|
} |
36
|
|
|
|
37
|
7 |
|
public function dump(string $prefix): string |
|
|
|
|
38
|
|
|
{ |
39
|
7 |
|
$whitelistedFunctions = $this->whitelist->getRecordedWhitelistedFunctions(); |
40
|
|
|
|
41
|
7 |
|
$hasNamespacedFunctions = $this->hasNamespacedFunctions($whitelistedFunctions); |
42
|
|
|
|
43
|
7 |
|
$statements = implode( |
44
|
7 |
|
PHP_EOL, |
45
|
7 |
|
$this->createClassAliasStatements( |
46
|
7 |
|
$this->whitelist->getRecordedWhitelistedClasses(), |
47
|
7 |
|
$hasNamespacedFunctions) |
48
|
|
|
) |
49
|
7 |
|
.PHP_EOL |
50
|
7 |
|
.PHP_EOL |
51
|
|
|
; |
52
|
7 |
|
$statements .= implode( |
53
|
7 |
|
PHP_EOL, |
54
|
7 |
|
$this->createFunctionAliasStatements( |
55
|
7 |
|
$whitelistedFunctions, |
56
|
7 |
|
$hasNamespacedFunctions |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
|
60
|
7 |
|
if ($hasNamespacedFunctions) { |
61
|
|
|
$dump = <<<PHP |
62
|
|
|
<?php |
63
|
|
|
|
64
|
|
|
// scoper-autoload.php @generated by PhpScoper |
65
|
|
|
|
66
|
|
|
namespace { |
67
|
|
|
\$loader = require_once __DIR__.'/autoload.php'; |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
$statements |
71
|
|
|
|
72
|
|
|
namespace { |
73
|
|
|
return \$loader; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
PHP; |
77
|
|
|
} else { |
78
|
|
|
$dump = <<<PHP |
79
|
|
|
<?php |
80
|
|
|
|
81
|
|
|
// scoper-autoload.php @generated by PhpScoper |
82
|
|
|
|
83
|
|
|
\$loader = require_once __DIR__.'/autoload.php'; |
84
|
|
|
|
85
|
5 |
|
$statements |
86
|
|
|
|
87
|
|
|
return \$loader; |
88
|
|
|
|
89
|
|
|
PHP; |
90
|
|
|
} |
91
|
|
|
|
92
|
7 |
|
$dump = $this->cleanAutoload($dump); |
93
|
|
|
|
94
|
7 |
|
return $dump; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string[] |
99
|
|
|
*/ |
100
|
7 |
|
private function createClassAliasStatements(array $whitelistedClasses, bool $hasNamespacedFunctions): array |
101
|
|
|
{ |
102
|
7 |
|
$statements = array_map( |
103
|
|
|
function (string $prefixedClass): string { |
104
|
3 |
|
return sprintf( |
105
|
3 |
|
'class_exists(\'%s\');', |
106
|
3 |
|
$prefixedClass |
107
|
|
|
); |
108
|
7 |
|
}, |
109
|
7 |
|
array_column( |
110
|
7 |
|
$whitelistedClasses, |
111
|
7 |
|
1 |
112
|
|
|
) |
113
|
|
|
); |
114
|
|
|
|
115
|
7 |
|
if ([] === $statements) { |
116
|
4 |
|
return $statements; |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
if ($hasNamespacedFunctions) { |
120
|
1 |
|
$statements = array_map( |
121
|
|
|
function (string $statement): string { |
122
|
1 |
|
return str_repeat(' ', 4).$statement; |
123
|
1 |
|
}, |
124
|
1 |
|
$statements |
125
|
|
|
); |
126
|
|
|
|
127
|
1 |
|
array_unshift($statements, 'namespace {'); |
128
|
1 |
|
$statements[] = '}'.PHP_EOL; |
129
|
|
|
} |
130
|
|
|
|
131
|
3 |
|
array_unshift( |
132
|
3 |
|
$statements, |
133
|
|
|
<<<'EOF' |
134
|
3 |
|
// Aliases for the whitelisted classes. For more information see: |
135
|
|
|
// https://github.com/humbug/php-scoper/blob/master/README.md#class-whitelisting |
136
|
|
|
EOF |
137
|
|
|
); |
138
|
|
|
|
139
|
3 |
|
return $statements; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string[] |
144
|
|
|
*/ |
145
|
7 |
|
private function createFunctionAliasStatements(array $whitelistedFunctions, bool $hasNamespacedFunctions): array |
146
|
|
|
{ |
147
|
7 |
|
$statements = array_map( |
148
|
|
|
function (array $node) use ($hasNamespacedFunctions): string { |
149
|
|
|
/** |
150
|
|
|
* @var string |
151
|
|
|
* @var string $alias |
152
|
|
|
*/ |
153
|
3 |
|
[$original, $alias] = $node; |
|
|
|
|
154
|
|
|
|
155
|
3 |
|
$original = new FullyQualified($original); |
|
|
|
|
156
|
3 |
|
$alias = new FullyQualified($alias); |
|
|
|
|
157
|
|
|
|
158
|
3 |
|
if ($hasNamespacedFunctions) { |
159
|
2 |
|
$namespace = $original->slice(0, -1); |
160
|
|
|
|
161
|
2 |
|
return sprintf( |
162
|
|
|
<<<'PHP' |
163
|
2 |
|
namespace %s{ |
164
|
|
|
if (!function_exists('%s')) { |
165
|
|
|
function %s() { |
166
|
|
|
return \%s(...func_get_args()); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
PHP |
171
|
|
|
, |
172
|
2 |
|
null === $namespace ? '' : $namespace->toString().' ', |
173
|
2 |
|
$original->toString(), |
174
|
2 |
|
null === $namespace ? $original->toString() : $original->slice(1)->toString(), |
175
|
2 |
|
$alias->toString() |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
return sprintf( |
180
|
|
|
<<<'PHP' |
181
|
1 |
|
if (!function_exists('%1$s')) { |
182
|
|
|
function %1$s() { |
183
|
|
|
return \%2$s(...func_get_args()); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
PHP |
187
|
|
|
, |
188
|
1 |
|
$original, |
189
|
1 |
|
$alias |
190
|
|
|
); |
191
|
7 |
|
}, |
192
|
7 |
|
$whitelistedFunctions |
193
|
|
|
); |
194
|
|
|
|
195
|
7 |
|
if ([] === $statements) { |
196
|
4 |
|
return $statements; |
197
|
|
|
} |
198
|
|
|
|
199
|
3 |
|
array_unshift( |
200
|
3 |
|
$statements, |
201
|
|
|
<<<'EOF' |
202
|
3 |
|
// Functions whitelisting. For more information see: |
203
|
|
|
// https://github.com/humbug/php-scoper/blob/master/README.md#functions-whitelisting |
204
|
|
|
EOF |
205
|
|
|
); |
206
|
|
|
|
207
|
3 |
|
return $statements; |
208
|
|
|
} |
209
|
|
|
|
210
|
7 |
|
private function hasNamespacedFunctions(array $functions): bool |
211
|
|
|
{ |
212
|
7 |
|
foreach ($functions as [$original, $alias]) { |
213
|
|
|
/* |
214
|
|
|
* @var string |
215
|
|
|
* @var string $alias |
216
|
|
|
*/ |
217
|
3 |
|
if (false !== strpos($original, '\\')) { |
|
|
|
|
218
|
3 |
|
return true; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
5 |
|
return false; |
223
|
|
|
} |
224
|
|
|
|
225
|
7 |
|
private function cleanAutoload(string $dump): string |
226
|
|
|
{ |
227
|
7 |
|
$cleanedDump = $dump; |
228
|
|
|
|
229
|
|
|
do { |
230
|
7 |
|
$dump = $cleanedDump; |
231
|
7 |
|
$cleanedDump = str_replace("\n\n\n", "\n\n", $dump); |
232
|
7 |
|
} while ($cleanedDump !== $dump); |
233
|
|
|
|
234
|
7 |
|
return $dump; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.