1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Backup\Tasks\Backup; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Database\ConfigurationUrlParser; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Spatie\Backup\Exceptions\CannotCreateDbDumper; |
10
|
|
|
use Spatie\DbDumper\Databases\MongoDb; |
11
|
|
|
use Spatie\DbDumper\Databases\MySql; |
12
|
|
|
use Spatie\DbDumper\Databases\PostgreSql; |
13
|
|
|
use Spatie\DbDumper\Databases\Sqlite; |
14
|
|
|
use Spatie\DbDumper\DbDumper; |
15
|
|
|
|
16
|
|
|
class DbDumperFactory |
17
|
|
|
{ |
18
|
|
|
protected static $custom = []; |
19
|
|
|
|
20
|
|
|
public static function createFromConnection(string $dbConnectionName): DbDumper |
21
|
|
|
{ |
22
|
|
|
$parser = new ConfigurationUrlParser(); |
23
|
|
|
try { |
24
|
|
|
$dbConfig = $parser->parseConfiguration(config("database.connections.{$dbConnectionName}")); |
25
|
|
|
} catch (Exception $e) { |
26
|
|
|
throw CannotCreateDbDumper::unsupportedDriver($dbConnectionName); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if (isset($dbConfig['read'])) { |
30
|
|
|
$dbConfig = Arr::except( |
31
|
|
|
array_merge($dbConfig, $dbConfig['read']), |
32
|
|
|
['read', 'write'] |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$dbDumper = static::forDriver($dbConfig['driver'] ?? '') |
37
|
|
|
->setHost(Arr::first(Arr::wrap($dbConfig['host'] ?? ''))) |
38
|
|
|
->setDbName($dbConfig['database']) |
39
|
|
|
->setUserName($dbConfig['username'] ?? '') |
40
|
|
|
->setPassword($dbConfig['password'] ?? ''); |
41
|
|
|
|
42
|
|
|
if ($dbDumper instanceof MySql) { |
43
|
|
|
$dbDumper->setDefaultCharacterSet($dbConfig['charset'] ?? ''); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($dbDumper instanceof MongoDb) { |
47
|
|
|
$dbDumper->setAuthenticationDatabase($dbConfig['dump']['mongodb_user_auth'] ?? ''); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (isset($dbConfig['port'])) { |
51
|
|
|
$dbDumper = $dbDumper->setPort($dbConfig['port']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (isset($dbConfig['dump'])) { |
55
|
|
|
$dbDumper = static::processExtraDumpParameters($dbConfig['dump'], $dbDumper); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $dbDumper; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public static function extend(string $driver, callable $callback) |
62
|
|
|
{ |
63
|
|
|
static::$custom[$driver] = $callback; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected static function forDriver($dbDriver): DbDumper |
67
|
|
|
{ |
68
|
|
|
$driver = strtolower($dbDriver); |
69
|
|
|
|
70
|
|
|
if (isset(static::$custom[$driver])) { |
71
|
|
|
return (static::$custom[$driver])(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($driver === 'mysql' || $driver === 'mariadb') { |
75
|
|
|
return new MySql(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($driver === 'pgsql') { |
79
|
|
|
return new PostgreSql(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($driver === 'sqlite') { |
83
|
|
|
return new Sqlite(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($driver === 'mongodb') { |
87
|
|
|
return new MongoDb(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
throw CannotCreateDbDumper::unsupportedDriver($driver); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected static function processExtraDumpParameters(array $dumpConfiguration, DbDumper $dbDumper): DbDumper |
94
|
|
|
{ |
95
|
|
|
collect($dumpConfiguration)->each(function ($configValue, $configName) use ($dbDumper) { |
96
|
|
|
$methodName = lcfirst(Str::studly(is_numeric($configName) ? $configValue : $configName)); |
97
|
|
|
$methodValue = is_numeric($configName) ? null : $configValue; |
98
|
|
|
|
99
|
|
|
$methodName = static::determineValidMethodName($dbDumper, $methodName); |
100
|
|
|
|
101
|
|
|
if (method_exists($dbDumper, $methodName)) { |
102
|
|
|
static::callMethodOnDumper($dbDumper, $methodName, $methodValue); |
103
|
|
|
} |
104
|
|
|
}); |
105
|
|
|
|
106
|
|
|
return $dbDumper; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected static function callMethodOnDumper(DbDumper $dbDumper, string $methodName, $methodValue): DbDumper |
110
|
|
|
{ |
111
|
|
|
if (! $methodValue) { |
112
|
|
|
$dbDumper->$methodName(); |
113
|
|
|
|
114
|
|
|
return $dbDumper; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$dbDumper->$methodName($methodValue); |
118
|
|
|
|
119
|
|
|
return $dbDumper; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected static function determineValidMethodName(DbDumper $dbDumper, string $methodName): string |
123
|
|
|
{ |
124
|
|
|
return collect([$methodName, 'set'.ucfirst($methodName)]) |
125
|
|
|
->first(function (string $methodName) use ($dbDumper) { |
126
|
|
|
return method_exists($dbDumper, $methodName); |
127
|
|
|
}, ''); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|