Skip to content

Commit 4e436f8

Browse files
authored
Add the prefix to the configuration (#186)
Related to #178.
1 parent 61b862e commit 4e436f8

File tree

3 files changed

+54
-69
lines changed

3 files changed

+54
-69
lines changed

src/Configuration.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,22 @@
2727
*/
2828
class Configuration
2929
{
30+
private const PREFIX = 'prefix';
3031
private const FINDER_KEYWORD = 'finders';
3132
private const PATCHERS_KEYWORD = 'patchers';
3233
private const WHITELIST_KEYWORD = 'whitelist';
3334
private const GLOBAL_NAMESPACE_KEYWORD = 'global_namespace_whitelist';
3435

3536
private const KEYWORDS = [
37+
self::PREFIX,
3638
self::FINDER_KEYWORD,
3739
self::PATCHERS_KEYWORD,
3840
self::WHITELIST_KEYWORD,
3941
self::GLOBAL_NAMESPACE_KEYWORD,
4042
];
4143

4244
private $path;
45+
private $prefix;
4346
private $filesWithContents;
4447
private $patchers;
4548
private $whitelist;
@@ -69,18 +72,21 @@ public static function load(string $path = null, array $paths = []): self
6972

7073
self::validateConfigKeys($config);
7174

75+
$prefix = self::retrievePrefix($config);
76+
7277
$patchers = self::retrievePatchers($config);
7378
$whitelist = self::retrieveWhitelist($config);
7479

7580
$finders = self::retrieveFinders($config);
7681
$filesFromPaths = self::retrieveFilesFromPaths($paths);
7782
$filesWithContents = self::retrieveFilesWithContents(chain($filesFromPaths, ...$finders));
7883

79-
return new self($path, $filesWithContents, $patchers, $whitelist);
84+
return new self($path, $prefix, $filesWithContents, $patchers, $whitelist);
8085
}
8186

8287
/**
8388
* @param string|null $path Absolute path to the configuration file loaded.
89+
* @param string|null $prefix The prefix applied.
8490
* @param [string, string][] $filesWithContents Array of tuple with the first argument being the file path and the second its contents
8591
* @param callable[] $patchers List of closures which can alter the content of the files being
8692
* scoped.
@@ -91,11 +97,13 @@ public static function load(string $path = null, array $paths = []): self
9197
*/
9298
private function __construct(
9399
?string $path,
100+
?string $prefix,
94101
array $filesWithContents,
95102
array $patchers,
96103
array $whitelist
97104
) {
98105
$this->path = $path;
106+
$this->prefix = $prefix;
99107
$this->filesWithContents = $filesWithContents;
100108
$this->patchers = $patchers;
101109
$this->whitelist = $whitelist;
@@ -113,17 +121,36 @@ public function withPaths(array $paths): self
113121

114122
return new self(
115123
$this->path,
124+
$this->prefix,
116125
array_merge($this->filesWithContents, $filesWithContents),
117126
$this->patchers,
118127
$this->whitelist
119128
);
120129
}
121130

131+
public function withPrefix(?string $prefix): self
132+
{
133+
$prefix = self::retrievePrefix([self::PREFIX => $prefix]);
134+
135+
return new self(
136+
$this->path,
137+
$prefix,
138+
$this->filesWithContents,
139+
$this->patchers,
140+
$this->whitelist
141+
);
142+
}
143+
122144
public function getPath(): string
123145
{
124146
return $this->path;
125147
}
126148

149+
public function getPrefix(): ?string
150+
{
151+
return $this->prefix;
152+
}
153+
127154
public function getFilesWithContents(): array
128155
{
129156
return $this->filesWithContents;
@@ -162,6 +189,25 @@ private static function validateConfigKey(string $key): void
162189
}
163190
}
164191

