|
29 | 29 | use function array_reduce;
|
30 | 30 | use function array_unique;
|
31 | 31 | use function array_unshift;
|
| 32 | +use function bin2hex; |
32 | 33 | use function dirname;
|
33 | 34 | use function file_exists;
|
34 | 35 | use function gettype;
|
|
42 | 43 | use function is_readable;
|
43 | 44 | use function is_string;
|
44 | 45 | use function iterator_to_array;
|
| 46 | +use function random_bytes; |
45 | 47 | use function realpath as native_realpath;
|
46 | 48 | use function Safe\file_get_contents;
|
47 | 49 | use function Safe\preg_match;
|
@@ -73,7 +75,7 @@ final class Configuration
|
73 | 75 | ];
|
74 | 76 |
|
75 | 77 | private ?string $path;
|
76 |
| - private ?string $prefix; |
| 78 | + private string $prefix; |
77 | 79 | private array $filesWithContents;
|
78 | 80 | private array $patchers;
|
79 | 81 | private Whitelist $whitelist;
|
@@ -150,19 +152,22 @@ public static function load(string $path = null, array $paths = []): self
|
150 | 152 | }
|
151 | 153 |
|
152 | 154 | /**
|
153 |
| - * @param string|null $path Absolute path to the configuration file loaded. |
154 |
| - * @param string|null $prefix The prefix applied. |
155 |
| - * @param string[][] $filesWithContents Array of tuple with the first argument being the file path and the second its contents |
156 |
| - * @param callable[] $patchers List of closures which can alter the content of the files being |
157 |
| - * scoped. |
158 |
| - * @param Whitelist $whitelist List of classes that will not be scoped. |
159 |
| - * returning a boolean which if `true` means the class should be scoped |
160 |
| - * (i.e. is ignored) or scoped otherwise. |
161 |
| - * @param string[] $whitelistedFiles List of absolute paths of files to completely ignore |
| 155 | + * @param string|null $path Absolute path to the configuration file loaded. |
| 156 | + * @param string $prefix The prefix applied. |
| 157 | + * @param string[][] $filesWithContents Array of tuple with the first argument being the |
| 158 | + * file path and the second its contents |
| 159 | + * @param callable[] $patchers List of closures which can alter the content of |
| 160 | + * the files being scoped. |
| 161 | + * @param Whitelist $whitelist List of classes that will not be scoped. |
| 162 | + * returning a boolean which if `true` means the |
| 163 | + * class should be scoped |
| 164 | + * (i.e. is ignored) or scoped otherwise. |
| 165 | + * @param string[] $whitelistedFiles List of absolute paths of files to completely |
| 166 | + * ignore |
162 | 167 | */
|
163 | 168 | private function __construct(
|
164 | 169 | ?string $path,
|
165 |
| - ?string $prefix, |
| 170 | + string $prefix, |
166 | 171 | array $filesWithContents,
|
167 | 172 | array $patchers,
|
168 | 173 | Whitelist $whitelist,
|
@@ -215,7 +220,7 @@ public function getPath(): ?string
|
215 | 220 | return $this->path;
|
216 | 221 | }
|
217 | 222 |
|
218 |
| - public function getPrefix(): ?string |
| 223 | + public function getPrefix(): string |
219 | 224 | {
|
220 | 225 | return $this->prefix;
|
221 | 226 | }
|
@@ -266,24 +271,18 @@ private static function validateConfigKey(string $key): void
|
266 | 271 | }
|
267 | 272 | }
|
268 | 273 |
|
269 |
| - /** |
270 |
| - * If the prefix is set to null in the config file/argument then a random prefix is being used. However if set to |
271 |
| - * empty, the configuration will use a null prefix. |
272 |
| - * |
273 |
| - * TL:DR; setting the prefix is a big confusing because it is not properly split in "set prefix" & prefix strategy". |
274 |
| - */ |
275 |
| - private static function retrievePrefix(array $config): ?string |
| 274 | + private static function retrievePrefix(array $config): string |
276 | 275 | {
|
277 | 276 | $prefix = $config[self::PREFIX_KEYWORD] ?? null;
|
278 | 277 |
|
279 | 278 | if (null === $prefix) {
|
280 |
| - return null; |
| 279 | + return self::generateRandomPrefix(); |
281 | 280 | }
|
282 | 281 |
|
283 | 282 | $prefix = trim($prefix);
|
284 | 283 |
|
285 | 284 | if ('' === $prefix) {
|
286 |
| - return null; |
| 285 | + return self::generateRandomPrefix(); |
287 | 286 | }
|
288 | 287 |
|
289 | 288 | if (1 === preg_match('/^[\p{L}\d_]+$/u', $prefix)) {
|
@@ -569,4 +568,9 @@ static function (array $files, SplFileInfo $fileInfo): array {
|
569 | 568 | []
|
570 | 569 | );
|
571 | 570 | }
|
| 571 | + |
| 572 | + private static function generateRandomPrefix(): string |
| 573 | + { |
| 574 | + return '_PhpScoper'.bin2hex(random_bytes(6)); |
| 575 | + } |
572 | 576 | }
|
0 commit comments