Skip to content

Commit f00568d

Browse files
vjiksamdark
andauthored
Add ability to change merge plan filepath (#130)
Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
1 parent a6226bc commit f00568d

18 files changed

Lines changed: 179 additions & 15 deletions

File tree

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ indent_style = space
1010
indent_size = 4
1111
trim_trailing_whitespace = true
1212

13+
[*.php]
14+
ij_php_space_before_short_closure_left_parenthesis = false
15+
ij_php_space_after_type_cast = true
16+
1317
[*.md]
1418
trim_trailing_whitespace = false
1519

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Enh #119: Improve performance of collecting data for `ReverseMerge` and `RecursiveMerge` (@samdark)
66
- Enh #122: Raise minimal PHP version to 8.0 (@vjik, @xepozz)
7+
- Enh #130: Add ability to change merge plan file path (@vjik)
78

89
## 1.1.1 January 05, 2022
910

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,20 @@ For more information about the wildcard syntax, see the [yiisoft/strings](https:
286286

287287
> Please note that in this sublayer keys with the same names are not allowed similar to other layers.
288288
289+
### `merge-plan-file`
290+
291+
This option allows you to override path to merge plan file. It is `.merge-plan.php` by default. To change it, set the value:
292+
293+
```json
294+
"extra": {
295+
"config-plugin-options": {
296+
"merge-plan-file": "custom/path/my-merge-plan.php"
297+
}
298+
}
299+
```
300+
301+
This can be useful when developing. Don't forget to set same path in `Config` constructor when changing this option.
302+
289303
### `build-merge-plan`
290304

291305
The `build-merge-plan` option allows you to disable creation/updating of the `config/.merge-plan.php`.

src/Composer/MergePlanProcess.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Composer\Composer;
88
use Composer\Package\PackageInterface;
9+
use Composer\Util\Filesystem;
910
use Yiisoft\Config\MergePlan;
1011
use Yiisoft\Config\Options;
1112
use Yiisoft\VarDumper\VarDumper;
@@ -144,16 +145,17 @@ private function updateMergePlan(): void
144145
$mergePlan = $this->mergePlan->toArray();
145146
ksort($mergePlan);
146147

147-
$filePath = $this->helper
148-
->getPaths()
149-
->absolute(Options::MERGE_PLAN_FILENAME);
148+
$filePath = $this->helper->getPaths()->absolute(
149+
$this->helper->getMergePlanFile()
150+
);
151+
(new Filesystem())->ensureDirectoryExists(dirname($filePath));
152+
150153
$oldContent = is_file($filePath) ? file_get_contents($filePath) : '';
151154

152155
$content = '<?php'
153156
. "\n\ndeclare(strict_types=1);"
154157
. "\n\n// Do not edit. Content will be replaced."
155-
. "\nreturn " . VarDumper::create($mergePlan)->export(true) . ";\n"
156-
;
158+
. "\nreturn " . VarDumper::create($mergePlan)->export(true) . ";\n";
157159

158160
if ($this->normalizeLineEndings($oldContent) !== $this->normalizeLineEndings($content)) {
159161
file_put_contents($filePath, $content, LOCK_EX);

src/Composer/ProcessHelper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ public function shouldBuildMergePlan(): bool
220220
return $this->rootPackageOptions->buildMergePlan();
221221
}
222222

223+
/**
224+
* @return string The merge plan filepath.
225+
*/
226+
public function getMergePlanFile(): string
227+
{
228+
return $this->rootPackageOptions->mergePlanFile();
229+
}
230+
223231
/**
224232
* Returns the absolute path to the package source directory {@see Options::sourceDirectory()}.
225233
*

src/Config.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,21 @@ final class Config implements ConfigInterface
3333
* @param string|null $environment The environment name.
3434
* @param object[] $modifiers Modifiers that affect merge process.
3535
* @param string $paramsGroup Group name for $params.
36+
* @param string $mergePlanFile The merge plan filepath.
3637
*
3738
* @throws ErrorException If the environment does not exist.
3839
*/
3940
public function __construct(
4041
ConfigPaths $paths,
4142
string $environment = null,
4243
array $modifiers = [],
43-
private string $paramsGroup = 'params'
44+
private string $paramsGroup = 'params',
45+
string $mergePlanFile = Options::DEFAULT_MERGE_PLAN_FILE,
4446
) {
4547
$environment = empty($environment) ? Options::DEFAULT_ENVIRONMENT : $environment;
4648

4749
/** @psalm-suppress UnresolvableInclude, MixedArgument */
48-
$mergePlan = new MergePlan(require $paths->absolute(Options::MERGE_PLAN_FILENAME));
50+
$mergePlan = new MergePlan(require $paths->absolute($mergePlanFile));
4951

5052
if (!$mergePlan->hasEnvironment($environment)) {
5153
$this->throwException(sprintf('The "%s" configuration environment does not exist.', $environment));

src/Options.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
*/
1414
final class Options
1515
{
16-
public const MERGE_PLAN_FILENAME = '.merge-plan.php';
16+
public const DEFAULT_MERGE_PLAN_FILE = '.merge-plan.php';
1717
public const DEFAULT_CONFIG_DIRECTORY = '';
1818
public const DEFAULT_VENDOR_DIRECTORY = 'vendor';
1919
public const DEFAULT_ENVIRONMENT = '/';
2020
public const ROOT_PACKAGE_NAME = '/';
2121
public const VENDOR_OVERRIDE_PACKAGE_NAME = '//';
2222

23+
private string $mergePlanFile = self::DEFAULT_MERGE_PLAN_FILE;
2324
private bool $buildMergePlan = true;
2425
private array $vendorOverrideLayerPackages = [];
2526
private string $sourceDirectory = self::DEFAULT_CONFIG_DIRECTORY;
@@ -32,6 +33,10 @@ public function __construct(array $extra)
3233

3334
$options = $extra['config-plugin-options'];
3435

36+
if (!empty($options['merge-plan-file'])) {
37+
$this->mergePlanFile = (string) $options['merge-plan-file'];
38+
}
39+
3540
if (isset($options['build-merge-plan'])) {
3641
$this->buildMergePlan = (bool) $options['build-merge-plan'];
3742
}
@@ -60,6 +65,11 @@ public static function isVariable(string $file): bool
6065
return str_starts_with($file, '$');
6166
}
6267

68+
public function mergePlanFile(): string
69+
{
70+
return $this->mergePlanFile;
71+
}
72+
6373
public function buildMergePlan(): bool
6474
{
6575
return $this->buildMergePlan;

tests/Composer/MergePlanProcessTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,32 @@ public function testProcess(): void
1818
$this->assertMergePlan();
1919
}
2020

21+
public function dataCustomMergePlanFile(): array
22+
{
23+
return [
24+
['my-merge-plan.php'],
25+
['test/my-merge-plan.php'],
26+
['../my-merge-plan.php'],
27+
];
28+
}
29+
30+
/**
31+
* @dataProvider dataCustomMergePlanFile
32+
*/
33+
public function testCustomMergePlanFile($filepath): void
34+
{
35+
$composer = $this->createComposerMock(
36+
mergePlanFile: $filepath,
37+
);
38+
new MergePlanProcess($composer);
39+
40+
$this->assertFileExists($this->getTempPath($filepath));
41+
}
42+
2143
public function testProcessWithoutMergePlanBuild(): void
2244
{
2345
new MergePlanProcess($this->createComposerMock(['alfa' => ['params' => 'alfa/params.php']], null, false));
24-
$this->assertFileDoesNotExist($this->getTempPath(Options::MERGE_PLAN_FILENAME));
46+
$this->assertFileDoesNotExist($this->getTempPath(Options::DEFAULT_MERGE_PLAN_FILE));
2547
}
2648

2749
public function testProcessWithEnvironment(): void

tests/Composer/TestCase.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use function json_decode;
2929
use function putenv;
3030
use function str_replace;
31-
use function sys_get_temp_dir;
3231
use function trim;
3332

3433
abstract class TestCase extends \PHPUnit\Framework\TestCase
@@ -44,7 +43,7 @@ public function __construct(?string $name = null, array $data = [], $dataName =
4443

4544
$this->filesystem = new Filesystem();
4645
$this->sourceDirectory = dirname(__DIR__) . '/TestAsset/packages';
47-
$this->tempDirectory = sys_get_temp_dir() . '/yiisoft';
46+
$this->tempDirectory = dirname(__DIR__) . '/runtime/composer';
4847
$this->tempConfigsDirectory = "$this->tempDirectory/config";
4948
}
5049

@@ -143,7 +142,7 @@ protected function assertMergePlan(array $environments = []): void
143142
],
144143
],
145144
], $environments),
146-
require $this->getTempPath(Options::MERGE_PLAN_FILENAME),
145+
require $this->getTempPath(Options::DEFAULT_MERGE_PLAN_FILE),
147146
);
148147
}
149148

