Skip to content

Commit e76bd30

Browse files
theofidryKonamiman
andauthored
Add output dir configuration option (#632)
Co-authored-by: Nestor Soriano <nestor.soriano@automattic.com>
1 parent 68bee4a commit e76bd30

File tree

13 files changed

+3011
-13
lines changed

13 files changed

+3011
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ potentially very difficult to debug due to dissimilar or unsupported package ver
3535
- [Usage](#usage)
3636
- [Configuration](docs/configuration.md#configuration)
3737
- [Prefix](docs/configuration.md#prefix)
38+
- [Output directory](docs/configuration.md#output-directory)
3839
- [Finders and paths](docs/configuration.md#finders-and-paths)
3940
- [Patchers](docs/configuration.md#patchers)
4041
- [Excluded files](docs/configuration.md#excluded-files)

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
"psr-4": {
4646
"Humbug\\PhpScoper\\": "src/"
4747
},
48+
"classmap": [
49+
"vendor-hotfix/"
50+
],
4851
"files": [
4952
"src/functions.php"
5053
]

docs/configuration.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Configuration
22

33
- [Prefix](#prefix)
4+
- [Output directory](#output-directory)
45
- [Finders and paths](#finders-and-paths)
56
- [Patchers](#patchers)
67
- [Excluded files](#excluded-files)
@@ -25,6 +26,7 @@ use Isolated\Symfony\Component\Finder\Finder;
2526

2627
return [
2728
'prefix' => null, // string|null
29+
'output-dir' => null, // string|null
2830
'finders' => [], // list<Finder>
2931
'patchers' => [], // list<callable(string $filePath, string $prefix, string $contents): string>
3032

@@ -52,6 +54,15 @@ The prefix to be used to isolate the code. If `null` or `''` (empty string) is g
5254
then a random prefix will be automatically generated.
5355

5456

57+
### Output directory
58+
59+
The base output directory where the prefixed files will be generated. If `null`
60+
is given, `build` is used.
61+
62+
This setting will be overridden by the command line option of the same name if
63+
present.
64+
65+
5566
### Finders and paths
5667

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

src/Configuration/Configuration.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ final class Configuration
2929
private readonly string $prefix;
3030

3131
/**
32-
* @param non-empty-string|null $path Absolute path to the configuration file loaded.
33-
* @param non-empty-string $prefix The prefix applied.
32+
* @param non-empty-string|null $path Absolute canonical path to the configuration file loaded.
33+
* @param non-empty-string|null $outputDir Absolute canonical path to the output directory.
34+
* @param non-empty-string $prefix The prefix applied.
3435
* @param array<string, array{string, string}> $filesWithContents Array of tuple with the
3536
* first argument being the file path and the second
3637
* its contents
@@ -40,13 +41,15 @@ final class Configuration
4041
*/
4142
public function __construct(
4243
private ?string $path,
44+
private ?string $outputDir,
4345
string $prefix,
4446
private array $filesWithContents,
4547
private array $excludedFilesWithContents,
4648
private Patcher $patcher,
4749
private SymbolsConfiguration $symbolsConfiguration
4850
) {
4951
self::validatePrefix($prefix);
52+
5053
$this->prefix = $prefix;
5154
}
5255

@@ -58,13 +61,22 @@ public function getPath(): ?string
5861
return $this->path;
5962
}
6063

64+
/**
65+
* @return non-empty-string|null Absolute canonical path
66+
*/
67+
public function getOutputDir(): ?string
68+
{
69+
return $this->outputDir;
70+
}
71+
6172
/**
6273
* @param non-empty-string $prefix
6374
*/
6475
public function withPrefix(string $prefix): self
6576
{
6677
return new self(
6778
$this->path,
79+
$this->outputDir,
6880
$prefix,
6981
$this->filesWithContents,
7082
$this->excludedFilesWithContents,

src/Configuration/ConfigurationFactory.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function create(?string $path = null, array $paths = []): Configuration
7777
self::validateConfigKeys($config);
7878

7979
$prefix = self::retrievePrefix($config);
80+
$outputDir = self::retrieveOutputDir($config);
8081

8182
$excludedFiles = null === $path
8283
? []
@@ -99,6 +100,7 @@ public function create(?string $path = null, array $paths = []): Configuration
99100

100101
return new Configuration(
101102
$path,
103+
$outputDir,
102104
$prefix,
103105
$filesWithContents,
104106
self::retrieveFilesWithContents($excludedFiles),
@@ -122,6 +124,7 @@ public function createWithPaths(Configuration $config, array $paths): Configurat
122124

123125
return new Configuration(
124126
$config->getPath(),
127+
$config->getOutputDir(),
125128
$config->getPrefix(),
126129
[
127130
...$config->getFilesWithContents(),
@@ -139,6 +142,7 @@ public function createWithPrefix(Configuration $config, string $prefix): Configu
139142

140143
return new Configuration(
141144
$config->getPath(),
145+
$config->getOutputDir(),
142146
$prefix,
143147
$config->getFilesWithContents(),
144148
$config->getExcludedFilesWithContents(),
@@ -226,6 +230,16 @@ private static function retrievePrefix(array $config): string
226230
return '' === $prefix ? self::generateRandomPrefix() : $prefix;
227231
}
228232

233+
/**
234+
* @return non-empty-string|null
235+
*/
236+
private static function retrieveOutputDir(array $config): ?string
237+
{
238+
$outputDir = trim((string) ($config[ConfigurationKeys::OUTPUT_DIR_KEYWORD] ?? ''));
239+
240+
return '' === $outputDir ? null : $outputDir;
241+
}
242+
229243
/**
230244
* @return array<(callable(string,string,string): string)|Patcher>
231245
*/

src/Configuration/ConfigurationKeys.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
final class ConfigurationKeys
1919
{
2020
public const PREFIX_KEYWORD = 'prefix';
21+
public const OUTPUT_DIR_KEYWORD = 'output-dir';
2122
public const EXCLUDED_FILES_KEYWORD = 'exclude-files';
2223
public const FINDER_KEYWORD = 'finders';
2324
public const PATCHERS_KEYWORD = 'patchers';
@@ -38,6 +39,7 @@ final class ConfigurationKeys
3839

3940
public const KEYWORDS = [
4041
self::PREFIX_KEYWORD,
42+
self::OUTPUT_DIR_KEYWORD,
4143
self::EXCLUDED_FILES_KEYWORD,
4244
self::FINDER_KEYWORD,
4345
self::PATCHERS_KEYWORD,

src/Console/Command/AddPrefixCommand.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ final class AddPrefixCommand implements Command, CommandAware
5353
private const STOP_ON_FAILURE_OPT = 'stop-on-failure';
5454
private const CONFIG_FILE_OPT = 'config';
5555
private const NO_CONFIG_OPT = 'no-config';
56+
57+
private const DEFAULT_OUTPUT_DIR = 'build';
58+
5659
private bool $init = false;
5760

5861
public function __construct(
@@ -90,7 +93,6 @@ public function getConfiguration(): CommandConfiguration
9093
'o',
9194
InputOption::VALUE_REQUIRED,
9295
'The output directory in which the prefixed code will be dumped.',
93-
'build',
9496
),
9597
new InputOption(
9698
self::FORCE_OPT,
@@ -134,12 +136,14 @@ public function execute(IO $io): int
134136
$cwd = getcwd();
135137

136138
$paths = $this->getPathArguments($io, $cwd);
137-
$outputDir = $this->getOutputDir($io, $cwd);
139+
$config = $this->retrieveConfig($io, $paths, $cwd);
138140

141+
$outputDir = $this->canonicalizePath(
142+
$this->getOutputDir($io, $config),
143+
$cwd,
144+
);
139145
$this->checkOutputDir($io, $outputDir);
140146

141-
$config = $this->retrieveConfig($io, $paths, $cwd);
142-
143147
$this->getScoper()->scope(
144148
$io,
145149
$config,
@@ -154,12 +158,15 @@ public function execute(IO $io): int
154158
/**
155159
* @return non-empty-string
156160
*/
157-
private function getOutputDir(IO $io, string $cwd): string
161+
private function getOutputDir(IO $io, Configuration $configuration): string
158162
{
159-
return $this->canonicalizePath(
160-
$io->getOption(self::OUTPUT_DIR_OPT)->asString(),
161-
$cwd,
162-
);
163+
$commandOutputDir = $io->getOption(self::OUTPUT_DIR_OPT)->asString();
164+
165+
if ('' !== $commandOutputDir) {
166+
return $commandOutputDir;
167+
}
168+
169+
return $configuration->getOutputDir() ?? self::DEFAULT_OUTPUT_DIR;
163170
}
164171

165172
private function checkOutputDir(IO $io, string $outputDir): void

src/scoper.inc.php.tpl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ use Isolated\Symfony\Component\Finder\Finder;
1212
// the PHP-Scoper analysis.
1313

1414
return [
15-
// The prefix configuration. If a non null value is be used, a random prefix
15+
// The prefix configuration. If a non-null value is used, a random prefix
1616
// will be generated instead.
1717
//
1818
// For more see: https://github.com/humbug/php-scoper/blob/master/docs/configuration.md#prefix
1919
'prefix' => null,
2020

21+
// The base output directory for the prefixed files.
22+
// This will be overridden by the 'output-dir' command line option if present.
23+
'output-dir' => null,
24+
2125
// By default when running php-scoper add-prefix, it will prefix all relevant code found in the current working
2226
// directory. You can however define which files should be scoped by defining a collection of Finders in the
2327
// following configuration key.

tests/Configuration/ConfigurationFactoryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public function test_it_can_create_a_complete_configuration(): void
9494
9595
return [
9696
'prefix' => 'MyPrefix',
97+
'output-dir' => 'dist',
9798
'exclude-files' => ['file1', 'file2'],
9899
'patchers' => [],
99100
'finders' => [],
@@ -127,6 +128,7 @@ public function test_it_can_create_a_complete_configuration(): void
127128

128129
self::assertSame($this->tmp.DIRECTORY_SEPARATOR.'scoper.inc.php', $configuration->getPath());
129130
self::assertSame('MyPrefix', $configuration->getPrefix());
131+
self::assertSame('dist', $configuration->getOutputDir());
130132
self::assertSame([], $configuration->getFilesWithContents());
131133
self::assertSame(
132134
[

tests/Configuration/ConfigurationTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function test_it_validates_the_prefix(
4040
$this->expectExceptionMessage($expectedExceptionMessage);
4141

4242
new Configuration(
43+
null,
4344
null,
4445
$prefix,
4546
[],
@@ -53,6 +54,7 @@ public function test_it_can_create_a_new_instance_with_a_different_prefix(): voi
5354
{
5455
$values = [
5556
'/path/to/config',
57+
'/path/to/outputDir',
5658
'initialPrefix',
5759
['/path/to/fileA' => ['/path/to/fileA', 'fileAContent']],
5860
['/path/to/fileB' => ['/path/to/fileB', 'fileBContent']],
@@ -68,7 +70,7 @@ public function test_it_can_create_a_new_instance_with_a_different_prefix(): voi
6870
$newConfig = $config->withPrefix('newPrefix');
6971

7072
$expectedNewConfigValues = $values;
71-
$expectedNewConfigValues[1] = 'newPrefix';
73+
$expectedNewConfigValues[2] = 'newPrefix';
7274

7375
self::assertStateIs($config, ...$values);
7476
self::assertStateIs($newConfig, ...$expectedNewConfigValues);
@@ -90,13 +92,15 @@ public static function prefixProvider(): iterable
9092
private static function assertStateIs(
9193
Configuration $configuration,
9294
?string $expectedPath,
95+
?string $expectedOutputDir,
9396
string $expectedPrefix,
9497
array $expectedFilesWithContents,
9598
array $expectedExcludedFilesWithContents,
9699
Patcher $expectedPatcher,
97100
SymbolsConfiguration $expectedSymbolsConfiguration
98101
): void {
99102
self::assertSame($expectedPath, $configuration->getPath());
103+
self::assertSame($expectedOutputDir, $configuration->getOutputDir());
100104
self::assertSame($expectedPrefix, $configuration->getPrefix());
101105
self::assertEqualsCanonicalizing(
102106
$expectedFilesWithContents,

tests/Scoper/ScoperFactoryTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function test_it_can_create_a_scoper(): void
4545

4646
$factory->createScoper(
4747
new Configuration(
48+
null,
4849
null,
4950
'_Humbug',
5051
[],

0 commit comments

Comments
 (0)