|
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
|
|
|
|
|
10
|
|
|
class NamedPdoEnvModule extends AbstractModule |
|
11
|
|
|
{ |
|
12
|
|
|
public const PARSE_PDO_DSN_REGEX = '/(.*?)\:(host|server)=.*?;(.*)/i'; |
|
13
|
|
|
|
|
14
|
|
|
private string $qualifer; |
|
15
|
|
|
private string $dsn; |
|
16
|
|
|
private string $username; |
|
17
|
|
|
private string $password; |
|
18
|
|
|
private string $slave; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array<string> */ |
|
21
|
|
|
private array $options; |
|
22
|
|
|
|
|
23
|
|
|
/** @var array<string> */ |
|
24
|
|
|
private array $queries; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $qualifer Qualifer for ExtendedPdoInterface |
|
28
|
|
|
* @param string $dsn Data Source Name (DSN) |
|
29
|
|
|
* @param string $username User name for the DSN string |
|
30
|
|
|
* @param string $password Password for the DSN string |
|
31
|
|
|
* @param string $slave Comma separated slave host list |
|
32
|
|
|
* @param array<string> $options A key=>value array of driver-specific connection options |
|
33
|
|
|
* @param array<string> $queries Queries to execute after the connection. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct( |
|
36
|
|
|
string $qualifer, |
|
37
|
|
|
string $dsn, |
|
38
|
|
|
string $username = '', |
|
39
|
|
|
string $password = '', |
|
40
|
|
|
string $slave = '', |
|
41
|
|
|
array $options = [], |
|
42
|
|
|
array $queries = [] |
|
43
|
|
|
) { |
|
44
|
|
|
$this->qualifer = $qualifer; |
|
45
|
|
|
$this->dsn = $dsn; |
|
46
|
|
|
$this->username = $username; |
|
47
|
|
|
$this->password = $password; |
|
48
|
|
|
$this->slave = $slave; |
|
49
|
|
|
$this->options = $options; |
|
50
|
|
|
$this->queries = $queries; |
|
51
|
|
|
parent::__construct(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function configure(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$this->slave ? $this->configureMasterSlaveDsn() |
|
60
|
|
|
: $this->configureSingleDsn(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function configureSingleDsn(): void |
|
64
|
|
|
{ |
|
65
|
|
|
$connection = new EnvConnection( |
|
66
|
|
|
$this->dsn, |
|
67
|
|
|
null, |
|
68
|
|
|
$this->username, |
|
69
|
|
|
$this->password, |
|
70
|
|
|
$this->options, |
|
71
|
|
|
$this->queries |
|
72
|
|
|
); |
|
73
|
|
|
$this->bind(EnvConnection::class)->annotatedWith($this->qualifer)->toInstance($connection); |
|
74
|
|
|
$this->bind(ExtendedPdoInterface::class)->annotatedWith($this->qualifer)->toProvider( |
|
75
|
|
|
NamedExtendedPdoProvider::class, |
|
76
|
|
|
$this->qualifer |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function configureMasterSlaveDsn(): void |
|
81
|
|
|
{ |
|
82
|
|
|
$locator = ConnectionLocatorFactory::fromEnv( |
|
83
|
|
|
$this->dsn, |
|
84
|
|
|
$this->username, |
|
85
|
|
|
$this->password, |
|
86
|
|
|
$this->slave, |
|
87
|
|
|
$this->options, |
|
88
|
|
|
$this->queries |
|
89
|
|
|
); |
|
90
|
|
|
$this->install(new AuraSqlReplicationModule($locator, $this->qualifer)); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|