|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Humbug\PhpScoper; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use function array_key_exists; |
|
9
|
|
|
use function array_values; |
|
10
|
|
|
use function gettype; |
|
11
|
|
|
use function is_array; |
|
12
|
|
|
use function is_bool; |
|
13
|
|
|
use function is_string; |
|
14
|
|
|
use function Safe\sprintf; |
|
15
|
|
|
|
|
16
|
|
|
final class ConfigurationWhitelistFactory |
|
17
|
|
|
{ |
|
18
|
|
|
public function createWhitelist(array $config): Whitelist |
|
19
|
|
|
{ |
|
20
|
|
|
$exposedElements = self::retrieveExposedElements($config); |
|
21
|
|
|
|
|
22
|
|
|
$exposeGlobalConstants = self::retrieveExposeGlobalSymbol( |
|
23
|
|
|
$config, |
|
24
|
|
|
ConfigurationKeys::EXPOSE_GLOBAL_CONSTANTS_KEYWORD, |
|
25
|
|
|
); |
|
26
|
|
|
$exposeGlobalClasses = self::retrieveExposeGlobalSymbol( |
|
27
|
|
|
$config, |
|
28
|
|
|
ConfigurationKeys::EXPOSE_GLOBAL_CLASSES_KEYWORD, |
|
29
|
|
|
); |
|
30
|
|
|
$exposeGlobalFunctions = self::retrieveExposeGlobalSymbol( |
|
31
|
|
|
$config, |
|
32
|
|
|
ConfigurationKeys::EXPOSE_GLOBAL_FUNCTIONS_KEYWORD, |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
return Whitelist::create( |
|
36
|
|
|
$exposeGlobalConstants, |
|
37
|
|
|
$exposeGlobalClasses, |
|
38
|
|
|
$exposeGlobalFunctions, |
|
39
|
|
|
...$exposedElements, |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* return list<string> |
|
45
|
|
|
*/ |
|
46
|
|
|
private static function retrieveExposedElements(array $config): array |
|
47
|
|
|
{ |
|
48
|
|
|
$key = ConfigurationKeys::WHITELIST_KEYWORD; |
|
49
|
|
|
|
|
50
|
|
|
if (!array_key_exists($key, $config)) { |
|
51
|
|
|
return []; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$whitelist = $config[$key]; |
|
55
|
|
|
|
|
56
|
|
|
if (!is_array($whitelist)) { |
|
57
|
|
|
throw new InvalidArgumentException( |
|
58
|
|
|
sprintf( |
|
59
|
|
|
'Expected "%s" to be an array of strings, found "%s" instead.', |
|
60
|
|
|
$key, |
|
61
|
|
|
gettype($whitelist), |
|
62
|
|
|
), |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
foreach ($whitelist as $index => $className) { |
|
67
|
|
|
if (is_string($className)) { |
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
throw new InvalidArgumentException( |
|
72
|
|
|
sprintf( |
|
73
|
|
|
'Expected whitelist to be an array of string, the "%d" element is not.', |
|
74
|
|
|
$index, |
|
75
|
|
|
), |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return array_values($whitelist); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private static function retrieveExposeGlobalSymbol(array $config, string $key): bool |
|
83
|
|
|
{ |
|
84
|
|
|
if (!array_key_exists($key, $config)) { |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$value = $config[$key]; |
|
89
|
|
|
|
|
90
|
|
|
if (!is_bool($value)) { |
|
91
|
|
|
throw new InvalidArgumentException( |
|
92
|
|
|
sprintf( |
|
93
|
|
|
'Expected %s to be a boolean, found "%s" instead.', |
|
94
|
|
|
$key, |
|
95
|
|
|
gettype($value), |
|
96
|
|
|
), |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $value; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|