@@ -138,6 +138,9 @@ protected function handleArgs(array $options)
138138 case '--self-update ' :
139139 $ this ->handleSelfUpdate ();
140140 exit (self ::EXIT_SUCCESS );
141+ case '--generate-configuration ' :
142+ $ this ->handleConfigGeneration ();
143+ exit (self ::EXIT_SUCCESS );
141144 case '--version-check ' :
142145 $ this ->handleVersionCheck ();
143146 exit (self ::EXIT_SUCCESS );
@@ -183,11 +186,25 @@ protected function findConfiguration() : string
183186 * @return \phpbu\App\Configuration
184187 * @throws \phpbu\App\Exception
185188 */
186- protected function createConfiguration (string $ configurationFile , Factory $ factory )
189+ protected function createConfiguration (string $ configurationFile , Factory $ factory ) : Configuration
187190 {
188191 // setup bootstrapper with --bootstrap option that has precedence over the config file value
189192 $ bootstrapper = new Bootstrapper (Arr::getValue ($ this ->arguments , 'bootstrap ' , '' ));
190193 $ configLoader = Configuration \Loader \Factory::createLoader ($ configurationFile , $ bootstrapper );
194+
195+ if ($ configLoader ->hasValidationErrors ()) {
196+ echo " Warning - The configuration file did not pass validation! " . \PHP_EOL .
197+ " The following problems have been detected: " . \PHP_EOL ;
198+
199+ foreach ($ configLoader ->getValidationErrors () as $ line => $ errors ) {
200+ echo \sprintf ("\n Line %d: \n" , $ line );
201+ foreach ($ errors as $ msg ) {
202+ echo \sprintf (" - %s \n" , $ msg );
203+ }
204+ }
205+ echo \PHP_EOL ;
206+ }
207+
191208 $ configuration = $ configLoader ->getConfiguration ($ factory );
192209
193210 // command line arguments overrule the config file settings
@@ -262,7 +279,7 @@ protected function handleSelfUpdate()
262279 unset($ phar );
263280 // replace current phar with the new one
264281 rename ($ tempFilename , $ localFilename );
265- } catch (Exception $ e ) {
282+ } catch (\ Exception $ e ) {
266283 // cleanup crappy phar
267284 unlink ($ tempFilename );
268285 echo 'failed ' . PHP_EOL . $ e ->getMessage () . PHP_EOL ;
@@ -288,6 +305,45 @@ protected function handleVersionCheck()
288305 }
289306 }
290307
308+ /**
309+ * Create a configuration file by asking the user some questions
310+ *
311+ * @return void
312+ */
313+ private function handleConfigGeneration ()
314+ {
315+ $ this ->printVersionString ();
316+
317+ print 'Configuration file format: xml|json (default: xml): ' ;
318+ $ format = \trim (\fgets (\STDIN )) === 'json ' ? 'json ' : 'xml ' ;
319+ $ file = 'phpbu. ' . $ format ;
320+
321+ if (file_exists ($ file )) {
322+ echo ' FAILED: The configuration file already exists. ' . \PHP_EOL ;
323+ exit (self ::EXIT_EXCEPTION );
324+ }
325+
326+ print \PHP_EOL . 'Generating ' . $ file . ' in ' . \getcwd () . \PHP_EOL . \PHP_EOL ;
327+
328+ print 'Bootstrap script (relative to path shown above; e.g: vendor/autoload.php): ' ;
329+ $ bootstrapScript = \trim (\fgets (\STDIN ));
330+
331+ $ generator = new Configuration \Generator ;
332+
333+ \file_put_contents (
334+ $ file ,
335+ $ generator ->generateConfigurationSkeleton (
336+ Version::minor (),
337+ $ format ,
338+ $ bootstrapScript
339+ )
340+ );
341+
342+ print \PHP_EOL . 'Generated ' . $ file . ' in ' . \getcwd () . \PHP_EOL . \PHP_EOL .
343+ 'ATTENTION: ' . \PHP_EOL .
344+ 'The created configuration is just a skeleton. You have to finish the configuration manually. ' . \PHP_EOL ;
345+ }
346+
291347 /**
292348 * Returns latest released phpbu version.
293349 *
@@ -339,15 +395,16 @@ protected function printHelp()
339395 echo <<<EOT
340396Usage: phpbu [option]
341397
342- --bootstrap=<file> A "bootstrap" PHP file that is included before the backup.
343- --configuration=<file> A phpbu configuration file.
344- --colors Use colors in output.
345- --debug Display debugging information during backup generation.
346- --limit=<subset> Limit backup execution to a subset.
347- --simulate Perform a trial run with no changes made.
348- -h, --help Print this usage information.
349- -v, --verbose Output more verbose information.
350- -V, --version Output version information and exit.
398+ --bootstrap=<file> A "bootstrap" PHP file that is included before the backup.
399+ --configuration=<file> A phpbu configuration file.
400+ --colors Use colors in output.
401+ --debug Display debugging information during backup generation.
402+ --generate-configuration Create a new configuration skeleton.
403+ --limit=<subset> Limit backup execution to a subset.
404+ --simulate Perform a trial run with no changes made.
405+ -h, --help Print this usage information.
406+ -v, --verbose Output more verbose information.
407+ -V, --version Output version information and exit.
351408
352409EOT ;
353410 if ($ this ->isPhar ) {
0 commit comments