Skip to content

Commit 0e6c03b

Browse files
Merge branch 'master' into 5.1
2 parents baa171f + 00e2fa4 commit 0e6c03b

File tree

86 files changed

+604
-491
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+604
-491
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*.phar
22
*.asc
33
.buildpath
4+
.DS_Store
45
.idea
56
.env
67
.project

build.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@
8989

9090
<exec executable="${basedir}/build/phar-manifest.php" output="${basedir}/build/phar/manifest.txt"/>
9191

92+
<!-- Copy .xsd for config validation -->
93+
<copy file="${basedir}/phpbu.xsd" tofile="${basedir}/build/phar/phpbu.xsd"/>
94+
9295
<!-- SF/CLI -->
9396
<copy file="${basedir}/vendor/sebastianfeldmann/cli/LICENSE" tofile="${basedir}/build/phar/lib/sf-cli/LICENSE"/>
9497
<copy todir="${basedir}/build/phar/lib/sf-cli">

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@
3030
"phpbu\\App\\": "src/"
3131
}
3232
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"phpbu\\App\\": "tests/phpbu/"
36+
}
37+
},
3338
"require": {
3439
"php": ">=7.0.0",
3540
"ext-dom": "*",
3641
"ext-json": "*",
3742
"ext-spl": "*",
38-
"sebastian/environment": "~1.1|~2.0|~3.0",
39-
"sebastianfeldmann/cli": "~2.0",
43+
"sebastian/environment": "~1.1|~2.0|~3.0|~4.0",
44+
"sebastianfeldmann/cli": "~2.2",
4045
"phpmailer/phpmailer": "~6.0",
4146
"symfony/event-dispatcher": "~2.6|~3.0|~4.0"
4247
},
@@ -45,6 +50,7 @@
4550
"sebastianfeldmann/ftp": "~0.9",
4651
"aws/aws-sdk-php": "~3.10",
4752
"kunalvarma05/dropbox-php-sdk": "~0.2",
53+
"tightenco/collect": "5.*",
4854
"phpseclib/phpseclib": "~2.0",
4955
"softlayer/objectstorage": "dev-master",
5056
"vlucas/phpdotenv": "~2.4",

phpunit.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@
1313
<logging>
1414
<log type="coverage-html"
1515
target="build/coverage"
16-
title="PHPBU"
17-
charset="UTF-8"
18-
yui="true"
19-
highlight="true"
2016
lowUpperBound="35"
2117
highLowerBound="70" />
2218
<log type="junit"
23-
target="build/logs/junit.xml"
24-
logIncompleteSkipped="false" />
19+
target="build/logs/junit.xml" />
2520
</logging>
2621
</phpunit>

src/Cli/Executable/Abstraction.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ protected function setup(string $cmd, string $path = '')
6464
*/
6565
public function getCommandLine() : CommandLine
6666
{
67-
return $this->createCommandLine();
67+
$process = $this->createCommandLine();
68+
$process->pipeFail(true);
69+
70+
return $process;
6871
}
6972

7073
/**

src/Cmd.php

Lines changed: 68 additions & 11 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);
@@ -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
340396
Usage: 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
352409
EOT;
353410
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+
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+
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 generateConfigurationSkeleton(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/Configuration/Loader.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace phpbu\App\Configuration;
33

4+
use phpbu\App\Configuration;
45
use phpbu\App\Factory;
56

67
/**
@@ -22,5 +23,19 @@ interface Loader
2223
* @param \phpbu\App\Factory $factory
2324
* @return \phpbu\App\Configuration
2425
*/
25-
public function getConfiguration(Factory $factory);
26+
public function getConfiguration(Factory $factory) : Configuration;
27+
28+
/**
29+
* Is the configuration valid
30+
*
31+
* @return bool
32+
*/
33+
public function hasValidationErrors() : bool;
34+
35+
/**
36+
* Return a list of all validation errors
37+
*
38+
* @return array
39+
*/
40+
public function getValidationErrors() : array;
2641
}

0 commit comments

Comments
 (0)