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\UserGlobalFunctionCollection; |
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 iterator_to_array; |
24
|
1 |
|
use function sprintf; |
25
|
|
|
use function str_replace; |
26
|
1 |
|
|
27
|
|
|
final class ScoperAutoloadGenerator |
28
|
|
|
{ |
29
|
1 |
|
private $whitelist; |
30
|
|
|
|
31
|
1 |
|
public function __construct(Whitelist $whitelist) |
32
|
|
|
{ |
33
|
1 |
|
$this->whitelist = $whitelist; |
34
|
1 |
|
} |
35
|
1 |
|
|
36
|
1 |
|
public function dump(string $prefix): string |
37
|
|
|
{ |
38
|
1 |
|
$classAliasStatements = $this->createClassAliasStatements($prefix); |
39
|
1 |
|
|
40
|
|
|
$statements = implode(PHP_EOL, $classAliasStatements).PHP_EOL.PHP_EOL; |
41
|
|
|
$statements .= implode(PHP_EOL, $this->createFunctionAliasStatements($this->whitelist->getUserGlobalFunctions())); |
42
|
1 |
|
|
43
|
|
|
$dump = <<<PHP |
44
|
|
|
<?php |
45
|
|
|
|
46
|
|
|
// scoper-autoload.php @generated by PhpScoper |
47
|
|
|
|
48
|
|
|
\$loader = require_once __DIR__.'/autoload.php'; |
49
|
|
|
|
50
|
|
|
$statements |
51
|
1 |
|
|
52
|
|
|
return \$loader; |
53
|
|
|
|
54
|
|
|
PHP; |
55
|
|
|
$cleanedDump = $dump; |
56
|
|
|
|
57
|
|
|
do { |
58
|
|
|
$dump = $cleanedDump; |
59
|
|
|
$cleanedDump = str_replace("\n\n\n", "\n\n", $dump); |
60
|
|
|
} while ($cleanedDump !== $dump); |
61
|
|
|
|
62
|
|
|
return $dump; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string[] |
67
|
|
|
*/ |
68
|
|
|
public function createClassAliasStatements(string $prefix): array |
69
|
|
|
{ |
70
|
|
|
$statements = array_map( |
71
|
|
|
function (string $whitelistedElement) use ($prefix): string { |
72
|
|
|
return sprintf( |
73
|
|
|
'class_exists(\'%s\%s\');', |
74
|
|
|
$prefix, |
75
|
|
|
$whitelistedElement |
76
|
|
|
); |
77
|
|
|
}, |
78
|
|
|
$this->whitelist->getClassWhitelistArray() |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
if ([] === $statements) { |
82
|
|
|
return $statements; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
array_unshift( |
86
|
|
|
$statements, |
87
|
|
|
<<<'EOF' |
88
|
|
|
// Aliases for the whitelisted classes. For more information see: |
89
|
|
|
// https://github.com/humbug/php-scoper/blob/master/README.md#classes--constants-whitelisting |
90
|
|
|
EOF |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
return $statements; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string[] |
98
|
|
|
*/ |
99
|
|
|
public function createFunctionAliasStatements(UserGlobalFunctionCollection $userGlobalFunctions): array |
100
|
|
|
{ |
101
|
|
|
$statements = array_map( |
102
|
|
|
function (array $node): string { |
103
|
|
|
/** |
104
|
|
|
* @var FullyQualified |
105
|
|
|
* @var FullyQualified $alias |
106
|
|
|
*/ |
107
|
|
|
[$original, $alias] = $node; |
|
|
|
|
108
|
|
|
|
109
|
|
|
return sprintf( |
110
|
|
|
<<<'PHP' |
111
|
|
|
if (!function_exists('%1$s')) { |
112
|
|
|
function %1$s() { |
113
|
|
|
return \%2$s(func_get_args()); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
PHP |
117
|
|
|
, |
118
|
|
|
$original->toString(), |
119
|
|
|
$alias->toString() |
120
|
|
|
); |
121
|
|
|
}, |
122
|
|
|
iterator_to_array($userGlobalFunctions) |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
if ([] === $statements) { |
126
|
|
|
return $statements; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
array_unshift( |
130
|
|
|
$statements, |
131
|
|
|
<<<'EOF' |
132
|
|
|
// Functions whitelisting. For more information see: |
133
|
|
|
// https://github.com/humbug/php-scoper/blob/master/README.md#global-user-functions |
134
|
|
|
EOF |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
return $statements; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
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.