1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ray\AuraSqlModule; |
6
|
|
|
|
7
|
|
|
use Aura\Sql\ExtendedPdoInterface; |
8
|
|
|
use Ray\Di\AbstractModule; |
9
|
|
|
use Ray\Di\Scope; |
10
|
|
|
|
11
|
|
|
class AuraSqlEnvModule extends AbstractModule |
12
|
|
|
{ |
13
|
|
|
private string $dsn; |
14
|
|
|
private string $username; |
15
|
|
|
private string $password; |
16
|
|
|
private string $slave; |
17
|
|
|
|
18
|
|
|
/** @var array<string> */ |
19
|
|
|
private array $options; |
20
|
|
|
|
21
|
|
|
/** @var array<string> */ |
22
|
|
|
private array $queries; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $dsnKey Env key for Data Source Name (DSN) |
26
|
|
|
* @param string $usernameKey Env key for Username for the DSN string |
27
|
|
|
* @param string $passwordKey Env key for Password for the DSN string |
28
|
|
|
* @param string $slaveKey Env key for Comma separated slave host list |
29
|
|
|
* @param array<string> $options A key=>value array of driver-specific connection options |
30
|
|
|
* @param array<string> $queries Queries to execute after the connection. |
31
|
|
|
*/ |
32
|
|
|
public function __construct( |
33
|
|
|
string $dsnKey, |
34
|
|
|
string $usernameKey = '', |
35
|
|
|
string $passwordKey = '', |
36
|
|
|
string $slaveKey = '', |
37
|
|
|
array $options = [], |
38
|
|
|
array $queries = [] |
39
|
|
|
) { |
40
|
|
|
$this->dsn = $dsnKey; |
41
|
|
|
$this->username = $usernameKey; |
42
|
|
|
$this->password = $passwordKey; |
43
|
|
|
$this->slave = $slaveKey; |
44
|
|
|
$this->options = $options; |
45
|
|
|
$this->queries = $queries; |
46
|
|
|
parent::__construct(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
protected function configure(): void |
53
|
|
|
{ |
54
|
|
|
$this->slave ? $this->configureMasterSlaveDsn() : $this->configureSingleDsn(); |
55
|
|
|
$this->install(new AuraSqlBaseModule($this->dsn)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function configureSingleDsn(): void |
59
|
|
|
{ |
60
|
|
|
$this->bind(Connection::class)->toInstance( |
61
|
|
|
new Connection($this->dsn, $this->username, $this->password, $this->options, $this->queries) |
62
|
|
|
); |
63
|
|
|
$this->bind(ExtendedPdoInterface::class)->toProvider(ExtendedPdoProvider::class)->in(Scope::SINGLETON); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function configureMasterSlaveDsn(): void |
67
|
|
|
{ |
68
|
|
|
$locator = ConnectionLocatorFactory::fromEnv( |
69
|
|
|
$this->dsn, |
70
|
|
|
$this->username, |
71
|
|
|
$this->password, |
72
|
|
|
$this->slave, |
73
|
|
|
$this->options, |
74
|
|
|
$this->queries |
75
|
|
|
); |
76
|
|
|
$this->install(new AuraSqlReplicationModule($locator)); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|