1
|
|
|
<?php |
2
|
|
|
namespace phpbu\Laravel\Configuration; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Configuration; |
5
|
|
|
use phpbu\App\Configuration\Backup\Target; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Translator |
9
|
|
|
* |
10
|
|
|
* @package phpbu\Laravel |
11
|
|
|
* @author Sebastian Feldmann <[email protected]> |
12
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
13
|
|
|
* @license http://www.opensource.org/licenses/MIT The MIT License (MIT) |
14
|
|
|
* @link http://phpbu.de/ |
15
|
|
|
*/ |
16
|
|
|
class Translator |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Configuration Proxy |
20
|
|
|
* |
21
|
|
|
* @var \phpbu\Laravel\Configuration\Proxy |
22
|
|
|
*/ |
23
|
|
|
private $proxy; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Configurable laravel backup types. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $types = [ |
31
|
|
|
'directories' => 'directoryConfigToBackup', |
32
|
|
|
'databases' => 'databaseConfigToBackup', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Translates the laravel configuration to a phpbu configuration. |
37
|
|
|
* |
38
|
|
|
* @param \phpbu\Laravel\Configuration\Proxy $proxy |
39
|
|
|
* @return \phpbu\App\Configuration |
40
|
|
|
* @throws \phpbu\Laravel\Configuration\Exception |
41
|
|
|
*/ |
42
|
7 |
|
public function translate(Proxy $proxy) |
43
|
|
|
{ |
44
|
7 |
|
$this->proxy = $proxy; |
45
|
7 |
|
$laravelPhpbu = $this->proxy->get('phpbu'); |
46
|
7 |
|
$configuration = new Configuration(); |
47
|
7 |
|
$configuration->setFilename($this->proxy->get('phpbu.config')); |
48
|
|
|
|
49
|
7 |
|
$this->addBackups($configuration, $laravelPhpbu); |
50
|
|
|
|
51
|
3 |
|
return $configuration; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Translate and add all configured backups. |
56
|
|
|
* |
57
|
|
|
* @param \phpbu\App\Configuration $configuration |
58
|
|
|
* @param array $laravelPhpbu |
59
|
|
|
*/ |
60
|
7 |
|
protected function addBackups(Configuration $configuration, array $laravelPhpbu) |
61
|
|
|
{ |
62
|
|
|
// walk the the configured backups |
63
|
7 |
|
foreach (array_keys($this->types) as $type) { |
64
|
7 |
|
foreach ($laravelPhpbu[$type] as $conf) { |
65
|
|
|
// create and add a phpbu backup config |
66
|
7 |
|
$configuration->addBackup($this->translateBackup($type, $conf)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
3 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Translates a given laravel config type to a phpbu backup configuration. |
73
|
|
|
* |
74
|
|
|
* @param string $type |
75
|
|
|
* @param array $conf |
76
|
|
|
* @throws \phpbu\Laravel\Configuration\Exception |
77
|
|
|
* @return \phpbu\App\Configuration\Backup |
78
|
|
|
*/ |
79
|
7 |
|
public function translateBackup($type, array $conf) |
80
|
|
|
{ |
81
|
|
|
/** @var \phpbu\App\Configuration\Backup $backup */ |
82
|
7 |
|
$backup = $this->{$this->types[$type]}($conf); |
83
|
7 |
|
$backup->setTarget($this->translateTarget($conf['target'])); |
84
|
|
|
|
85
|
5 |
|
$this->addChecksIfConfigured($backup, $conf); |
86
|
5 |
|
$this->addSyncIfConfigured($backup, $conf); |
87
|
5 |
|
$this->addCleanupIfConfigured($backup, $conf); |
88
|
5 |
|
$this->addCryptIfConfigured($backup, $conf); |
89
|
|
|
|
90
|
5 |
|
return $backup; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Translate a laravel directory config to phpbu backup configuration. |
95
|
|
|
* |
96
|
|
|
* @param array $dir |
97
|
|
|
* @return \phpbu\App\Configuration\Backup |
98
|
|
|
* @throws \phpbu\Laravel\Configuration\Exception |
99
|
|
|
*/ |
100
|
7 |
|
protected function directoryConfigToBackup(array $dir) |
101
|
|
|
{ |
102
|
7 |
|
$backup = new Configuration\Backup($dir['source']['path'], false); |
103
|
|
|
// build source config |
104
|
|
|
$options = [ |
105
|
7 |
|
'path' => $dir['source']['path'], |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
// check for configuration options |
109
|
7 |
View Code Duplication |
if (isset($dir['source']['options'])) { |
|
|
|
|
110
|
7 |
|
$options = array_merge($options, $dir['source']['options']); |
111
|
|
|
} |
112
|
7 |
|
$backup->setSource(new Configuration\Backup\Source('tar', $options)); |
113
|
7 |
|
return $backup; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Translate a laravel db config to phpbu backup configuration. |
118
|
|
|
* |
119
|
|
|
* @param array $db |
120
|
|
|
* @throws \phpbu\Laravel\Configuration\Exception |
121
|
|
|
* @return \phpbu\App\Configuration\Backup |
122
|
|
|
*/ |
123
|
5 |
|
protected function databaseConfigToBackup(array $db) |
124
|
|
|
{ |
125
|
5 |
|
$connection = $this->getDatabaseConnectionConfig($db['source']['connection']); |
126
|
|
|
|
127
|
|
|
// translate laravel settings to source options |
128
|
|
|
$options = [ |
129
|
3 |
|
'host' => $connection['host'], |
130
|
3 |
|
'user' => $connection['username'], |
131
|
3 |
|
'password' => $connection['password'], |
132
|
3 |
|
'databases' => $connection['database'], |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
// check for configuration options |
136
|
3 |
View Code Duplication |
if (isset($db['source']['options'])) { |
|
|
|
|
137
|
3 |
|
$options = array_merge($options, $db['source']['options']); |
138
|
|
|
} |
139
|
|
|
|
140
|
3 |
|
$backup = new Configuration\Backup('db-' . $db['source']['connection'], false); |
141
|
3 |
|
$type = $this->getDatabaseSourceType($connection['driver']); |
142
|
3 |
|
$backup->setSource(new Configuration\Backup\Source($type, $options)); |
143
|
|
|
|
144
|
3 |
|
return $backup; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get a database connection configuration. |
149
|
|
|
* |
150
|
|
|
* @param string $connection |
151
|
|
|
* @return array |
152
|
|
|
* @throws \Exception |
153
|
|
|
*/ |
154
|
5 |
|
protected function getDatabaseConnectionConfig($connection) |
155
|
|
|
{ |
156
|
5 |
|
$connections = $this->proxy->get('database.connections'); |
157
|
5 |
|
if (!isset($connections[$connection])) { |
158
|
1 |
|
throw new Exception('Unknown database connection: ' . $connection); |
159
|
|
|
} |
160
|
4 |
|
$config = $connections[$connection]; |
161
|
4 |
|
if (!in_array($config['driver'], ['mysql', 'pgsql'])) { |
162
|
1 |
|
throw new Exception('Currently only MySQL and PostgreSQL databases are supported using the laravel config'); |
163
|
|
|
} |
164
|
3 |
|
return $config; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Map database driver to phpbu source type. |
169
|
|
|
* |
170
|
|
|
* @param string $driver |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
3 |
|
protected function getDatabaseSourceType($driver) |
174
|
|
|
{ |
175
|
3 |
|
$types = ['mysql' => 'mysqldump', 'pgsql' => 'pgdump']; |
176
|
3 |
|
return $types[$driver]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Translate the target configuration. |
181
|
|
|
* |
182
|
|
|
* @param array $config |
183
|
|
|
* @return Target |
184
|
|
|
* @throws \Exception |
185
|
|
|
*/ |
186
|
7 |
|
protected function translateTarget(array $config) |
187
|
|
|
{ |
188
|
7 |
|
if (empty($config['dirname'])) { |
189
|
1 |
|
throw new Exception('invalid target: dirname has to be configured'); |
190
|
|
|
} |
191
|
6 |
|
if (empty($config['filename'])) { |
192
|
1 |
|
throw new Exception('invalid target: filename has to be configured'); |
193
|
|
|
} |
194
|
5 |
|
$dirname = $config['dirname']; |
195
|
5 |
|
$filename = $config['filename']; |
196
|
5 |
|
$compression = !empty($config['compression']) ? $config['compression'] : null; |
197
|
|
|
|
198
|
5 |
|
return new Target($dirname, $filename, $compression); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Adds a check configuration to the given backup configuration. |
203
|
|
|
* |
204
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
205
|
|
|
* @param array $conf |
206
|
|
|
*/ |
207
|
5 |
|
protected function addChecksIfConfigured(Configuration\Backup $backup, array $conf) |
208
|
|
|
{ |
209
|
5 |
|
if (isset($conf['check'])) { |
210
|
1 |
|
$backup->addCheck( |
211
|
1 |
|
new Configuration\Backup\Check( |
212
|
1 |
|
$conf['check']['type'], |
213
|
1 |
|
$conf['check']['value'] |
214
|
|
|
) |
215
|
|
|
); |
216
|
|
|
} |
217
|
5 |
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Adds a sync configuration to the given backup configuration. |
221
|
|
|
* |
222
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
223
|
|
|
* @param array $conf |
224
|
|
|
*/ |
225
|
5 |
|
protected function addSyncIfConfigured(Configuration\Backup $backup, array $conf) |
226
|
|
|
{ |
227
|
5 |
|
if (isset($conf['sync'])) { |
228
|
1 |
|
$backup->addSync( |
229
|
1 |
|
new Configuration\Backup\Sync( |
230
|
1 |
|
'laravel-storage', |
231
|
1 |
|
false, |
232
|
|
|
[ |
233
|
1 |
|
'filesystem' => $conf['sync']['filesystem'], |
234
|
1 |
|
'path' => $conf['sync']['path'] |
235
|
|
|
] |
236
|
|
|
) |
237
|
|
|
); |
238
|
|
|
} |
239
|
5 |
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Adds a cleanup configuration to the given backup configuration. |
243
|
|
|
* |
244
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
245
|
|
|
* @param array $conf |
246
|
|
|
*/ |
247
|
5 |
|
protected function addCleanupIfConfigured(Configuration\Backup $backup, array $conf) |
248
|
|
|
{ |
249
|
5 |
|
if (isset($conf['cleanup'])) { |
250
|
1 |
|
$backup->setCleanup( |
251
|
1 |
|
new Configuration\Backup\Cleanup( |
252
|
1 |
|
$conf['cleanup']['type'], |
253
|
1 |
|
false, |
254
|
1 |
|
$conf['cleanup']['options'] |
255
|
|
|
) |
256
|
|
|
); |
257
|
|
|
} |
258
|
5 |
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Adds a encryption configuration to the given encryption configuration. |
262
|
|
|
* |
263
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
264
|
|
|
* @param array $conf |
265
|
|
|
*/ |
266
|
5 |
|
protected function addCryptIfConfigured(Configuration\Backup $backup, array $conf) |
267
|
|
|
{ |
268
|
5 |
|
if (isset($conf['crypt'])) { |
269
|
|
|
$backup->setCrypt( |
270
|
|
|
new Configuration\Backup\Crypt( |
271
|
|
|
$conf['crypt']['type'], |
272
|
|
|
false, |
273
|
|
|
$conf['crypt']['options'] |
274
|
|
|
) |
275
|
|
|
); |
276
|
|
|
} |
277
|
5 |
|
} |
278
|
|
|
} |
279
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.