Skip to content

Commit

Permalink
Make the prefix configurable (#202)
Browse files Browse the repository at this point in the history
Closes #178
  • Loading branch information
theofidry authored May 12, 2018
2 parents 8abd8de + fe77969 commit a095ee2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ potentially very difficult to debug due to dissimilar or unsupported package ver
- [PHAR](#phar)
- [Composer](#composer)
- [Configuration](#configuration)
- [Prefix](#prefix)
- [Finders and paths](#finders-and-paths)
- [Patchers](#patchers)
- [Whitelist][whitelist]
Expand Down Expand Up @@ -122,14 +123,20 @@ with a `--config` option.
use Isolated\Symfony\Component\Finder\Finder;

return [
'prefix' => null,
'finders' => [],
'patchers' => [],
'global_namespace_whitelist' => [],
'whitelist' => [],
];
```


### Prefix

The prefix to be used to isolate the code. If `null` or `'''` is given, then a random
prefix will be automatically be generated.


### Finders and paths

By default when running `php-scoper add-prefix`, it will prefix all relevant
Expand Down
1 change: 1 addition & 0 deletions scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Isolated\Symfony\Component\Finder\Finder;

return [
'prefix' => 'Foo',
'whitelist' => [
Finder::class,
],
Expand Down
11 changes: 8 additions & 3 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ class Configuration
private const FINDER_KEYWORD = 'finders';
private const PATCHERS_KEYWORD = 'patchers';
private const WHITELIST_KEYWORD = 'whitelist';
private const GLOBAL_NAMESPACE_KEYWORD = 'global_namespace_whitelist';

private const KEYWORDS = [
self::PREFIX,
self::FINDER_KEYWORD,
self::PATCHERS_KEYWORD,
self::WHITELIST_KEYWORD,
self::GLOBAL_NAMESPACE_KEYWORD,
];

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

if (null === $prefix) {
return uniqid('_PhpScoper');
return null;
}

$prefix = trim($prefix);
Expand Down Expand Up @@ -345,6 +343,13 @@ private static function retrieveFilesFromPaths(array $paths): iterable
$finder->files()
->in($pathsToSearch)
->append($filesToAppend)
->filter(function (SplFileInfo $fileInfo): ?bool {
if ($fileInfo->isLink()) {
return false;
}

return null;
})
->sortByName()
;

Expand Down
34 changes: 28 additions & 6 deletions src/Console/Command/AddPrefixCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,23 @@ private function validateOutputDir(InputInterface $input, OutputStyle $io): void

private function retrieveConfig(InputInterface $input, OutputInterface $output, OutputStyle $io): Configuration
{
$prefix = $input->getOption(self::PREFIX_OPT);

if ($input->getOption(self::NO_CONFIG_OPT)) {
$io->writeln(
'Loading without configuration file.',
OutputStyle::VERBOSITY_DEBUG
OutputInterface::VERBOSITY_DEBUG
);

$config = Configuration::load();
$config = $config->withPrefix($input->getOption(self::PREFIX_OPT));

if (null !== $prefix) {
$config = $config->withPrefix($prefix);
}

if (null === $config->getPrefix()) {
$config = $config->withPrefix($this->generateRandomPrefix());
}

return $this->retrievePaths($input, $config);
}
Expand All @@ -395,7 +404,7 @@ private function retrieveConfig(InputInterface $input, OutputInterface $output,
'Config file "<comment>%s</comment>" not found. Skipping.',
$configFile
),
OutputStyle::VERBOSITY_DEBUG
OutputInterface::VERBOSITY_DEBUG
);

return self::retrieveConfig($input, $output, $io);
Expand All @@ -411,7 +420,7 @@ private function retrieveConfig(InputInterface $input, OutputInterface $output,
if (null === $configFile) {
$io->writeln(
'Loading without configuration file.',
OutputStyle::VERBOSITY_DEBUG
OutputInterface::VERBOSITY_DEBUG
);
} elseif (false === file_exists($configFile)) {
throw new RuntimeException(
Expand All @@ -426,14 +435,22 @@ private function retrieveConfig(InputInterface $input, OutputInterface $output,
'Using the configuration file "%s".',
$configFile
),
OutputStyle::VERBOSITY_DEBUG
OutputInterface::VERBOSITY_DEBUG
);
}

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

return $config->withPrefix($input->getOption(self::PREFIX_OPT));
if (null !== $prefix) {
$config = $config->withPrefix($prefix);
}

if (null === $config->getPrefix()) {
$config = $config->withPrefix($this->generateRandomPrefix());
}

return $config;
}

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

return $path;
}

private function generateRandomPrefix(): string
{
return uniqid('_PhpScoper', false);
}
}
3 changes: 3 additions & 0 deletions src/scoper.inc.php.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ declare(strict_types=1);
use Isolated\Symfony\Component\Finder\Finder;

return [
// The prefix configuration. If a non null value will be used, a random prefix will be generated.
'prefix' => null,

// By default when running php-scoper add-prefix, it will prefix all relevant code found in the current working
// directory. You can however define which files should be scoped by defining a collection of Finders in the
// following configuration key.
Expand Down

0 comments on commit a095ee2

Please sign in to comment.