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; |
16
|
|
|
|
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
use function Safe\preg_match; |
19
|
|
|
use function Safe\sprintf; |
20
|
|
|
|
21
|
|
|
final class Configuration |
22
|
|
|
{ |
23
|
|
|
private const PREFIX_PATTERN = '/^[\p{L}\d_\\\\]+$/u'; |
24
|
|
|
|
25
|
|
|
private ?string $path; |
26
|
|
|
private string $prefix; |
27
|
|
|
private array $filesWithContents; |
28
|
|
|
private array $patchers; |
29
|
|
|
private Whitelist $whitelist; |
30
|
|
|
private array $whitelistedFiles; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string|null $path Absolute path to the configuration file loaded. |
34
|
|
|
* @param string $prefix The prefix applied. |
35
|
|
|
* @param array<string, array{string, string}> $filesWithContents Array of tuple with the |
|
|
|
|
36
|
|
|
* first argument being the file path and the second |
37
|
|
|
* its contents |
38
|
|
|
* @param callable[] $patchers List of closures which can alter the content of |
39
|
|
|
* the files being scoped. |
40
|
|
|
* @param Whitelist $whitelist List of classes that will not be scoped. |
41
|
|
|
* returning a boolean which if `true` means the |
42
|
|
|
* class should be scoped |
43
|
|
|
* (i.e. is ignored) or scoped otherwise. |
44
|
|
|
* @param string[] $whitelistedFiles List of absolute paths of files to completely |
45
|
|
|
* ignore |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
?string $path, |
49
|
|
|
string $prefix, |
50
|
|
|
array $filesWithContents, |
51
|
|
|
array $patchers, |
52
|
|
|
Whitelist $whitelist, |
53
|
|
|
array $whitelistedFiles |
54
|
|
|
) { |
55
|
|
|
self::validatePrefix($prefix); |
56
|
|
|
|
57
|
|
|
$this->path = $path; |
58
|
|
|
$this->prefix = $prefix; |
59
|
|
|
$this->filesWithContents = $filesWithContents; |
60
|
|
|
$this->patchers = $patchers; |
61
|
|
|
$this->whitelist = $whitelist; |
62
|
|
|
$this->whitelistedFiles = $whitelistedFiles; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getPath(): ?string |
66
|
|
|
{ |
67
|
|
|
return $this->path; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getPrefix(): string |
71
|
|
|
{ |
72
|
|
|
return $this->prefix; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array<string, array{string, string}> |
|
|
|
|
77
|
|
|
*/ |
78
|
|
|
public function getFilesWithContents(): array |
79
|
3 |
|
{ |
80
|
|
|
return $this->filesWithContents; |
81
|
3 |
|
} |
82
|
1 |
|
|
83
|
|
|
/** |
84
|
2 |
|
* @return callable[] |
85
|
|
|
*/ |
86
|
|
|
public function getPatchers(): array |
87
|
|
|
{ |
88
|
|
|
return $this->patchers; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getWhitelist(): Whitelist |
92
|
|
|
{ |
93
|
|
|
return $this->whitelist; |
94
|
2 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string[] |
98
|
|
|
*/ |
99
|
|
|
public function getWhitelistedFiles(): array |
100
|
|
|
{ |
101
|
|
|
return $this->whitelistedFiles; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
private static function validatePrefix(string $prefix): void |
105
|
|
|
{ |
106
|
|
|
if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) { |
107
|
|
|
throw new InvalidArgumentException( |
108
|
|
|
sprintf( |
109
|
|
|
'The prefix needs to be composed solely of letters, digits and backslashes (as namespace separators). Got "%s"', |
110
|
|
|
$prefix, |
111
|
|
|
), |
112
|
|
|
); |
113
|
|
|
} |
114
|
2 |
|
echo '/\\\{2,}$/'; |
115
|
|
|
|
116
|
2 |
|
if (preg_match('/\\\{2,}/', $prefix)) { |
117
|
|
|
throw new InvalidArgumentException( |
118
|
|
|
sprintf( |
119
|
|
|
'Invalid namespace separator sequence. Got "%s"', |
120
|
|
|
$prefix, |
121
|
|
|
), |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|