Skip to content

Commit aae706c

Browse files
committed
feature #1028 Add DotenvConfigurator, for .env.etc files (nicolas-grekas)
This PR was merged into the 2.x branch. Discussion ---------- Add DotenvConfigurator, for .env.etc files Needed for symfony/recipes#1342 Commits ------- 43b9ebf Add DotenvConfigurator, for .env.etc files
2 parents de73a3a + 43b9ebf commit aae706c

File tree

4 files changed

+353
-5
lines changed

4 files changed

+353
-5
lines changed

src/Configurator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function __construct(Composer $composer, IOInterface $io, Options $option
3939
'copy-from-recipe' => Configurator\CopyFromRecipeConfigurator::class,
4040
'copy-from-package' => Configurator\CopyFromPackageConfigurator::class,
4141
'env' => Configurator\EnvConfigurator::class,
42+
'dotenv' => Configurator\DotenvConfigurator::class,
4243
'container' => Configurator\ContainerConfigurator::class,
4344
'makefile' => Configurator\MakefileConfigurator::class,
4445
'composer-scripts' => Configurator\ComposerScriptsConfigurator::class,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Flex\Configurator;
13+
14+
use Symfony\Flex\Lock;
15+
use Symfony\Flex\Recipe;
16+
use Symfony\Flex\Update\RecipeUpdate;
17+
18+
class DotenvConfigurator extends AbstractConfigurator
19+
{
20+
public function configure(Recipe $recipe, $vars, Lock $lock, array $options = [])
21+
{
22+
foreach ($vars as $suffix => $vars) {
23+
$configurator = new EnvConfigurator($this->composer, $this->io, $this->options, $suffix);
24+
$configurator->configure($recipe, $vars, $lock, $options);
25+
}
26+
}
27+
28+
public function unconfigure(Recipe $recipe, $vars, Lock $lock)
29+
{
30+
foreach ($vars as $suffix => $vars) {
31+
$configurator = new EnvConfigurator($this->composer, $this->io, $this->options, $suffix);
32+
$configurator->unconfigure($recipe, $vars, $lock);
33+
}
34+
}
35+
36+
public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array $newConfig): void
37+
{
38+
foreach ($originalConfig as $suffix => $vars) {
39+
$configurator = new EnvConfigurator($this->composer, $this->io, $this->options, $suffix);
40+
$configurator->update($recipeUpdate, $vars, $newConfig[$suffix] ?? []);
41+
}
42+
43+
foreach ($newConfig as $suffix => $vars) {
44+
if (!isset($originalConfig[$suffix])) {
45+
$configurator = new EnvConfigurator($this->composer, $this->io, $this->options, $suffix);
46+
$configurator->update($recipeUpdate, [], $vars);
47+
}
48+
}
49+
}
50+
}

src/Configurator/EnvConfigurator.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
namespace Symfony\Flex\Configurator;
1313

14+
use Composer\Composer;
15+
use Composer\IO\IOInterface;
1416
use Symfony\Flex\Lock;
17+
use Symfony\Flex\Options;
1518
use Symfony\Flex\Recipe;
1619
use Symfony\Flex\Update\RecipeUpdate;
1720

@@ -20,11 +23,24 @@
2023
*/
2124
class EnvConfigurator extends AbstractConfigurator
2225
{
26+
private string $suffix;
27+
28+
public function __construct(Composer $composer, IOInterface $io, Options $options, string $suffix = '')
29+
{
30+
parent::__construct($composer, $io, $options);
31+
$this->suffix = $suffix;
32+
}
33+
2334
public function configure(Recipe $recipe, $vars, Lock $lock, array $options = [])
2435
{
25-
$this->write('Adding environment variable defaults');
36+
$this->write('Adding environment variable defaults'.('' === $this->suffix ? '' : ' ('.$this->suffix.')'));
2637

2738
$this->configureEnvDist($recipe, $vars, $options['force'] ?? false);
39+
40+
if ('' !== $this->suffix) {
41+
return;
42+
}
43+
2844
if (!file_exists($this->options->get('root-dir').'/'.($this->options->get('runtime')['dotenv_path'] ?? '.env').'.test')) {
2945
$this->configurePhpUnit($recipe, $vars, $options['force'] ?? false);
3046
}
@@ -50,8 +66,9 @@ public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array
5066
private function configureEnvDist(Recipe $recipe, $vars, bool $update)
5167
{
5268
$dotenvPath = $this->options->get('runtime')['dotenv_path'] ?? '.env';
69+
$files = '' === $this->suffix ? [$dotenvPath.'.dist', $dotenvPath] : [$dotenvPath.'.'.$this->suffix];
5370

54-
foreach ([$dotenvPath.'.dist', $dotenvPath] as $file) {
71+
foreach ($files as $file) {
5572
$env = $this->options->get('root-dir').'/'.$file;
5673
if (!is_file($env)) {
5774
continue;
@@ -136,8 +153,9 @@ private function configurePhpUnit(Recipe $recipe, $vars, bool $update)
136153
private function unconfigureEnvFiles(Recipe $recipe, $vars)
137154
{
138155
$dotenvPath = $this->options->get('runtime')['dotenv_path'] ?? '.env';
156+
$files = '' === $this->suffix ? [$dotenvPath, $dotenvPath.'.dist'] : [$dotenvPath.'.'.$this->suffix];
139157

140-
foreach ([$dotenvPath, $dotenvPath.'.dist'] as $file) {
158+
foreach ($files as $file) {
141159
$env = $this->options->get('root-dir').'/'.$file;
142160
if (!file_exists($env)) {
143161
continue;
@@ -205,7 +223,7 @@ private function generateRandomBytes($length = 16)
205223
private function getContentsAfterApplyingRecipe(string $rootDir, Recipe $recipe, array $vars): array
206224
{
207225
$dotenvPath = $this->options->get('runtime')['dotenv_path'] ?? '.env';
208-
$files = [$dotenvPath, $dotenvPath.'.dist', 'phpunit.xml.dist', 'phpunit.xml'];
226+
$files = '' === $this->suffix ? [$dotenvPath, $dotenvPath.'.dist', 'phpunit.xml.dist', 'phpunit.xml'] : [$dotenvPath.'.'.$this->suffix];
209227

210228
if (0 === \count($vars)) {
211229
return array_fill_keys($files, null);
@@ -222,7 +240,7 @@ private function getContentsAfterApplyingRecipe(string $rootDir, Recipe $recipe,
222240
true
223241
);
224242

225-
if (!file_exists($rootDir.'/'.$dotenvPath.'.test')) {
243+
if ('' === $this->suffix && !file_exists($rootDir.'/'.$dotenvPath.'.test')) {
226244
$this->configurePhpUnit(
227245
$recipe,
228246
$vars,

0 commit comments

Comments
 (0)