@@ -32,6 +32,22 @@ class ConfigHelper extends Helper
3232 */
3333 protected $ cachedConfig = null ;
3434
35+ /**
36+ * Filesystem
37+ *
38+ * @var Filesystem
39+ */
40+ protected $ filesystem ;
41+
42+ public function __construct (Filesystem $ filesystem = null )
43+ {
44+ if (null === $ filesystem ) {
45+ $ filesystem = new Filesystem ();
46+ }
47+
48+ $ this ->filesystem = $ filesystem ;
49+ }
50+
3551 /**
3652 * {@inheritDoc}
3753 */
@@ -89,7 +105,7 @@ public function loadConfig()
89105 $ fullPath = $ configDir . '/ ' . $ configKey . '.yml ' ;
90106 $ config [$ configKey ] = array ();
91107
92- if (file_exists ($ fullPath )) {
108+ if ($ this -> filesystem -> exists ($ fullPath )) {
93109 $ config [$ configKey ] = Yaml::parse ($ fullPath );
94110 }
95111 }
@@ -111,31 +127,31 @@ public function getConfig($type)
111127 }
112128
113129 $ this ->loadConfig ();
130+
114131 return $ this ->cachedConfig [$ type ];
115132 }
116133
117134 /**
118135 * Initialize a configuration files
119136 */
120- public function initConfig (OutputInterface $ output = null , DialogHelper $ dialogHelper = null )
137+ public function initConfig (OutputInterface $ output = null , DialogHelper $ dialogHelper = null , $ noInteraction = false )
121138 {
122139 $ log = function ($ message ) use ($ output ) {
123140 if ($ output ) {
124141 $ output ->writeln ($ message );
125142 }
126143 };
127144
128- $ fs = new Filesystem ();
129145 if (strtoupper (substr (PHP_OS , 0 , 3 )) === 'WIN ' ) {
130146 throw new \RuntimeException ('This feature is currently only supported on Linux and OSX (maybe). Please submit a PR to support it on windows. ' );
131147 }
132148
133149 $ configDir = $ this ->getConfigDir ();
134150 $ distDir = __DIR__ . '/../../Resources/config.dist ' ;
135151
136- if (!file_exists ($ configDir )) {
152+ if (!$ this -> filesystem -> exists ($ configDir )) {
137153 $ log ('<info>[+] Creating directory:</info> ' . $ configDir );
138- $ fs ->mkdir ($ configDir );
154+ $ this -> filesystem ->mkdir ($ configDir );
139155 }
140156
141157 $ configFilenames = array (
@@ -146,21 +162,28 @@ public function initConfig(OutputInterface $output = null, DialogHelper $dialogH
146162 $ srcFile = $ distDir . '/ ' . $ configFilename ;
147163 $ destFile = $ configDir . '/ ' . $ configFilename ;
148164
149- if (!file_exists ($ srcFile )) {
165+ if (!$ this -> filesystem -> exists ($ srcFile )) {
150166 throw new \Exception ('Dist (source) file " ' . $ srcFile . '" does not exist. ' );
151167 }
152168
153- if (file_exists ($ destFile )) {
169+ if ($ this -> filesystem -> exists ($ destFile )) {
154170 if (null !== $ dialogHelper ) {
155- if (!$ dialogHelper ->askConfirmation ($ output , '" ' . $ configFilename . '" already exists, do you want to overwrite it? ' )) {
156- return 0 ;
171+ if (false === $ noInteraction ) {
172+ $ confirmed = $ dialogHelper ->askConfirmation (
173+ $ output ,
174+ '" ' . $ configFilename . '" already exists, do you want to overwrite it? '
175+ );
176+
177+ if (!$ confirmed ) {
178+ return ;
179+ }
157180 }
158181 } else {
159182 $ log (sprintf ('<info>File</info> %s <info> already exists, not overwriting. ' , $ destFile ));
160183 }
161184 }
162185
163- $ fs ->copy ($ srcFile , $ destFile );
186+ $ this -> filesystem ->copy ($ srcFile , $ destFile );
164187 $ log ('<info>[+] Creating file:</info> ' . $ destFile );
165188 }
166189 }
0 commit comments