|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ray\AuraSqlModule; |
|
4
|
|
|
|
|
5
|
|
|
use Aura\Sql\ConnectionLocator; |
|
6
|
|
|
use Aura\Sql\ExtendedPdo; |
|
7
|
|
|
use Aura\Sql\ExtendedPdoInterface; |
|
8
|
|
|
use Ray\Di\AbstractModule; |
|
9
|
|
|
|
|
10
|
|
|
class NamedPdoModule extends AbstractModule |
|
11
|
|
|
{ |
|
12
|
|
|
const PARSE_PDO_DSN_REGEX = '/(.*?)\:(host|server)=.*?;(.*)/i'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $qualifer; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $dsn; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $user; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $password; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $slave; |
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $qualifer |
|
40
|
|
|
* @param string $dsn |
|
41
|
|
|
* @param string $user |
|
42
|
|
|
* @param string $pass |
|
43
|
|
|
*/ |
|
44
|
21 |
|
public function __construct($qualifer, $dsn, $user = '', $pass = '', $slave = '') |
|
45
|
|
|
{ |
|
46
|
21 |
|
$this->qualifer = $qualifer; |
|
47
|
21 |
|
$this->dsn = $dsn; |
|
48
|
21 |
|
$this->user = $user; |
|
49
|
21 |
|
$this->password = $pass; |
|
50
|
21 |
|
$this->slave = $slave; |
|
51
|
21 |
|
parent::__construct(); |
|
52
|
21 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
21 |
|
protected function configure() |
|
58
|
|
|
{ |
|
59
|
21 |
|
$this->slave ? $this->configureMasterSlaveDsn($this->qualifer, $this->dsn, $this->user, $this->password, $this->slave) |
|
60
|
18 |
|
: $this->configureSingleDsn($this->qualifer, $this->dsn, $this->user, $this->password); |
|
61
|
21 |
|
} |
|
62
|
|
|
|
|
63
|
12 |
|
private function configureSingleDsn($qualifer, $dsn, $user, $password) |
|
64
|
|
|
{ |
|
65
|
12 |
|
$this->bind(ExtendedPdoInterface::class) |
|
66
|
12 |
|
->annotatedWith($qualifer) |
|
67
|
12 |
|
->toConstructor( |
|
68
|
12 |
|
ExtendedPdo::class, |
|
69
|
12 |
|
"dsn={$qualifer}_dsn,username={$qualifer}_username,password={$qualifer}_password" |
|
70
|
8 |
|
); |
|
71
|
12 |
|
$this->bind()->annotatedWith("{$qualifer}_dsn")->toInstance($dsn); |
|
72
|
12 |
|
$this->bind()->annotatedWith("{$qualifer}_username")->toInstance($user); |
|
73
|
12 |
|
$this->bind()->annotatedWith("{$qualifer}_password")->toInstance($password); |
|
74
|
12 |
|
} |
|
75
|
|
|
|
|
76
|
9 |
|
private function configureMasterSlaveDsn($qualifer, $dsn, $user, $password, $slaveList) |
|
77
|
|
|
{ |
|
78
|
9 |
|
$locator = new ConnectionLocator(); |
|
79
|
9 |
|
$locator->setWrite('master', new Connection($dsn, $user, $password)); |
|
80
|
9 |
|
$i = 1; |
|
81
|
9 |
|
$slaves = explode(',', $slaveList); |
|
82
|
9 |
|
foreach ($slaves as $slave) { |
|
83
|
9 |
|
$slaveDsn = $this->changeHost($dsn, $slave); |
|
84
|
9 |
|
$name = 'slave' . (string) $i++; |
|
85
|
9 |
|
$locator->setRead($name, new Connection($slaveDsn, $user, $password)); |
|
86
|
6 |
|
} |
|
87
|
9 |
|
$this->install(new AuraSqlReplicationModule($locator, $qualifer)); |
|
88
|
9 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $dsn |
|
92
|
|
|
* @param string $host |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
9 |
|
private function changeHost($dsn, $host) |
|
97
|
|
|
{ |
|
98
|
9 |
|
preg_match(self::PARSE_PDO_DSN_REGEX, $dsn, $parts); |
|
99
|
9 |
|
if (! $parts) { |
|
|
|
|
|
|
100
|
3 |
|
return $dsn; |
|
101
|
|
|
} |
|
102
|
6 |
|
$dsn = sprintf('%s:%s=%s;%s', $parts[1], $parts[2], $host, $parts[3]); |
|
103
|
|
|
|
|
104
|
6 |
|
return $dsn; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.