11
11
12
12
namespace Symfony \Flex \Configurator ;
13
13
14
+ use Composer \Composer ;
15
+ use Composer \IO \IOInterface ;
14
16
use Symfony \Flex \Lock ;
17
+ use Symfony \Flex \Options ;
15
18
use Symfony \Flex \Recipe ;
16
19
use Symfony \Flex \Update \RecipeUpdate ;
17
20
20
23
*/
21
24
class EnvConfigurator extends AbstractConfigurator
22
25
{
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
+
23
34
public function configure (Recipe $ recipe , $ vars , Lock $ lock , array $ options = [])
24
35
{
25
- $ this ->write ('Adding environment variable defaults ' );
36
+ $ this ->write ('Adding environment variable defaults ' .( '' === $ this -> suffix ? '' : ' ( ' . $ this -> suffix . ' ) ' ) );
26
37
27
38
$ this ->configureEnvDist ($ recipe , $ vars , $ options ['force ' ] ?? false );
39
+
40
+ if ('' !== $ this ->suffix ) {
41
+ return ;
42
+ }
43
+
28
44
if (!file_exists ($ this ->options ->get ('root-dir ' ).'/ ' .($ this ->options ->get ('runtime ' )['dotenv_path ' ] ?? '.env ' ).'.test ' )) {
29
45
$ this ->configurePhpUnit ($ recipe , $ vars , $ options ['force ' ] ?? false );
30
46
}
@@ -50,8 +66,9 @@ public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array
50
66
private function configureEnvDist (Recipe $ recipe , $ vars , bool $ update )
51
67
{
52
68
$ dotenvPath = $ this ->options ->get ('runtime ' )['dotenv_path ' ] ?? '.env ' ;
69
+ $ files = '' === $ this ->suffix ? [$ dotenvPath .'.dist ' , $ dotenvPath ] : [$ dotenvPath .'. ' .$ this ->suffix ];
53
70
54
- foreach ([ $ dotenvPath . ' .dist ' , $ dotenvPath ] as $ file ) {
71
+ foreach ($ files as $ file ) {
55
72
$ env = $ this ->options ->get ('root-dir ' ).'/ ' .$ file ;
56
73
if (!is_file ($ env )) {
57
74
continue ;
@@ -136,8 +153,9 @@ private function configurePhpUnit(Recipe $recipe, $vars, bool $update)
136
153
private function unconfigureEnvFiles (Recipe $ recipe , $ vars )
137
154
{
138
155
$ dotenvPath = $ this ->options ->get ('runtime ' )['dotenv_path ' ] ?? '.env ' ;
156
+ $ files = '' === $ this ->suffix ? [$ dotenvPath , $ dotenvPath .'.dist ' ] : [$ dotenvPath .'. ' .$ this ->suffix ];
139
157
140
- foreach ([ $ dotenvPath , $ dotenvPath . ' .dist ' ] as $ file ) {
158
+ foreach ($ files as $ file ) {
141
159
$ env = $ this ->options ->get ('root-dir ' ).'/ ' .$ file ;
142
160
if (!file_exists ($ env )) {
143
161
continue ;
@@ -205,7 +223,7 @@ private function generateRandomBytes($length = 16)
205
223
private function getContentsAfterApplyingRecipe (string $ rootDir , Recipe $ recipe , array $ vars ): array
206
224
{
207
225
$ 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 ];
209
227
210
228
if (0 === \count ($ vars )) {
211
229
return array_fill_keys ($ files , null );
@@ -222,7 +240,7 @@ private function getContentsAfterApplyingRecipe(string $rootDir, Recipe $recipe,
222
240
true
223
241
);
224
242
225
- if (!file_exists ($ rootDir .'/ ' .$ dotenvPath .'.test ' )) {
243
+ if ('' === $ this -> suffix && !file_exists ($ rootDir .'/ ' .$ dotenvPath .'.test ' )) {
226
244
$ this ->configurePhpUnit (
227
245
$ recipe ,
228
246
$ vars ,
0 commit comments