1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Backup\Tasks\Backup; |
4
|
|
|
|
5
|
|
|
use Spatie\DbDumper\Databases\MongoDb; |
6
|
|
|
use Spatie\DbDumper\DbDumper; |
7
|
|
|
use Spatie\DbDumper\Databases\MySql; |
8
|
|
|
use Spatie\DbDumper\Databases\Sqlite; |
9
|
|
|
use Spatie\DbDumper\Databases\PostgreSql; |
10
|
|
|
use Spatie\Backup\Exceptions\CannotCreateDbDumper; |
11
|
|
|
|
12
|
|
|
class DbDumperFactory |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param string $dbConnectionName |
16
|
|
|
* |
17
|
|
|
* @return \Spatie\DbDumper\DbDumper |
18
|
|
|
*/ |
19
|
|
|
public static function createFromConnection(string $dbConnectionName): DbDumper |
20
|
|
|
{ |
21
|
|
|
$dbConfig = config("database.connections.{$dbConnectionName}"); |
22
|
|
|
|
23
|
|
|
$dbHost = array_get($dbConfig, 'read.host', array_get($dbConfig, 'host')); |
24
|
|
|
|
25
|
|
|
$dbDumper = static::forDriver($dbConfig['driver']) |
26
|
|
|
->setHost($dbHost ?? '') |
27
|
|
|
->setDbName($dbConfig['database']) |
28
|
|
|
->setUserName($dbConfig['username'] ?? '') |
29
|
|
|
->setPassword($dbConfig['password'] ?? ''); |
30
|
|
|
|
31
|
|
|
if ($dbDumper instanceof MySql) { |
32
|
|
|
$dbDumper->setDefaultCharacterSet($dbConfig['charset'] ?? ''); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (isset($dbConfig['port'])) { |
36
|
|
|
$dbDumper = $dbDumper->setPort($dbConfig['port']); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (isset($dbConfig['dump'])) { |
40
|
|
|
$dbDumper = static::processExtraDumpParameters($dbConfig['dump'], $dbDumper); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $dbDumper; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected static function forDriver($dbDriver): DbDumper |
47
|
|
|
{ |
48
|
|
|
$driver = strtolower($dbDriver); |
49
|
|
|
|
50
|
|
|
if ($driver === 'mysql') { |
51
|
|
|
return new MySql(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($driver === 'pgsql') { |
55
|
|
|
return new PostgreSql(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($driver === 'sqlite') { |
59
|
|
|
return new Sqlite(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($driver === 'mongodb') { |
63
|
|
|
return new MongoDb(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
throw CannotCreateDbDumper::unsupportedDriver($driver); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $dumpConfiguration |
71
|
|
|
* |
72
|
|
|
* @param $dbDumper |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
protected static function processExtraDumpParameters(array $dumpConfiguration, $dbDumper): DbDumper |
77
|
|
|
{ |
78
|
|
|
collect($dumpConfiguration)->each(function ($configValue, $configName) use ($dbDumper) { |
79
|
|
|
$methodName = lcfirst(studly_case(is_numeric($configName) ? $configValue : $configName)); |
80
|
|
|
$methodValue = is_numeric($configName) ? null : $configValue; |
81
|
|
|
|
82
|
|
|
$methodName = static::determineValidMethodName($dbDumper, $methodName); |
83
|
|
|
|
84
|
|
|
if (method_exists($dbDumper, $methodName)) { |
85
|
|
|
static::callMethodOnDumper($dbDumper, $methodName, $methodValue); |
86
|
|
|
} |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
return $dbDumper; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param \Spatie\DbDumper\DbDumper $dbDumper |
94
|
|
|
* @param string $methodName |
95
|
|
|
* @param string|null $methodValue |
96
|
|
|
* |
97
|
|
|
* @return \Spatie\DbDumper\DbDumper |
98
|
|
|
*/ |
99
|
|
|
protected static function callMethodOnDumper(DbDumper $dbDumper, string $methodName, $methodValue): DbDumper |
100
|
|
|
{ |
101
|
|
|
if (! $methodValue) { |
|
|
|
|
102
|
|
|
$dbDumper->$methodName(); |
103
|
|
|
|
104
|
|
|
return $dbDumper; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$dbDumper->$methodName($methodValue); |
108
|
|
|
|
109
|
|
|
return $dbDumper; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected static function determineValidMethodName(DbDumper $dbDumper, string $methodName): string |
113
|
|
|
{ |
114
|
|
|
return collect([$methodName, 'set'.ucfirst($methodName)]) |
115
|
|
|
->first(function (string $methodName) use ($dbDumper) { |
116
|
|
|
return method_exists($dbDumper, $methodName); |
117
|
|
|
}, ''); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: