|
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\PhpParser\NodeVisitor\Collection\WhitelistedFunctionCollection; |
|
18
|
|
|
use Humbug\PhpScoper\Whitelist; |
|
19
|
|
|
use PhpParser\Node\Name\FullyQualified; |
|
20
|
|
|
use const PHP_EOL; |
|
21
|
|
|
use function array_map; |
|
22
|
|
|
use function array_unshift; |
|
23
|
|
|
use function count; |
|
24
|
1 |
|
use function iterator_to_array; |
|
25
|
|
|
use function sprintf; |
|
26
|
1 |
|
use function str_repeat; |
|
27
|
|
|
use function str_replace; |
|
28
|
|
|
|
|
29
|
1 |
|
final class ScoperAutoloadGenerator |
|
30
|
|
|
{ |
|
31
|
1 |
|
private $whitelist; |
|
32
|
|
|
|
|
33
|
1 |
|
public function __construct(Whitelist $whitelist) |
|
34
|
1 |
|
{ |
|
35
|
1 |
|
$this->whitelist = $whitelist; |
|
36
|
1 |
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
public function dump(string $prefix): string |
|
39
|
1 |
|
{ |
|
40
|
|
|
$whitelistedFunctions = $this->whitelist->getWhitelistedFunctions(); |
|
41
|
|
|
|
|
42
|
1 |
|
$hasNamespacedFunctions = $this->hasNamespacedFunctions($whitelistedFunctions); |
|
43
|
|
|
|
|
44
|
|
|
$statements = implode(PHP_EOL, $this->createClassAliasStatements($prefix, $hasNamespacedFunctions)).PHP_EOL.PHP_EOL; |
|
45
|
|
|
$statements .= implode(PHP_EOL, $this->createFunctionAliasStatements($whitelistedFunctions, $hasNamespacedFunctions)); |
|
46
|
|
|
|
|
47
|
|
|
if ($hasNamespacedFunctions) { |
|
48
|
|
|
$dump = <<<PHP |
|
49
|
|
|
<?php |
|
50
|
|
|
|
|
51
|
1 |
|
// scoper-autoload.php @generated by PhpScoper |
|
52
|
|
|
|
|
53
|
|
|
namespace { |
|
54
|
|
|
\$loader = require_once __DIR__.'/autoload.php'; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$statements |
|
58
|
|
|
|
|
59
|
|
|
namespace { |
|
60
|
|
|
return \$loader; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
PHP; |
|
64
|
|
|
} else { |
|
65
|
|
|
$dump = <<<PHP |
|
66
|
|
|
<?php |
|
67
|
|
|
|
|
68
|
|
|
// scoper-autoload.php @generated by PhpScoper |
|
69
|
|
|
|
|
70
|
|
|
\$loader = require_once __DIR__.'/autoload.php'; |
|
71
|
|
|
|
|
72
|
|
|
$statements |
|
73
|
|
|
|
|
74
|
|
|
return \$loader; |
|
75
|
|
|
|
|
76
|
|
|
PHP; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$dump = $this->cleanAutoload($dump); |
|
80
|
|
|
|
|
81
|
|
|
return $dump; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return string[] |
|
86
|
|
|
*/ |
|
87
|
|
|
private function createClassAliasStatements(string $prefix, bool $hasNamespacedFunctions): array |
|
88
|
|
|
{ |
|
89
|
|
|
$statements = array_map( |
|
90
|
|
|
function (string $whitelistedElement) use ($prefix): string { |
|
91
|
|
|
return sprintf( |
|
92
|
|
|
'class_exists(\'%s\%s\');', |
|
93
|
|
|
$prefix, |
|
94
|
|
|
$whitelistedElement |
|
95
|
|
|
); |
|
96
|
|
|
}, |
|
97
|
|
|
$this->whitelist->getClassWhitelistArray() |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
if ([] === $statements) { |
|
101
|
|
|
return $statements; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if ($hasNamespacedFunctions) { |
|
105
|
|
|
$statements = array_map( |
|
106
|
|
|
function (string $statement): string { |
|
107
|
|
|
return str_repeat(' ', 4).$statement; |
|
108
|
|
|
}, |
|
109
|
|
|
$statements |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
array_unshift($statements, 'namespace {'); |
|
113
|
|
|
$statements[] = '}'.PHP_EOL; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
array_unshift( |
|
117
|
|
|
$statements, |
|
118
|
|
|
<<<'EOF' |
|
119
|
|
|
// Aliases for the whitelisted classes. For more information see: |
|
120
|
|
|
// https://github.com/humbug/php-scoper/blob/master/README.md#class-whitelisting |
|
121
|
|
|
EOF |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
return $statements; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return string[] |
|
129
|
|
|
*/ |
|
130
|
|
|
private function createFunctionAliasStatements( |
|
131
|
|
|
WhitelistedFunctionCollection $whitelistedFunctions, |
|
132
|
|
|
bool $hasNamespacedFunctions |
|
133
|
|
|
): array { |
|
134
|
|
|
$statements = array_map( |
|
135
|
|
|
function (array $node) use ($hasNamespacedFunctions): string { |
|
136
|
|
|
/** |
|
137
|
|
|
* @var FullyQualified |
|
138
|
|
|
* @var FullyQualified $alias |
|
139
|
|
|
*/ |
|
140
|
|
|
[$original, $alias] = $node; |
|
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
if ($hasNamespacedFunctions) { |
|
143
|
|
|
$namespace = $original->slice(0, -1); |
|
144
|
|
|
|
|
145
|
|
|
return sprintf( |
|
146
|
|
|
<<<'PHP' |
|
147
|
|
|
namespace %s{ |
|
148
|
|
|
if (!function_exists('%s')) { |
|
149
|
|
|
function %s() { |
|
150
|
|
|
return \%s(...func_get_args()); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
PHP |
|
155
|
|
|
, |
|
156
|
|
|
null === $namespace ? '' : $namespace->toString().' ', |
|
157
|
|
|
$original->toString(), |
|
158
|
|
|
null === $namespace ? $original->toString() : $original->slice(1)->toString(), |
|
159
|
|
|
$alias->toString() |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return sprintf( |
|
164
|
|
|
<<<'PHP' |
|
165
|
|
|
if (!function_exists('%1$s')) { |
|
166
|
|
|
function %1$s() { |
|
167
|
|
|
return \%2$s(...func_get_args()); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
PHP |
|
171
|
|
|
, |
|
172
|
|
|
$original->toString(), |
|
173
|
|
|
$alias->toString() |
|
174
|
|
|
); |
|
175
|
|
|
}, |
|
176
|
|
|
iterator_to_array($whitelistedFunctions) |
|
177
|
|
|
); |
|
178
|
|
|
|
|
179
|
|
|
if ([] === $statements) { |
|
180
|
|
|
return $statements; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
array_unshift( |
|
184
|
|
|
$statements, |
|
185
|
|
|
<<<'EOF' |
|
186
|
|
|
// Functions whitelisting. For more information see: |
|
187
|
|
|
// https://github.com/humbug/php-scoper/blob/master/README.md#functions-whitelisting |
|
188
|
|
|
EOF |
|
189
|
|
|
); |
|
190
|
|
|
|
|
191
|
|
|
return $statements; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
private function hasNamespacedFunctions(WhitelistedFunctionCollection $functions): bool |
|
195
|
|
|
{ |
|
196
|
|
|
foreach ($functions as [$original, $alias]) { |
|
197
|
|
|
/** |
|
198
|
|
|
* @var FullyQualified |
|
199
|
|
|
* @var FullyQualified $alias |
|
200
|
|
|
*/ |
|
201
|
|
|
if (count($original->parts) > 1) { |
|
|
|
|
|
|
202
|
|
|
return true; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return false; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
private function cleanAutoload(string $dump): string |
|
210
|
|
|
{ |
|
211
|
|
|
$cleanedDump = $dump; |
|
212
|
|
|
|
|
213
|
|
|
do { |
|
214
|
|
|
$dump = $cleanedDump; |
|
215
|
|
|
$cleanedDump = str_replace("\n\n\n", "\n\n", $dump); |
|
216
|
|
|
} while ($cleanedDump !== $dump); |
|
217
|
|
|
|
|
218
|
|
|
return $dump; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.