1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Runner; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup; |
|
|
|
|
5
|
|
|
use phpbu\App\Backup\Compressor; |
6
|
|
|
use phpbu\App\Backup\Collector; |
7
|
|
|
use phpbu\App\Backup\Target; |
8
|
|
|
use phpbu\App\Configuration; |
9
|
|
|
use phpbu\App\Result; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Simulate Runner |
13
|
|
|
* |
14
|
|
|
* @package phpbu |
15
|
|
|
* @author Sebastian Feldmann <[email protected]> |
16
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
17
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
18
|
|
|
* @link https://phpbu.de/ |
19
|
|
|
* @since Class available since Release 5.1.0 |
20
|
|
|
*/ |
21
|
|
|
class Simulate extends Process |
22
|
|
|
{ |
23
|
|
|
use Compression; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Execute backups. |
27
|
|
|
* |
28
|
|
|
* @param \phpbu\App\Configuration $configuration |
29
|
|
|
* @return \phpbu\App\Result |
30
|
|
|
* @throws \Exception |
31
|
|
|
*/ |
32
|
5 |
|
public function run(Configuration $configuration) : Result |
33
|
|
|
{ |
34
|
5 |
|
$this->configuration = $configuration; |
35
|
5 |
|
$this->result->phpbuStart($configuration); |
36
|
|
|
|
37
|
|
|
// create backups |
38
|
|
|
/** @var \phpbu\App\Configuration\Backup $backup */ |
39
|
5 |
|
foreach ($configuration->getBackups() as $backup) { |
40
|
|
|
// make sure the backup should be executed and is not excluded via the --limit option |
41
|
5 |
View Code Duplication |
if (!$configuration->isBackupActive($backup->getName())) { |
42
|
1 |
|
$this->result->debug('skipping backup: ' . $backup->getName() . PHP_EOL); |
43
|
1 |
|
continue; |
44
|
|
|
} |
45
|
|
|
// setup target and collector |
46
|
5 |
|
$target = $this->factory->createTarget($backup->getTarget()); |
47
|
5 |
|
$collector = new Collector($target); |
48
|
|
|
|
49
|
5 |
|
$this->simulateSource($backup, $target); |
50
|
5 |
|
$this->simulateChecks($backup, $target, $collector); |
51
|
5 |
|
$this->simulateCrypt($backup, $target); |
52
|
5 |
|
$this->simulateSyncs($backup, $target); |
53
|
5 |
|
$this->simulateCleanup($backup, $target, $collector); |
54
|
|
|
} |
55
|
5 |
|
$this->result->phpbuEnd(); |
56
|
|
|
|
57
|
5 |
|
return $this->result; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Simulate the backup. |
62
|
|
|
* |
63
|
|
|
* @param \phpbu\App\Configuration\Backup $conf |
64
|
|
|
* @param \phpbu\App\Backup\Target $target |
65
|
|
|
* @throws \Exception |
66
|
|
|
*/ |
67
|
5 |
View Code Duplication |
protected function simulateSource(Configuration\Backup $conf, Target $target) |
68
|
|
|
{ |
69
|
5 |
|
$this->result->backupStart($conf); |
70
|
|
|
/* @var \phpbu\App\Runner\Source $runner */ |
71
|
5 |
|
$source = $this->factory->createSource($conf->getSource()->type, $conf->getSource()->options); |
72
|
|
|
|
73
|
5 |
|
if ($source instanceof Backup\Source\Simulator) { |
74
|
5 |
|
$status = $source->simulate($target, $this->result); |
75
|
5 |
|
$this->compress($status, $target, $this->result); |
76
|
|
|
} |
77
|
5 |
|
$this->result->backupEnd($conf); |
78
|
5 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Simulate checks. |
82
|
|
|
* |
83
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
84
|
|
|
* @param \phpbu\App\Backup\Target $target |
85
|
|
|
* @param \phpbu\App\Backup\Collector $collector |
86
|
|
|
* @throws \Exception |
87
|
|
|
*/ |
88
|
5 |
View Code Duplication |
protected function simulateChecks(Configuration\Backup $backup, Target $target, Collector $collector) |
89
|
|
|
{ |
90
|
5 |
|
foreach ($backup->getChecks() as $config) { |
91
|
5 |
|
$this->result->checkStart($config); |
92
|
5 |
|
$check = $this->factory->createCheck($config->type); |
93
|
5 |
|
if ($check instanceof Backup\Check\Simulator) { |
94
|
5 |
|
$check->simulate($target, $config->value, $collector, $this->result); |
95
|
|
|
} |
96
|
5 |
|
$this->result->checkEnd($config); |
97
|
|
|
} |
98
|
5 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Simulate encryption. |
102
|
|
|
* |
103
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
104
|
|
|
* @param \phpbu\App\Backup\Target $target |
105
|
|
|
* @throws \phpbu\App\Exception |
106
|
|
|
*/ |
107
|
5 |
View Code Duplication |
protected function simulateCrypt(Configuration\Backup $backup, Target $target) |
108
|
|
|
{ |
109
|
5 |
|
if ($backup->hasCrypt()) { |
110
|
5 |
|
$crypt = $backup->getCrypt(); |
111
|
5 |
|
$this->result->cryptStart($crypt); |
112
|
5 |
|
$crypter = $this->factory->createCrypter($crypt->type, $crypt->options); |
113
|
5 |
|
if ($crypter instanceof Backup\Crypter\Simulator) { |
114
|
5 |
|
$crypter->simulate($target, $this->result); |
115
|
|
|
} |
116
|
5 |
|
$this->result->cryptEnd($crypt); |
117
|
|
|
} |
118
|
5 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Simulate all syncs. |
122
|
|
|
* |
123
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
124
|
|
|
* @param \phpbu\App\Backup\Target $target |
125
|
|
|
* @throws \Exception |
126
|
|
|
*/ |
127
|
5 |
|
protected function simulateSyncs(Configuration\Backup $backup, Target $target) |
128
|
|
|
{ |
129
|
|
|
/* @var \phpbu\App\Configuration\Backup\Sync $sync */ |
130
|
5 |
|
foreach ($backup->getSyncs() as $sync) { |
131
|
5 |
|
$sync = $this->factory->createSync($sync->type, $sync->options); |
132
|
5 |
|
if ($sync instanceof Backup\Sync\Simulator) { |
133
|
5 |
|
$sync->simulate($target, $this->result); |
134
|
|
|
} |
135
|
|
|
} |
136
|
5 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Simulate the cleanup. |
140
|
|
|
* |
141
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
142
|
|
|
* @param \phpbu\App\Backup\Target $target |
143
|
|
|
* @param \phpbu\App\Backup\Collector $collector |
144
|
|
|
* @throws \phpbu\App\Exception |
145
|
|
|
*/ |
146
|
5 |
View Code Duplication |
protected function simulateCleanup(Configuration\Backup $backup, Target $target, Collector $collector) |
147
|
|
|
{ |
148
|
|
|
/* @var \phpbu\App\Configuration\Backup\Cleanup $cleanup */ |
149
|
5 |
|
if ($backup->hasCleanup()) { |
150
|
5 |
|
$cleanup = $backup->getCleanup(); |
151
|
5 |
|
$cleaner = $this->factory->createCleaner($cleanup->type, $cleanup->options); |
152
|
5 |
|
$this->result->cleanupStart($cleanup); |
153
|
5 |
|
if ($cleaner instanceof Backup\Cleaner\Simulator) { |
154
|
5 |
|
$cleaner->simulate($target, $collector, $this->result); |
155
|
|
|
} |
156
|
5 |
|
$this->result->cleanupEnd($cleanup); |
157
|
|
|
} |
158
|
5 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Execute the compressor. |
162
|
|
|
* Returns the path to the created archive file. |
163
|
|
|
* |
164
|
|
|
* @param \phpbu\App\Backup\Compressor\Executable $compressor |
165
|
|
|
* @param \phpbu\App\Backup\Target $target |
166
|
|
|
* @param \phpbu\App\Result $result |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
3 |
|
protected function executeCompressor(Compressor\Executable $compressor, Target $target, Result $result) : string |
170
|
|
|
{ |
171
|
3 |
|
$result->debug($compressor->getExecutable($target)->getCommand()); |
172
|
3 |
|
return $compressor->getArchiveFile($target); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: