-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLDockerContainer.php
More file actions
97 lines (73 loc) · 3.14 KB
/
MySQLDockerContainer.php
File metadata and controls
97 lines (73 loc) · 3.14 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
<?php
declare(strict_types=1);
namespace TinyBlocks\DockerContainer;
use TinyBlocks\DockerContainer\Contracts\MySQL\MySQLContainerStarted;
use TinyBlocks\DockerContainer\Internal\Containers\Drivers\MySQL\MySQLCommands;
use TinyBlocks\DockerContainer\Internal\Containers\Drivers\MySQL\MySQLStarted;
use TinyBlocks\DockerContainer\Waits\Conditions\MySQL\MySQLReady;
use TinyBlocks\DockerContainer\Waits\ContainerWaitAfterStarted;
use TinyBlocks\DockerContainer\Waits\ContainerWaitForDependency;
class MySQLDockerContainer extends GenericDockerContainer implements MySQLContainer
{
private array $grantedHosts = [];
public function run(
array $commands = [],
?ContainerWaitAfterStarted $waitAfterStarted = null
): MySQLContainerStarted {
$containerStarted = parent::run(commands: $commands);
$condition = MySQLReady::from(container: $containerStarted);
$waitForDependency = ContainerWaitForDependency::untilReady(condition: $condition);
$waitForDependency->waitBefore();
$environmentVariables = $containerStarted->getEnvironmentVariables();
$database = $environmentVariables->getValueBy(key: 'MYSQL_DATABASE');
$rootPassword = $environmentVariables->getValueBy(key: 'MYSQL_ROOT_PASSWORD');
if (!empty($database)) {
$command = MySQLCommands::createDatabase(database: $database, rootPassword: $rootPassword);
$containerStarted->executeAfterStarted(commands: [$command]);
}
if (!empty($this->grantedHosts)) {
foreach ($this->grantedHosts as $host) {
$command = MySQLCommands::grantPrivilegesToRoot(host: $host, rootPassword: $rootPassword);
$containerStarted->executeAfterStarted(commands: [$command]);
}
}
return MySQLStarted::from(containerStarted: $containerStarted);
}
public function runIfNotExists(
array $commands = [],
?ContainerWaitAfterStarted $waitAfterStarted = null
): MySQLContainerStarted {
$containerStarted = parent::runIfNotExists(commands: $commands);
return MySQLStarted::from(containerStarted: $containerStarted);
}
public function withTimezone(string $timezone): static
{
$this->withEnvironmentVariable(key: 'TZ', value: $timezone);
return $this;
}
public function withUsername(string $user): static
{
$this->withEnvironmentVariable(key: 'MYSQL_USER', value: $user);
return $this;
}
public function withPassword(string $password): static
{
$this->withEnvironmentVariable(key: 'MYSQL_PASSWORD', value: $password);
return $this;
}
public function withDatabase(string $database): static
{
$this->withEnvironmentVariable(key: 'MYSQL_DATABASE', value: $database);
return $this;
}
public function withRootPassword(string $rootPassword): static
{
$this->withEnvironmentVariable(key: 'MYSQL_ROOT_PASSWORD', value: $rootPassword);
return $this;
}
public function withGrantedHosts(array $hosts = ['%', '172.%']): static
{
$this->grantedHosts = $hosts;
return $this;
}
}