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 InvalidArgumentException; |
19
|
|
|
use function Safe\preg_match; |
20
|
|
|
use function Safe\sprintf; |
21
|
|
|
|
22
|
|
|
final class Configuration |
23
|
|
|
{ |
24
|
|
|
private const PREFIX_PATTERN = '/^[\p{L}\d_\\\\]+$/u'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var non-empty-string|null |
|
|
|
|
28
|
|
|
*/ |
29
|
|
|
private ?string $path; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var non-empty-string |
|
|
|
|
33
|
|
|
*/ |
34
|
|
|
private string $prefix; |
35
|
|
|
|
36
|
|
|
private array $filesWithContents; |
37
|
|
|
private array $excludedFilesWithContents; |
38
|
|
|
private Patcher $patcher; |
39
|
|
|
private SymbolsConfiguration $symbolsConfiguration; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param non-empty-string|null $path Absolute path to the configuration file loaded. |
|
|
|
|
43
|
|
|
* @param non-empty-string $prefix The prefix applied. |
44
|
|
|
* @param array<string, array{string, string}> $filesWithContents Array of tuple with the |
45
|
|
|
* first argument being the file path and the second |
46
|
|
|
* its contents |
47
|
|
|
* @param array<string, array{string, string}> $excludedFilesWithContents Array of tuple |
48
|
|
|
* with the first argument being the file path and |
49
|
|
|
* the second its contents |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
?string $path, |
53
|
|
|
string $prefix, |
54
|
|
|
array $filesWithContents, |
55
|
|
|
array $excludedFilesWithContents, |
56
|
|
|
Patcher $patcher, |
57
|
|
|
SymbolsConfiguration $symbolsConfiguration |
58
|
|
|
) { |
59
|
|
|
self::validatePrefix($prefix); |
60
|
|
|
|
61
|
|
|
$this->path = $path; |
62
|
|
|
$this->prefix = $prefix; |
63
|
|
|
$this->filesWithContents = $filesWithContents; |
64
|
|
|
$this->excludedFilesWithContents = $excludedFilesWithContents; |
65
|
|
|
$this->patcher = $patcher; |
66
|
|
|
$this->symbolsConfiguration = $symbolsConfiguration; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return non-empty-string|null Absolute canonical path |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
public function getPath(): ?string |
73
|
|
|
{ |
74
|
|
|
return $this->path; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param non-empty-string $prefix |
|
|
|
|
79
|
|
|
*/ |
80
|
|
|
public function withPrefix(string $prefix): self |
81
|
|
|
{ |
82
|
|
|
return new self( |
83
|
|
|
$this->path, |
84
|
|
|
$prefix, |
85
|
|
|
$this->filesWithContents, |
86
|
|
|
$this->excludedFilesWithContents, |
87
|
|
|
$this->patcher, |
88
|
|
|
$this->symbolsConfiguration, |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return non-empty-string |
|
|
|
|
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
|
|
|
/** |
109
|
|
|
* @return array<string, array{string, string}> |
110
|
|
|
*/ |
111
|
|
|
public function getExcludedFilesWithContents(): array |
112
|
|
|
{ |
113
|
|
|
return $this->excludedFilesWithContents; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getPatcher(): Patcher |
117
|
|
|
{ |
118
|
|
|
return $this->patcher; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getSymbolsConfiguration(): SymbolsConfiguration |
122
|
|
|
{ |
123
|
|
|
return $this->symbolsConfiguration; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private static function validatePrefix(string $prefix): void |
127
|
|
|
{ |
128
|
|
|
if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) { |
129
|
|
|
throw new InvalidArgumentException( |
130
|
|
|
sprintf( |
131
|
|
|
'The prefix needs to be composed solely of letters, digits and backslashes (as namespace separators). Got "%s"', |
132
|
|
|
$prefix, |
133
|
|
|
), |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (preg_match('/\\\{2,}/', $prefix)) { |
138
|
|
|
throw new InvalidArgumentException( |
139
|
|
|
sprintf( |
140
|
|
|
'Invalid namespace separator sequence. Got "%s"', |
141
|
|
|
$prefix, |
142
|
|
|
), |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|