-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathMysqlServerController.php
More file actions
255 lines (213 loc) · 7.72 KB
/
Copy pathMysqlServerController.php
File metadata and controls
255 lines (213 loc) · 7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
declare(strict_types=1);
namespace lucatume\WPBrowser\Extension;
use Codeception\Exception\ExtensionException;
use lucatume\WPBrowser\ManagedProcess\MysqlServer;
use lucatume\WPBrowser\Utils\Filesystem;
use Symfony\Component\Console\Output\OutputInterface;
class MysqlServerController extends ServiceExtension
{
use PidBasedController;
public const PID_FILE_NAME = 'mysql-server.pid';
/**
* @var \lucatume\WPBrowser\ManagedProcess\MysqlServer
*/
private $mysqlServer;
public function start(OutputInterface $output): void
{
$pidFile = $this->getPidFile();
$port = $this->getPort();
if ($this->isProcessRunning($pidFile) || $this->isMysqlServerReachable($port)) {
$output->writeln("MySQL server already running on port $port.");
return;
}
$database = $this->getDatabase();
$user = $this->getUser();
$password = $this->getPassword();
$binary = $this->getBinary();
$shareDir = $this->getShareDir($binary);
$output->write("Starting MySQL server on port $port ...");
try {
$this->mysqlServer = new MysqlServer(
codecept_output_dir('_mysql_server'),
$port,
$database,
$user,
$password,
$binary,
$shareDir
);
$this->mysqlServer->setOutput($output);
$this->mysqlServer->start();
} catch (\Exception $e) {
throw new ExtensionException($this, "Error while starting MySQL server. {$e->getMessage()}", $e);
}
$output->write(' ok', true);
}
public function getPidFile(): string
{
return codecept_output_dir(self::PID_FILE_NAME);
}
private function isMysqlServerReachable(int $port): bool
{
try {
new \PDO(
"mysql:host=127.0.0.1;port={$port}",
$this->getUser(),
$this->getPassword(),
[\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_TIMEOUT => 1]
);
return true;
} catch (\PDOException $e) {
// An auth or protocol-level error still proves a server is listening on the port;
// only a failure to connect at all (2002/2003) means no server is there.
return !in_array((int)$e->getCode(), [2002, 2003], true);
}
}
private function getDatabase(): string
{
/** @var array{database?: string} $config */
$config = $this->config;
if (isset($config['database']) && !(is_string($config['database']) && !empty($config['database']))) {
throw new ExtensionException(
$this,
'The "database" configuration option must be a string.'
);
}
return $config['database'] ?? 'wordpress';
}
private function getUser(): string
{
/** @var array{user?: string} $config */
$config = $this->config;
if (isset($config['user']) && !(is_string($config['user']) && !empty($config['user']))) {
throw new ExtensionException(
$this,
'The "user" configuration option must be a string.'
);
}
return $config['user'] ?? 'wordpress';
}
private function getPassword(): string
{
/** @var array{password?: string} $config */
$config = $this->config;
if (isset($config['password']) && !is_string($config['password'])) {
throw new ExtensionException(
$this,
'The "password" configuration option must be a string.'
);
}
return $config['password'] ?? 'wordpress';
}
/**
* @throws ExtensionException
*/
public function getPort(): int
{
$config = $this->config;
if (isset($config['port'])
&& !(
is_numeric($config['port'])
&& (int)$config['port'] == $config['port']
&& $config['port'] > 0
)) {
throw new ExtensionException(
$this,
'The "port" configuration option must be an integer greater than 0.'
);
}
/** @var array{port?: number} $config */
return (int)($config['port'] ?? 8906);
}
public function stop(OutputInterface $output): void
{
$pidFile = $this->getPidFile();
$mysqlServerPid = is_file($pidFile) ? (int)file_get_contents($pidFile) : 0;
if (!$mysqlServerPid) {
$output->writeln('MySQL server not running.');
return;
}
$output->write("Stopping MySQL server with PID $mysqlServerPid ...", false);
$this->kill($mysqlServerPid);
$this->removePidFile($pidFile);
$output->write(' ok', true);
}
public function getPrettyName(): string
{
return 'MySQL Community Server';
}
/**
* @return array{
* running: string,
* pidFile: string,
* port: int
* }
* @throws ExtensionException
*/
public function getInfo(): array
{
$isRunning = is_file($this->getPidFile()) || $this->isMysqlServerReachable($this->getPort());
$info = [
'running' => $isRunning ? 'yes' : 'no',
'pidFile' => Filesystem::relativePath(codecept_root_dir(), $this->getPidFile()),
'host' => '127.0.0.1',
'port' => $this->getPort(),
'user' => $this->getUser(),
'password' => $this->getPassword(),
'root user' => 'root',
'root password' => $this->getUser() === 'root' ? $this->getPassword() : ''
];
if ($isRunning) {
$info['mysql command'] = $this->getCliConnectionCommandline();
$info['mysql root command'] = $this->getRootCliConnectionCommandline();
}
return $info;
}
private function getCliConnectionCommandline(): string
{
if ($this->getPassword() === '') {
return "mysql -h 127.0.0.1 -P {$this->getPort()} -u {$this->getUser()}";
}
return "mysql -h 127.0.0.1 -P {$this->getPort()} -u {$this->getUser()} -p'{$this->getPassword()}'";
}
private function getRootCliConnectionCommandline(): string
{
$rootPassword = $this->getUser() === 'root' ? $this->getPassword() : '';
if ($rootPassword === '') {
return "mysql -h 127.0.0.1 -P {$this->getPort()} -u root";
}
return "mysql -h 127.0.0.1 -P {$this->getPort()} -u root -p'{$rootPassword}'";
}
private function getBinary(): ?string
{
$config = $this->config;
if (isset($config['binary']) && !(is_string($config['binary']) && is_executable($config['binary']))) {
throw new ExtensionException(
$this,
'The "binary" configuration option must be an executable file.'
);
}
/** @var array{binary?: string} $config */
return ($config['binary'] ?? null);
}
private function getShareDir(?string $binary): ?string
{
/** @var array{shareDir?: string} $config */
$config = $this->config;
if (isset($config['shareDir']) && !(is_string($config['shareDir']) && is_dir($config['shareDir']))) {
throw new ExtensionException(
$this,
'The "shareDir" configuration option must be a directory.'
);
}
$shareDir = $config['shareDir'] ?? null;
if ($binary && $shareDir === null) {
throw new ExtensionException(
$this,
'The "shareDir" configuration option must be set when using a custom binary.'
);
}
return $shareDir;
}
}