192+
/**
193+
* If the prefix is set to null in the config file/argument then a random prefix is being used. However if set to
194+
* empty, the configuration will use a null prefix.
195+
*
196+
* TL:DR; setting the prefix is a big confusing because it is not properly split in "set prefix" & prefix strategy".
197+
*/
198+
private static function retrievePrefix(array $config): ?string
199+
{
200+
$prefix = array_key_exists(self::PREFIX, $config) ? $config[self::PREFIX] : null;
201+
202+
if (null === $prefix) {
203+
return uniqid('_PhpScoper');
204+
}
205+
206+
$prefix = trim($prefix);
207+
208+
return '' === $prefix ? null : $prefix;
209+
}
210+
165211
private static function retrievePatchers(array $config): array
166212
{
167213
if (false === array_key_exists(self::PATCHERS_KEYWORD, $config)) {

src/Console/Command/AddPrefixCommand.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
138138
);
139139

140140
$logger->outputScopingStart(
141-
$input->getOption(self::PREFIX_OPT),
141+
$config->getPrefix(),
142142
$input->getArgument(self::PATH_ARG)
143143
);
144144

145145
try {
146146
$this->scopeFiles(
147-
$input->getOption(self::PREFIX_OPT),
147+
$config->getPrefix(),
148148
$config->getFilesWithContents(),
149149
$output,
150150
$config->getPatchers(),
@@ -273,25 +273,10 @@ private function validatePrefix(InputInterface $input): void
273273
{
274274
$prefix = $input->getOption(self::PREFIX_OPT);
275275

276-
if (null === $prefix) {
277-
$prefix = uniqid('PhpScoper');
278-
} else {
279-
$prefix = trim($prefix);
280-
}
281-
282-
if (1 === preg_match('/(?<prefix>.*?)\\\\*$/', $prefix, $matches)) {
276+
if (null !== $prefix && 1 === preg_match('/(?<prefix>.*?)\\\\*$/', $prefix, $matches)) {
283277
$prefix = $matches['prefix'];
284278
}
285279

286-
if ('' === $prefix) {
287-
throw new RuntimeException(
288-
sprintf(
289-
'Expected "%s" argument to be a non empty string.',
290-
self::PREFIX_OPT
291-
)
292-
);
293-
}
294-
295280
$input->setOption(self::PREFIX_OPT, $prefix);
296281
}
297282

@@ -385,6 +370,7 @@ private function retrieveConfig(InputInterface $input, OutputInterface $output,
385370
);
386371

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

389375
return $this->retrievePaths($input, $config);
390376
}
@@ -445,8 +431,9 @@ private function retrieveConfig(InputInterface $input, OutputInterface $output,
445431
}
446432

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

449-
return $this->retrievePaths($input, $config);
436+
return $config->withPrefix($input->getOption(self::PREFIX_OPT));
450437
}
451438

452439
private function retrievePaths(InputInterface $input, Configuration $config): Configuration

tests/Console/Command/AddPrefixCommandTest.php

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ public function test_applies_a_random_prefix_when_none_given()
446446
Argument::that(
447447
function (string $prefix): bool {
448448
$this->assertRegExp(
449-
'/^PhpScoper[a-z0-9]{13}$/',
449+
'/^\_PhpScoper[a-z0-9]{13}$/',
450450
$prefix
451451
);
452452

@@ -742,41 +742,6 @@ public function test_relative_output_directory_are_made_absolute()
742742
$this->scoperProphecy->scope(Argument::cetera())->shouldHaveBeenCalledTimes(count($expectedFiles));
743743
}
744744

745-
/**
746-
* @dataProvider provideEmptyPrefixes
747-
*/
748-
public function test_cannot_apply_an_empty_prefix(string $prefix)
749-
{
750-
$input = [
751-
'add-prefix',
752-
'--prefix' => $prefix,
753-
'paths' => [
754-
$root = self::FIXTURE_PATH.'/set002/original',
755-
],
756-
'--no-interaction',
757-
'--no-config' => null,
758-
];
759-
760-
$this->fileSystemProphecy->isAbsolutePath(Argument::cetera())->shouldNotBeCalled();
761-
762-
$this->scoperProphecy->scope(Argument::cetera())->shouldNotBeCalled();
763-
764-
try {
765-
$this->appTester->run($input);
766-
767-
$this->fail('Expected exception to be thrown.');
768-
} catch (RuntimeException $exception) {
769-
$this->assertSame(
770-
'Expected "prefix" argument to be a non empty string.',
771-
$exception->getMessage()
772-
);
773-
}
774-
775-
$this->fileSystemProphecy->isAbsolutePath(Argument::cetera())->shouldNotHaveBeenCalled();
776-
777-
$this->scoperProphecy->scope(Argument::cetera())->shouldNotHaveBeenCalled();
778-
}
779-
780745
public function test_throws_an_error_when_passing_a_non_existent_config()
781746
{
782747
$input = [
@@ -953,19 +918,6 @@ public function test_can_scope_projects_with_invalid_files()
953918
$this->scoperProphecy->scope(Argument::cetera())->shouldHaveBeenCalledTimes(count($expectedFiles));
954919
}
955920

956-
public function provideEmptyPrefixes()
957-
{
958-
yield 'empty' => [''];
959-
960-
yield 'space only' => [' '];
961-
962-
yield 'backslashes' => ['\\'];
963-
964-
yield '1 backslash' => ['\\'];
965-
966-
yield '2 backslashes' => ['\\\\'];
967-
}
968-
969921
private function createAppTester(): ApplicationTester
970922
{
971923
/** @var Filesystem $fileSystem */

0 commit comments

Comments
 (0)