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 $whitelistedFilesWithContents; |
29
|
|
|
private array $patchers; |
30
|
|
|
private Whitelist $whitelist; |
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 array<string, array{string, string}> $whitelistedFilesWithContents Array of tuple |
39
|
|
|
* with the first argument being the file path and the |
40
|
|
|
* second its contents |
41
|
|
|
* @param callable[] $patchers List of closures which can alter the content of |
42
|
|
|
* the files being scoped. |
43
|
|
|
* @param Whitelist $whitelist List of classes that will not be scoped. |
44
|
|
|
* returning a boolean which if `true` means the |
45
|
|
|
* class should be scoped |
46
|
|
|
* (i.e. is ignored) or scoped otherwise. |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
?string $path, |
50
|
|
|
string $prefix, |
51
|
|
|
array $filesWithContents, |
52
|
|
|
array $whitelistedFilesWithContents, |
53
|
|
|
array $patchers, |
54
|
|
|
Whitelist $whitelist |
55
|
|
|
) { |
56
|
|
|
self::validatePrefix($prefix); |
57
|
|
|
|
58
|
|
|
$this->path = $path; |
59
|
|
|
$this->prefix = $prefix; |
60
|
|
|
$this->filesWithContents = $filesWithContents; |
61
|
|
|
$this->patchers = $patchers; |
62
|
|
|
$this->whitelist = $whitelist; |
63
|
|
|
$this->whitelistedFilesWithContents = $whitelistedFilesWithContents; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getPath(): ?string |
67
|
|
|
{ |
68
|
|
|
return $this->path; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getPrefix(): string |
72
|
|
|
{ |
73
|
|
|
return $this->prefix; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array<string, array{string, string}> |
78
|
|
|
*/ |
79
|
3 |
|
public function getFilesWithContents(): array |
80
|
|
|
{ |
81
|
3 |
|
return $this->filesWithContents; |
82
|
1 |
|
} |
83
|
|
|
|
84
|
2 |
|
/** |
85
|
|
|
* @return callable[] |
86
|
|
|
*/ |
87
|
|
|
public function getPatchers(): array |
88
|
|
|
{ |
89
|
|
|
return $this->patchers; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getWhitelist(): Whitelist |
93
|
|
|
{ |
94
|
2 |
|
return $this->whitelist; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array<string, array{string, string}> |
99
|
|
|
*/ |
100
|
|
|
public function getWhitelistedFilesWithContents(): array |
101
|
|
|
{ |
102
|
|
|
return $this->whitelistedFilesWithContents; |
103
|
|
|
} |
104
|
2 |
|
|
105
|
|
|
private static function validatePrefix(string $prefix): void |
106
|
|
|
{ |
107
|
|
|
if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) { |
108
|
|
|
throw new InvalidArgumentException( |
109
|
|
|
sprintf( |
110
|
|
|
'The prefix needs to be composed solely of letters, digits and backslashes (as namespace separators). Got "%s"', |
111
|
|
|
$prefix, |
112
|
|
|
), |
113
|
|
|
); |
114
|
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
|
|
|
|