5
5
use Narrowspark \Automatic \Common \Configurator \AbstractConfigurator ;
6
6
use Narrowspark \Automatic \Common \Contract \Configurator as ConfiguratorContract ;
7
7
use Narrowspark \Automatic \Common \Contract \Package as PackageContract ;
8
+ use Narrowspark \Automatic \Configurator \Traits \AppendToFileTrait ;
8
9
9
10
final class EnvConfigurator extends AbstractConfigurator
10
11
{
12
+ use AppendToFileTrait;
13
+
11
14
/**
12
15
* {@inheritdoc}
13
16
*/
@@ -32,8 +35,8 @@ public function configure(PackageContract $package): void
32
35
$ data = '' ;
33
36
34
37
foreach ((array ) $ package ->getConfig (ConfiguratorContract::TYPE , self ::getName ()) as $ key => $ value ) {
35
- if ($ key[ 0 ] === ' # ' && \is_numeric (\mb_substr ($ key , 1 ))) {
36
- $ data .= '# ' . $ value . "\n" ;
38
+ if (\mb_strpos ( $ key, ' # ' ) === 0 && \is_numeric (\mb_substr ($ key , 1 ))) {
39
+ $ data .= '# ' . $ value . \ PHP_EOL ;
37
40
38
41
continue ;
39
42
}
@@ -48,7 +51,7 @@ public function configure(PackageContract $package): void
48
51
$ value = '" ' . \str_replace (['\\' , '" ' , "\t" , "\n" ], ['\\\\' , '\\" ' , '\t ' , '\n ' ], $ value ) . '" ' ;
49
52
}
50
53
51
- $ data .= $ key . '= ' . $ value . "\n" ;
54
+ $ data .= $ key . '= ' . $ value . \ PHP_EOL ;
52
55
}
53
56
54
57
if (! \file_exists ($ this ->path ->getWorkingDir () . \DIRECTORY_SEPARATOR . '.env ' )) {
@@ -57,8 +60,8 @@ public function configure(PackageContract $package): void
57
60
58
61
$ data = $ this ->markData ($ package ->getPrettyName (), $ data );
59
62
60
- \file_put_contents ($ distenv , $ data, \ FILE_APPEND );
61
- \file_put_contents ($ this ->path ->getWorkingDir () . \DIRECTORY_SEPARATOR . '.env ' , $ data, \ FILE_APPEND );
63
+ $ this -> appendToFile ($ distenv , $ data );
64
+ $ this -> appendToFile ($ this ->path ->getWorkingDir () . \DIRECTORY_SEPARATOR . '.env ' , $ data );
62
65
}
63
66
64
67
/**
@@ -75,10 +78,10 @@ public function unconfigure(PackageContract $package): void
75
78
if (! \file_exists ($ env )) {
76
79
continue ;
77
80
}
78
- /** @codeCoverageIgnoreEnd */
81
+ // @codeCoverageIgnoreEnd
79
82
$ count = 0 ;
80
83
$ contents = \preg_replace (
81
- \sprintf ('{%s* ###> %s ###.*###< %s ###%s+}s ' , "\n" , $ package ->getPrettyName (), $ package ->getPrettyName (), "\n" ),
84
+ \sprintf ('{###> %s ###.*###< %s ###%s+}s ' , $ package ->getPrettyName (), $ package ->getPrettyName (), \ PHP_EOL ),
82
85
'' ,
83
86
(string ) \file_get_contents ($ env ),
84
87
-1 ,
@@ -93,7 +96,7 @@ public function unconfigure(PackageContract $package): void
93
96
94
97
$ this ->write (\sprintf ('Removing environment variables from %s ' , $ file ));
95
98
96
- \file_put_contents ($ env , $ contents );
99
+ $ this -> filesystem -> dumpFile ($ env , $ contents );
97
100
}
98
101
}
99
102
}
0 commit comments