1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Backup\BackupHandlers\Database; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Spatie\Backup\Console; |
7
|
|
|
|
8
|
|
|
class DatabaseBuilder |
9
|
|
|
{ |
10
|
|
|
protected $database; |
11
|
|
|
protected $console; |
12
|
|
|
|
13
|
|
|
public function __construct() |
14
|
|
|
{ |
15
|
|
|
$this->console = new Console(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function getDatabase(array $realConfig) |
19
|
|
|
{ |
20
|
|
|
switch($realConfig['driver']) { |
21
|
|
|
case 'mysql': |
22
|
|
|
try { |
23
|
|
|
$this->buildMySQL($realConfig); |
24
|
|
|
} catch (Exception $e) { |
25
|
|
|
throw new \Exception('Whoops, '.$e->getMessage()); |
26
|
|
|
} |
27
|
|
|
break; |
28
|
|
|
|
29
|
|
|
case 'pgsql': |
30
|
|
|
try { |
31
|
|
|
$this->buildPgSql($realConfig); |
32
|
|
|
} catch (Exception $e) { |
33
|
|
|
throw new \Exception('Whoops, '.$e->getMessage()); |
34
|
|
|
} |
35
|
|
|
break; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->database; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
protected function buildMySQL(array $config) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$port = isset($config['port']) ? $config['port'] : 3306; |
44
|
|
|
|
45
|
|
|
$socket = isset($config['unix_socket']) ? $config['unix_socket'] : ''; |
46
|
|
|
|
47
|
|
|
$this->database = new Databases\MySQLDatabase( |
48
|
|
|
$this->console, |
49
|
|
|
$config['database'], |
50
|
|
|
$config['username'], |
51
|
|
|
$config['password'], |
52
|
|
|
$this->determineHost($config), |
53
|
|
|
$port, |
54
|
|
|
$socket |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Build a PgSQLDatabase instance. |
60
|
|
|
* @param array $config |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
protected function buildPgSql(array $config) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$port = isset($config['port']) ? $config['port'] : 5432; |
65
|
|
|
|
66
|
|
|
$schema = isset($config['schema']) ? $config['schema'] : 'public'; |
67
|
|
|
|
68
|
|
|
$this->database = new Databases\PgSQLDatabase( |
69
|
|
|
$this->console, |
70
|
|
|
$config['database'], |
71
|
|
|
$schema, |
72
|
|
|
$config['username'], |
73
|
|
|
$config['password'], |
74
|
|
|
$this->determineHost($config), |
75
|
|
|
$port |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Determine the host from the given config. |
81
|
|
|
* |
82
|
|
|
* @param array $config |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
* |
86
|
|
|
* @throws Exception |
87
|
|
|
*/ |
88
|
|
|
public function determineHost(array $config) |
89
|
|
|
{ |
90
|
|
|
if (isset($config['host'])) { |
91
|
|
|
return $config['host']; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (isset($config['read']['host'])) { |
95
|
|
|
return $config['read']['host']; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
throw new Exception('could not determine host from config'); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.