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\Console; |
16
|
|
|
|
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
|
19
|
|
|
final class Configuration |
20
|
|
|
{ |
21
|
|
|
private $path; |
22
|
|
|
private $patchers; |
23
|
|
|
private $globalNamespaceWhitelisters; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string|null $path Absolute path to the configuration file loaded |
27
|
|
|
* @param callable[] $patchers List of closures which can alter the content of the files being |
28
|
|
|
* scoped |
29
|
|
|
* @param callable[]|string[] $globalNamespace List of class names from the global namespace that should be scoped |
30
|
|
|
* or closures filtering if the class should be scoped or not |
31
|
|
|
*/ |
32
|
|
|
private function __construct(string $path = null, array $patchers, array $globalNamespace) |
33
|
|
|
{ |
34
|
|
|
$this->path = $path; |
35
|
|
|
$this->patchers = $patchers; |
36
|
|
|
$this->globalNamespaceWhitelisters = $globalNamespace; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string|null $path Absolute path to the configuration file. |
41
|
|
|
* |
42
|
|
|
* @return self |
43
|
|
|
*/ |
44
|
|
|
public static function load(string $path = null): self |
45
|
|
|
{ |
46
|
|
|
if (null === $path) { |
47
|
|
|
return new self(null, [], []); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$config = include $path; |
51
|
|
|
|
52
|
|
|
if (false === is_array($config)) { |
53
|
|
|
throw new InvalidArgumentException( |
54
|
|
|
sprintf( |
55
|
|
|
'Expected configuration to be an array, found "%s" instead.', |
56
|
|
|
gettype($config) |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$patchers = self::retrievePatchers($config); |
62
|
|
|
$globalNamespace = self::retrieveGlobalNamespaceWhitelisters($config); |
63
|
|
|
|
64
|
|
|
return new self($path, $patchers, $globalNamespace); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getPath(): string |
68
|
|
|
{ |
69
|
|
|
return $this->path; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return callable[] |
74
|
|
|
*/ |
75
|
|
|
public function getPatchers() |
76
|
|
|
{ |
77
|
|
|
return $this->patchers; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return callable[]|string[] |
82
|
|
|
*/ |
83
|
|
|
public function getGlobalNamespaceWhitelisters() |
84
|
|
|
{ |
85
|
|
|
return $this->globalNamespaceWhitelisters; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private static function retrievePatchers(array $config): array |
89
|
|
|
{ |
90
|
|
|
if (false === array_key_exists('patchers', $config)) { |
91
|
|
|
return []; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$patchers = $config['patchers']; |
95
|
|
|
|
96
|
|
|
if (false === is_array($patchers)) { |
97
|
|
|
throw new InvalidArgumentException( |
98
|
|
|
sprintf( |
99
|
|
|
'Expected patchers to be an array of callables, found "%s" instead.', |
100
|
|
|
gettype($patchers) |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
foreach ($patchers as $index => $patcher) { |
106
|
|
|
if (false === is_callable($patcher)) { |
107
|
|
|
throw new InvalidArgumentException( |
108
|
|
|
sprintf( |
109
|
|
|
'Expected patchers to be an array of callables, the "%d" element is not.', |
110
|
|
|
$index |
111
|
|
|
) |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $patchers; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private static function retrieveGlobalNamespaceWhitelisters(array $config): array |
120
|
|
|
{ |
121
|
|
|
if (false === array_key_exists('global_namespace_whitelist', $config)) { |
122
|
|
|
return []; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$globalNamespace = $config['global_namespace_whitelist']; |
126
|
|
|
|
127
|
|
|
if (false === is_array($globalNamespace)) { |
128
|
|
|
throw new InvalidArgumentException( |
129
|
|
|
sprintf( |
130
|
|
|
'Expected "global_namespace" to be an array, found "%s" instead.', |
131
|
|
|
gettype($globalNamespace) |
132
|
|
|
) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
foreach ($globalNamespace as $index => $className) { |
137
|
|
|
if (false === is_string($className) && false === is_callable($className)) { |
138
|
|
|
throw new InvalidArgumentException( |
139
|
|
|
sprintf( |
140
|
|
|
'Expected "global_namespace" to be an array of callables or strings, the "%d" element ' |
141
|
|
|
.'is not.', |
142
|
|
|
$index |
143
|
|
|
) |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $globalNamespace; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|