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\Configuration; |
16
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\Patcher\Patcher; |
18
|
|
|
use Humbug\PhpScoper\Whitelist; |
19
|
|
|
use InvalidArgumentException; |
20
|
|
|
use function Safe\preg_match; |
21
|
|
|
use function Safe\sprintf; |
22
|
|
|
|
23
|
|
|
final class Configuration |
24
|
|
|
{ |
25
|
|
|
private const PREFIX_PATTERN = '/^[\p{L}\d_\\\\]+$/u'; |
26
|
|
|
|
27
|
|
|
private ?string $path; |
28
|
|
|
private string $prefix; |
29
|
|
|
private array $filesWithContents; |
30
|
|
|
private array $whitelistedFilesWithContents; |
31
|
|
|
private Patcher $patcher; |
32
|
|
|
private Whitelist $whitelist; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string[] |
36
|
|
|
*/ |
37
|
|
|
private array $internalClasses; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string[] |
41
|
|
|
*/ |
42
|
|
|
private array $internalFunctions; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string[] |
46
|
|
|
*/ |
47
|
|
|
private array $internalConstants; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string|null $path Absolute path to the configuration file loaded. |
51
|
|
|
* @param string $prefix The prefix applied. |
52
|
|
|
* @param array<string, array{string, string}> $filesWithContents Array of tuple with the |
53
|
|
|
* first argument being the file path and the second |
54
|
|
|
* its contents |
55
|
|
|
* @param array<string, array{string, string}> $whitelistedFilesWithContents Array of tuple |
56
|
|
|
* with the first argument being the file path and |
57
|
|
|
* the second its contents |
58
|
|
|
* @param Whitelist $whitelist List of classes that will not be scoped. |
59
|
|
|
* returning a boolean which if `true` means the |
60
|
|
|
* class should be scoped |
61
|
|
|
* (i.e. is ignored) or scoped otherwise. |
62
|
|
|
* @param string[] $internalClasses |
63
|
|
|
* @param string[] $internalFunctions |
64
|
|
|
* @param string[] $internalConstants |
65
|
|
|
*/ |
66
|
|
|
public function __construct( |
67
|
|
|
?string $path, |
68
|
|
|
string $prefix, |
69
|
|
|
array $filesWithContents, |
70
|
|
|
array $whitelistedFilesWithContents, |
71
|
|
|
Patcher $patcher, |
72
|
|
|
Whitelist $whitelist, |
73
|
|
|
array $internalClasses, |
74
|
|
|
array $internalFunctions, |
75
|
|
|
array $internalConstants |
76
|
|
|
) { |
77
|
|
|
self::validatePrefix($prefix); |
78
|
|
|
|
79
|
|
|
$this->path = $path; |
80
|
|
|
$this->prefix = $prefix; |
81
|
|
|
$this->filesWithContents = $filesWithContents; |
82
|
|
|
$this->patcher = $patcher; |
83
|
|
|
$this->whitelist = $whitelist; |
84
|
|
|
$this->whitelistedFilesWithContents = $whitelistedFilesWithContents; |
85
|
|
|
$this->internalClasses = $internalClasses; |
86
|
|
|
$this->internalFunctions = $internalFunctions; |
87
|
|
|
$this->internalConstants = $internalConstants; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getPath(): ?string |
91
|
|
|
{ |
92
|
|
|
return $this->path; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getPrefix(): string |
96
|
|
|
{ |
97
|
|
|
return $this->prefix; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array<string, array{string, string}> |
102
|
|
|
*/ |
103
|
|
|
public function getFilesWithContents(): array |
104
|
|
|
{ |
105
|
|
|
return $this->filesWithContents; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getPatcher(): Patcher |
109
|
|
|
{ |
110
|
|
|
return $this->patcher; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getWhitelist(): Whitelist |
114
|
|
|
{ |
115
|
|
|
return $this->whitelist; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return array<string, array{string, string}> |
120
|
|
|
*/ |
121
|
|
|
public function getWhitelistedFilesWithContents(): array |
122
|
|
|
{ |
123
|
|
|
return $this->whitelistedFilesWithContents; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string[] |
128
|
|
|
*/ |
129
|
|
|
public function getInternalClasses(): array |
130
|
|
|
{ |
131
|
|
|
return $this->internalClasses; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string[] |
136
|
|
|
*/ |
137
|
|
|
public function getInternalFunctions(): array |
138
|
|
|
{ |
139
|
|
|
return $this->internalFunctions; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string[] |
144
|
|
|
*/ |
145
|
|
|
public function getInternalConstants(): array |
146
|
|
|
{ |
147
|
|
|
return $this->internalConstants; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
private static function validatePrefix(string $prefix): void |
151
|
|
|
{ |
152
|
|
|
if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) { |
153
|
|
|
throw new InvalidArgumentException( |
154
|
|
|
sprintf( |
155
|
|
|
'The prefix needs to be composed solely of letters, digits and backslashes (as namespace separators). Got "%s"', |
156
|
|
|
$prefix, |
157
|
|
|
), |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if (preg_match('/\\\{2,}/', $prefix)) { |
162
|
|
|
throw new InvalidArgumentException( |
163
|
|
|
sprintf( |
164
|
|
|
'Invalid namespace separator sequence. Got "%s"', |
165
|
|
|
$prefix, |
166
|
|
|
), |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|