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