Skip to content

Commit 870dd31

Browse files
committed
Make the prefix configurable
Closes #178
1 parent 8abd8de commit 870dd31

File tree

5 files changed

+48
-10
lines changed

5 files changed

+48
-10
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ potentially very difficult to debug due to dissimilar or unsupported package ver
3434
- [PHAR](#phar)
3535
- [Composer](#composer)
3636
- [Configuration](#configuration)
37+
- [Prefix](#prefix)
3738
- [Finders and paths](#finders-and-paths)
3839
- [Patchers](#patchers)
3940
- [Whitelist][whitelist]
@@ -122,14 +123,20 @@ with a `--config` option.
122123
use Isolated\Symfony\Component\Finder\Finder;
123124

124125
return [
126+
'prefix' => null,
125127
'finders' => [],
126128
'patchers' => [],
127-
'global_namespace_whitelist' => [],
128129
'whitelist' => [],
129130
];
130131
```
131132

132133

134+
### Prefix
135+
136+
The prefix to be used to isolate the code. If `null` or `'''` is given, then a random
137+
prefix will be automatically be generated.
138+
139+
133140
### Finders and paths
134141

135142
By default when running `php-scoper add-prefix`, it will prefix all relevant

scoper.inc.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Isolated\Symfony\Component\Finder\Finder;
1616

1717
return [
18+
'prefix' => 'Foo',
1819
'whitelist' => [
1920
Finder::class,
2021
],

src/Configuration.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@ class Configuration
3131
private const FINDER_KEYWORD = 'finders';
3232
private const PATCHERS_KEYWORD = 'patchers';
3333
private const WHITELIST_KEYWORD = 'whitelist';
34-
private const GLOBAL_NAMESPACE_KEYWORD = 'global_namespace_whitelist';
3534

3635
private const KEYWORDS = [
3736
self::PREFIX,
3837
self::FINDER_KEYWORD,
3938
self::PATCHERS_KEYWORD,
4039
self::WHITELIST_KEYWORD,
41-
self::GLOBAL_NAMESPACE_KEYWORD,
4240
];
4341

4442
private $path;
@@ -200,7 +198,7 @@ private static function retrievePrefix(array $config): ?string
200198
$prefix = array_key_exists(self::PREFIX, $config) ? $config[self::PREFIX] : null;
201199

202200
if (null === $prefix) {
203-
return uniqid('_PhpScoper');
201+
return null;
204202
}
205203

206204
$prefix = trim($prefix);
@@ -345,6 +343,13 @@ private static function retrieveFilesFromPaths(array $paths): iterable
345343
$finder->files()
346344
->in($pathsToSearch)
347345
->append($filesToAppend)
346+
->filter(function (SplFileInfo $fileInfo): ?bool {
347+
if ($fileInfo->isLink()) {
348+
return false;
349+
}
350+
351+
return null;
352+
})
348353
->sortByName()
349354
;
350355

src/Console/Command/AddPrefixCommand.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,14 +363,23 @@ private function validateOutputDir(InputInterface $input, OutputStyle $io): void
363363

364364
private function retrieveConfig(InputInterface $input, OutputInterface $output, OutputStyle $io): Configuration
365365
{
366+
$prefix = $input->getOption(self::PREFIX_OPT);
367+
366368
if ($input->getOption(self::NO_CONFIG_OPT)) {
367369
$io->writeln(
368370
'Loading without configuration file.',
369-
OutputStyle::VERBOSITY_DEBUG
371+
OutputInterface::VERBOSITY_DEBUG
370372
);
371373

372374
$config = Configuration::load();
373-
$config = $config->withPrefix($input->getOption(self::PREFIX_OPT));
375+
376+
if (null !== $prefix) {
377+
$config->withPrefix($prefix);
378+
}
379+
380+
if (null === $config->getPrefix()) {
381+
$config = $config->withPrefix($this->generateRandomPrefix());
382+
}
374383

375384
return $this->retrievePaths($input, $config);
376385
}
@@ -395,7 +404,7 @@ private function retrieveConfig(InputInterface $input, OutputInterface $output,
395404
'Config file "<comment>%s</comment>" not found. Skipping.',
396405
$configFile
397406
),
398-
OutputStyle::VERBOSITY_DEBUG
407+
OutputInterface::VERBOSITY_DEBUG
399408
);
400409

401410
return self::retrieveConfig($input, $output, $io);
@@ -411,7 +420,7 @@ private function retrieveConfig(InputInterface $input, OutputInterface $output,
411420
if (null === $configFile) {
412421
$io->writeln(
413422
'Loading without configuration file.',
414-
OutputStyle::VERBOSITY_DEBUG
423+
OutputInterface::VERBOSITY_DEBUG
415424
);
416425
} elseif (false === file_exists($configFile)) {
417426
throw new RuntimeException(
@@ -426,14 +435,22 @@ private function retrieveConfig(InputInterface $input, OutputInterface $output,
426435
'Using the configuration file "%s".',
427436
$configFile
428437
),
429-
OutputStyle::VERBOSITY_DEBUG
438+
OutputInterface::VERBOSITY_DEBUG
430439
);
431440
}
432441

433442
$config = Configuration::load($configFile);
434443
$config = $this->retrievePaths($input, $config);
435444

436-
return $config->withPrefix($input->getOption(self::PREFIX_OPT));
445+
if (null !== $prefix) {
446+
$config->withPrefix($prefix);
447+
}
448+
449+
if (null === $config->getPrefix()) {
450+
$config = $config->withPrefix($this->generateRandomPrefix());
451+
}
452+
453+
return $config;
437454
}
438455

439456
private function retrievePaths(InputInterface $input, Configuration $config): Configuration
@@ -456,4 +473,9 @@ private function makeAbsolutePath(string $path): string
456473

457474
return $path;
458475
}
476+
477+
private function generateRandomPrefix(): string
478+
{
479+
return uniqid('_PhpScoper', false);
480+
}
459481
}

src/scoper.inc.php.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ declare(strict_types=1);
55
use Isolated\Symfony\Component\Finder\Finder;
66

77
return [
8+
// The prefix configuration. If a non null value will be used, a random prefix will be generated.
9+
'prefix' => null,
10+
811
// By default when running php-scoper add-prefix, it will prefix all relevant code found in the current working
912
// directory. You can however define which files should be scoped by defining a collection of Finders in the
1013
// following configuration key.

0 commit comments

Comments
 (0)