@@ -182,18 +181,20 @@ protected function createComposerMock(
182181
array $extraEnvironments = [],
183182
array $vendorOverridePackage = null,
184183
bool $buildMergePlan = true,
185-
string $extraConfigFile = null
184+
string $extraConfigFile = null,
185+
?string $mergePlanFile = null,
186186
) {
187187
$rootPath = $this->tempDirectory;
188188
$sourcePath = $this->sourceDirectory;
189189
$targetPath = "$this->tempDirectory/vendor";
190190

191-
$extra = array_merge([
191+
$extra = [
192192
'config-plugin-file' => $extraConfigFile,
193193
'config-plugin-options' => [
194194
'source-directory' => 'config',
195195
'vendor-override-layer' => $vendorOverridePackage ?? 'test/over',
196196
'build-merge-plan' => $buildMergePlan,
197+
'merge-plan-file' => $mergePlanFile,
197198
],
198199
'config-plugin' => [
199200
'empty' => [],
@@ -207,7 +208,8 @@ protected function createComposerMock(
207208
'web.php',
208209
],
209210
],
210-
], ['config-plugin-environments' => $extraEnvironments]);
211+
'config-plugin-environments' => $extraEnvironments,
212+
];
211213

212214
$config = $this->createMock(Config::class);
213215
$config

tests/ConfigTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,26 @@ public function testDuplicateKeysWithRecursiveKeyPathErrorMessage(): void
464464
$config->get('params');
465465
}
466466

467+
public function testCustomMergePlanFile(): void
468+
{
469+
$config = new Config(
470+
new ConfigPaths(__DIR__ . '/TestAsset/configs/custom-merge-plan-file', 'config'),
471+
Options::DEFAULT_ENVIRONMENT,
472+
mergePlanFile: '../merge-plan.php',
473+
);
474+
475+
$this->assertSame(
476+
[
477+
'a-web-key' => 'a-web-value',
478+
'a-web-environment-override-key' => 'a-web-override-value',
479+
'b-web-key' => 'b-web-value',
480+
'b-web-environment-override-key' => 'b-web-override-value',
481+
'root-web-key' => 'root-params-value',
482+
],
483+
$config->get('web')
484+
);
485+
}
486+
467487
public function testConfigWithCustomParams(): void
468488
{
469489
$config = new Config(

0 commit comments

Comments
 (0)