Skip to content

Commit 7579707

Browse files
committed
[feature/disableWithNoDev] feat: allow to disable a file in production mode
1 parent d7ce7f0 commit 7579707

9 files changed

+69
-2
lines changed

Processor.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ public function __construct(IOInterface $io)
1616
$this->io = $io;
1717
}
1818

19-
public function processFile(array $config)
19+
public function processFile(array $config, $devMode = true)
2020
{
2121
$config = $this->processConfig($config);
2222

2323
$realFile = $config['file'];
2424
$parameterKey = $config['parameter-key'];
2525

26+
if ($devMode !== true && !empty($config['dev-only']) && true === $config['dev-only']) {
27+
$this->io->write(sprintf('<info>Skipping the "%s" file</info>', $realFile));
28+
return;
29+
}
30+
2631
$exists = is_file($realFile);
2732

2833
$yamlParser = new Parser();

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ If the old parameter is no longer present (maybe because it has been renamed and
149149
removed already), no parameters are overwritten. You don't need to remove obsolete
150150
parameters from the rename map once they have been renamed.
151151

152+
### Skipping file in production mode
153+
154+
You can enable the parameter handler only in development mode:
155+
156+
```json
157+
{
158+
"extra": {
159+
"incenteev-parameters": {
160+
"dev-only": true
161+
}
162+
}
163+
}
164+
165+
```
166+
167+
Doing that, calling composer with `--no-dev` flag will disable the parameter
168+
handler.
169+
152170
### Managing multiple ignored files
153171

154172
The parameter handler can manage multiple ignored files. To use this feature,

ScriptHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static function buildParameters(Event $event)
3131
throw new \InvalidArgumentException('The extra.incenteev-parameters setting must be an array of configuration objects.');
3232
}
3333

34-
$processor->processFile($config);
34+
$processor->processFile($config, $event->isDevMode());
3535
}
3636
}
3737
}

Tests/ProcessorTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,34 @@ public function provideParameterHandlingTestCases()
171171

172172
return $tests;
173173
}
174+
175+
public function testNoDev()
176+
{
177+
$dataDir = __DIR__.'/fixtures/dev_only';
178+
179+
$testCase = array_replace_recursive(
180+
array(
181+
'title' => 'unknown test',
182+
'config' => array(
183+
'file' => 'parameters.yml',
184+
),
185+
'dist-file' => 'parameters.yml.dist',
186+
'environment' => array(),
187+
'interactive' => false,
188+
),
189+
(array) Yaml::parse(file_get_contents($dataDir.'/setup.yml'))
190+
);
191+
192+
$workingDir = sys_get_temp_dir() . '/incenteev_parameter_handler';
193+
$this->initializeTestCase($testCase, $dataDir, $workingDir);
194+
195+
$message = sprintf('<info>Skipping the "%s" file</info>', $testCase['config']['file']);
196+
$this->io->write($message)->shouldBeCalled();
197+
198+
$this->setInteractionExpectations($testCase);
199+
200+
$this->processor->processFile($testCase['config'], false);
201+
202+
$this->assertFileEquals($dataDir.'/expected.yml', $workingDir.'/'.$testCase['config']['file'], $testCase['title']);
203+
}
174204
}

Tests/ScriptHandlerTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ protected function setUp()
2323
$composer->getPackage()->willReturn($this->package);
2424
$this->event->getComposer()->willReturn($composer);
2525
$this->event->getIO()->willReturn($this->io);
26+
$this->event->isDevMode()->willReturn(true);
2627
}
2728

2829
/**

Tests/fixtures/dev_only/dist.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
parameters:
2+
foo: bar
3+
foo2: bar2

Tests/fixtures/dev_only/existing.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# This file is auto-generated during the composer install
2+
parameters:
3+
foo: bar

Tests/fixtures/dev_only/expected.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# This file is auto-generated during the composer install
2+
parameters:
3+
foo: bar

Tests/fixtures/dev_only/setup.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
title: Skip file in production mode
2+
3+
config:
4+
dev-only: true

0 commit comments

Comments
 (0)