Skip to content

Commit d39c729

Browse files
Add --generate-configuration option
With this option you can create a new json or xml configuration file skeleton. You still have to edit the configuration file manualy but the basic structure is created for you.
1 parent 05aea21 commit d39c729

File tree

4 files changed

+181
-20
lines changed

4 files changed

+181
-20
lines changed

src/Cmd.php

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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);
@@ -288,6 +291,45 @@ protected function handleVersionCheck()
288291
}
289292
}
290293

294+
/**
295+
* Create a configuration file by asking the user some questions
296+
*
297+
* @return void
298+
*/
299+
private function handleConfigGeneration()
300+
{
301+
$this->printVersionString();
302+
303+
print 'Configuration file format: xml|json (default: xml): ';
304+
$format = \trim(\fgets(\STDIN)) === 'json' ? 'json' : 'xml';
305+
$file = 'phpbu.' . $format;
306+
307+
if (file_exists($file)) {
308+
echo ' FAILED: The configuration file already exists.' . \PHP_EOL;
309+
exit(self::EXIT_EXCEPTION);
310+
}
311+
312+
print \PHP_EOL . 'Generating ' . $file . ' in ' . \getcwd() . \PHP_EOL . \PHP_EOL;
313+
314+
print 'Bootstrap script (relative to path shown above; e.g: vendor/autoload.php): ';
315+
$bootstrapScript = \trim(\fgets(\STDIN));
316+
317+
$generator = new Configuration\Generator;
318+
319+
\file_put_contents(
320+
$file,
321+
$generator->generateDefaultConfiguration(
322+
Version::minor(),
323+
$format,
324+
$bootstrapScript
325+
)
326+
);
327+
328+
print \PHP_EOL . 'Generated ' . $file . ' in ' . \getcwd() . \PHP_EOL . \PHP_EOL .
329+
'ATTENTION:' . \PHP_EOL .
330+
'The created configuration is just a skeleton. You have to finish the configuration manually.' . \PHP_EOL;
331+
}
332+
291333
/**
292334
* Returns latest released phpbu version.
293335
*
@@ -339,15 +381,16 @@ protected function printHelp()
339381
echo <<<EOT
340382
Usage: phpbu [option]
341383
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.
384+
--bootstrap=<file> A "bootstrap" PHP file that is included before the backup.
385+
--configuration=<file> A phpbu configuration file.
386+
--colors Use colors in output.
387+
--debug Display debugging information during backup generation.
388+
--generate-configuration Create a new configuration skeleton.
389+
--limit=<subset> Limit backup execution to a subset.
390+
--simulate Perform a trial run with no changes made.
391+
-h, --help Print this usage information.
392+
-v, --verbose Output more verbose information.
393+
-V, --version Output version information and exit.
351394
352395
EOT;
353396
if ($this->isPhar) {

src/Cmd/Args.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ final class Args
3535
* @var array
3636
*/
3737
private $longOptions = [
38-
'bootstrap=' => true,
39-
'colors' => true,
40-
'configuration=' => true,
41-
'debug' => true,
42-
'help' => true,
43-
'limit=' => true,
44-
'restore' => true,
45-
'simulate' => true,
46-
'verbose' => true,
47-
'version' => true
38+
'bootstrap=' => true,
39+
'colors' => true,
40+
'configuration=' => true,
41+
'debug' => true,
42+
'generate-configuration' => true,
43+
'help' => true,
44+
'limit=' => true,
45+
'restore' => true,
46+
'simulate' => true,
47+
'verbose' => true,
48+
'version' => true
4849
];
4950

5051
/**

src/Configuration/Generator.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace phpbu\App\Configuration;
4+
5+
final class Generator
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private const TEMPLATE_XML = <<<EOT
11+
<?xml version="1.0" encoding="UTF-8"?>
12+
<phpbu xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
13+
xsi:noNamespaceSchemaLocation="https://schema.phpbu.de/{phpbu_version}/phpbu.xsd"
14+
bootstrap="{bootstrap_script}"
15+
verbose="true">
16+
17+
<!-- uncomment if you want to use an adapter
18+
<adapters>
19+
</adapters>
20+
-->
21+
22+
<!-- uncomment if you want to use logging
23+
<logging>
24+
</logging>
25+
-->
26+
27+
<backups>
28+
<backup name="__FIXME__">
29+
<source type="__FIXME__">
30+
<option name="__FIXME__" value="__FIXME__" />
31+
</source>
32+
<target dirname="__FIXME__" filename="backup-%Y%m%d-%H%i" compress="bzip2" />
33+
</backup>
34+
</backups>
35+
</phpbu>
36+
37+
EOT;
38+
39+
/**
40+
* @var string
41+
*/
42+
private const TEMPLATE_JSON = <<<EOT
43+
{
44+
"bootstrap": "{bootstrap_script}",
45+
"verbose": true,
46+
"adapters": [
47+
],
48+
"logging": [
49+
],
50+
"backups": [
51+
{
52+
"source": {
53+
"type": "__FIXME__",
54+
"options": {
55+
"__FIXME__": "__FIXME__"
56+
}
57+
}
58+
"target": {
59+
"dirname": "__FIXME__",
60+
"filename": "backup-%Y%m%d-%H%i"
61+
"compress": "bzip2"
62+
}
63+
}
64+
]
65+
}
66+
67+
EOT;
68+
69+
/**
70+
* Return the config file content
71+
*
72+
* @param string $version
73+
* @param string $format
74+
* @param string $bootstrapScript
75+
* @return string
76+
*/
77+
public function generateDefaultConfiguration(string $version, string $format, string $bootstrapScript) : string
78+
{
79+
return \str_replace(
80+
[
81+
'{phpbu_version}',
82+
'{bootstrap_script}'
83+
],
84+
[
85+
$version,
86+
$bootstrapScript
87+
],
88+
$this->getTemplate($format)
89+
);
90+
}
91+
92+
/**
93+
* Return the template code for the requested format
94+
*
95+
* @param string $format
96+
* @return string
97+
*/
98+
private function getTemplate(string $format)
99+
{
100+
return $format === 'json' ? self::TEMPLATE_JSON : self::TEMPLATE_XML;
101+
}
102+
}

src/Version.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function __construct($release, $path)
6060
*
6161
* @return string
6262
*/
63-
public function getVersionNumber()
63+
public function getVersionNumber() : string
6464
{
6565
if ($this->number === null) {
6666
if (count(explode('.', $this->release)) == 3) {
@@ -87,6 +87,21 @@ public static function id() : string
8787
return self::$version;
8888
}
8989

90+
/**
91+
* Return the minor version number e.g. x.x, x.y, x.z
92+
*
93+
* @return string
94+
*/
95+
public static function minor() : string
96+
{
97+
if (\strpos(self::id(), '-')) {
98+
$version = \explode('-', self::id())[0];
99+
} else {
100+
$version = self::id();
101+
}
102+
return \implode('.', \array_slice(\explode('.', $version), 0, 2));
103+
}
104+
90105
/**
91106
* Return the version string.
92107
*

0 commit comments

Comments
 (0)