1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Sergii Bondarenko, <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
namespace Drupal\TqExtension\Utils\Database; |
6
|
|
|
|
7
|
|
|
// Helpers. |
8
|
|
|
use Behat\DebugExtension\Debugger; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Operator. |
12
|
|
|
* |
13
|
|
|
* @package Drupal\TqExtension\Utils\Database |
14
|
|
|
*/ |
15
|
|
|
class Operator |
16
|
|
|
{ |
17
|
|
|
use Debugger; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* MySQL and MySQLDump login arguments. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $credentials = ''; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Operator constructor. |
28
|
|
|
* |
29
|
|
|
* @param string $username |
30
|
|
|
* The name of MySQL user. |
31
|
|
|
* @param string $password |
32
|
|
|
* Password from an account of MySQL user. |
33
|
|
|
* @param string $host |
34
|
|
|
* MySQL host address. |
35
|
|
|
* @param string $port |
36
|
|
|
* Port of the MySQL host. |
37
|
|
|
*/ |
38
|
|
|
public function __construct($username, $password = '', $host = '', $port = '') |
39
|
|
|
{ |
40
|
|
|
// -u%s -p%s -h%s -P%s |
41
|
|
|
$credentials = []; |
42
|
|
|
|
43
|
|
|
foreach ([ |
44
|
|
|
'u' => $username, |
45
|
|
|
'p' => $password, |
46
|
|
|
'h' => $host, |
47
|
|
|
'P' => $port, |
48
|
|
|
] as $parameter => $value) { |
49
|
|
|
if (!empty($value)) { |
50
|
|
|
$credentials[] = "-$parameter$value"; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->credentials = implode(' ', $credentials); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $name |
59
|
|
|
* Name of the database to check. |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
* Checking state. |
63
|
|
|
*/ |
64
|
|
|
public function exist($name) |
65
|
|
|
{ |
66
|
|
|
return !empty($this->exec("mysql -e 'show databases' | grep '^$name$'")); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $name |
71
|
|
|
* Name of the database to create. |
72
|
|
|
*/ |
73
|
|
|
public function create($name) |
74
|
|
|
{ |
75
|
|
|
if (!$this->exist($name)) { |
76
|
|
|
$this->exec("mysql -e '%s database $name;'", __FUNCTION__); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $name |
82
|
|
|
* Name of the database to drop. |
83
|
|
|
*/ |
84
|
|
|
public function drop($name) |
85
|
|
|
{ |
86
|
|
|
if ($this->exist($name)) { |
87
|
|
|
$this->exec("mysql -e '%s database $name;'", __FUNCTION__); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $source |
93
|
|
|
* Source DB name. |
94
|
|
|
* @param string $destination |
95
|
|
|
* Name of the new DB. |
96
|
|
|
*/ |
97
|
|
|
public function copy($source, $destination) |
98
|
|
|
{ |
99
|
|
|
$this->drop($destination); |
100
|
|
|
$this->create($destination); |
101
|
|
|
$this->exec("mysqldump $source | mysql $destination"); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $name |
106
|
|
|
* Name of the DB. |
107
|
|
|
*/ |
108
|
|
|
public function clear($name) |
109
|
|
|
{ |
110
|
|
|
$this->drop($name); |
111
|
|
|
$this->create($name); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Executes a shell command. |
116
|
|
|
* |
117
|
|
|
* @param string $command |
118
|
|
|
* Command to execute. |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
* Result of a shell command. |
122
|
|
|
*/ |
123
|
|
|
private function exec($command) |
124
|
|
|
{ |
125
|
|
|
// Adding credentials after "mysql" and "mysqldump" commands. |
126
|
|
|
$command = preg_replace( |
127
|
|
|
'/(mysql(?:dump)?)/', |
128
|
|
|
"\\1 $this->credentials", |
129
|
|
|
vsprintf($command, array_slice(func_get_args(), 1)) |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
self::debug(['%s'], [$command]); |
133
|
|
|
|
134
|
|
|
return trim(shell_exec($command)); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|