Skip to content

Commit

Permalink
Add DotenvConfigurator, for .env.etc files
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Oct 1, 2024
1 parent de73a3a commit 43b9ebf
Show file tree
Hide file tree
Showing 4 changed files with 353 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function __construct(Composer $composer, IOInterface $io, Options $option
'copy-from-recipe' => Configurator\CopyFromRecipeConfigurator::class,
'copy-from-package' => Configurator\CopyFromPackageConfigurator::class,
'env' => Configurator\EnvConfigurator::class,
'dotenv' => Configurator\DotenvConfigurator::class,
'container' => Configurator\ContainerConfigurator::class,
'makefile' => Configurator\MakefileConfigurator::class,
'composer-scripts' => Configurator\ComposerScriptsConfigurator::class,
Expand Down
50 changes: 50 additions & 0 deletions src/Configurator/DotenvConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Flex\Configurator;

use Symfony\Flex\Lock;
use Symfony\Flex\Recipe;
use Symfony\Flex\Update\RecipeUpdate;

class DotenvConfigurator extends AbstractConfigurator
{
public function configure(Recipe $recipe, $vars, Lock $lock, array $options = [])
{
foreach ($vars as $suffix => $vars) {
$configurator = new EnvConfigurator($this->composer, $this->io, $this->options, $suffix);
$configurator->configure($recipe, $vars, $lock, $options);
}
}

public function unconfigure(Recipe $recipe, $vars, Lock $lock)
{
foreach ($vars as $suffix => $vars) {
$configurator = new EnvConfigurator($this->composer, $this->io, $this->options, $suffix);
$configurator->unconfigure($recipe, $vars, $lock);
}
}

public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array $newConfig): void
{
foreach ($originalConfig as $suffix => $vars) {
$configurator = new EnvConfigurator($this->composer, $this->io, $this->options, $suffix);
$configurator->update($recipeUpdate, $vars, $newConfig[$suffix] ?? []);
}

foreach ($newConfig as $suffix => $vars) {
if (!isset($originalConfig[$suffix])) {
$configurator = new EnvConfigurator($this->composer, $this->io, $this->options, $suffix);
$configurator->update($recipeUpdate, [], $vars);
}
}
}
}
28 changes: 23 additions & 5 deletions src/Configurator/EnvConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace Symfony\Flex\Configurator;

use Composer\Composer;
use Composer\IO\IOInterface;
use Symfony\Flex\Lock;
use Symfony\Flex\Options;
use Symfony\Flex\Recipe;
use Symfony\Flex\Update\RecipeUpdate;

Expand All @@ -20,11 +23,24 @@
*/
class EnvConfigurator extends AbstractConfigurator
{
private string $suffix;

public function __construct(Composer $composer, IOInterface $io, Options $options, string $suffix = '')
{
parent::__construct($composer, $io, $options);
$this->suffix = $suffix;
}

public function configure(Recipe $recipe, $vars, Lock $lock, array $options = [])
{
$this->write('Adding environment variable defaults');
$this->write('Adding environment variable defaults'.('' === $this->suffix ? '' : ' ('.$this->suffix.')'));

$this->configureEnvDist($recipe, $vars, $options['force'] ?? false);

if ('' !== $this->suffix) {
return;
}

if (!file_exists($this->options->get('root-dir').'/'.($this->options->get('runtime')['dotenv_path'] ?? '.env').'.test')) {
$this->configurePhpUnit($recipe, $vars, $options['force'] ?? false);
}
Expand All @@ -50,8 +66,9 @@ public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array
private function configureEnvDist(Recipe $recipe, $vars, bool $update)
{
$dotenvPath = $this->options->get('runtime')['dotenv_path'] ?? '.env';
$files = '' === $this->suffix ? [$dotenvPath.'.dist', $dotenvPath] : [$dotenvPath.'.'.$this->suffix];

foreach ([$dotenvPath.'.dist', $dotenvPath] as $file) {
foreach ($files as $file) {
$env = $this->options->get('root-dir').'/'.$file;
if (!is_file($env)) {
continue;
Expand Down Expand Up @@ -136,8 +153,9 @@ private function configurePhpUnit(Recipe $recipe, $vars, bool $update)
private function unconfigureEnvFiles(Recipe $recipe, $vars)
{
$dotenvPath = $this->options->get('runtime')['dotenv_path'] ?? '.env';
$files = '' === $this->suffix ? [$dotenvPath, $dotenvPath.'.dist'] : [$dotenvPath.'.'.$this->suffix];

foreach ([$dotenvPath, $dotenvPath.'.dist'] as $file) {
foreach ($files as $file) {
$env = $this->options->get('root-dir').'/'.$file;
if (!file_exists($env)) {
continue;
Expand Down Expand Up @@ -205,7 +223,7 @@ private function generateRandomBytes($length = 16)
private function getContentsAfterApplyingRecipe(string $rootDir, Recipe $recipe, array $vars): array
{
$dotenvPath = $this->options->get('runtime')['dotenv_path'] ?? '.env';
$files = [$dotenvPath, $dotenvPath.'.dist', 'phpunit.xml.dist', 'phpunit.xml'];
$files = '' === $this->suffix ? [$dotenvPath, $dotenvPath.'.dist', 'phpunit.xml.dist', 'phpunit.xml'] : [$dotenvPath.'.'.$this->suffix];

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

if (!file_exists($rootDir.'/'.$dotenvPath.'.test')) {
if ('' === $this->suffix && !file_exists($rootDir.'/'.$dotenvPath.'.test')) {
$this->configurePhpUnit(
$recipe,
$vars,
Expand Down
Loading

0 comments on commit 43b9ebf

Please sign in to comment.