|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Ray.AuraSqlModule package. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Ray\AuraSqlModule; |
|
8
|
|
|
|
|
9
|
|
|
use Aura\Sql\ConnectionLocator; |
|
10
|
|
|
use Aura\Sql\ExtendedPdo; |
|
11
|
|
|
use Aura\Sql\ExtendedPdoInterface; |
|
12
|
|
|
use Ray\Di\AbstractModule; |
|
13
|
|
|
|
|
14
|
|
|
class NamedPdoModule extends AbstractModule |
|
15
|
|
|
{ |
|
16
|
|
|
const PARSE_PDO_DSN_REGEX = '/(.*?)\:(host|server)=.*?;(.*)/i'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
private $qualifer; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $dsn; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $user; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $password; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private $slave; |
|
42
|
|
|
|
|
43
|
6 |
|
public function __construct( |
|
44
|
|
|
string $qualifer, |
|
45
|
|
|
string $dsn, |
|
46
|
|
|
string $user = '', |
|
47
|
|
|
string $pass = '', |
|
48
|
|
|
string $slave = '' |
|
49
|
|
|
) { |
|
50
|
6 |
|
$this->qualifer = $qualifer; |
|
51
|
6 |
|
$this->dsn = $dsn; |
|
52
|
6 |
|
$this->user = $user; |
|
53
|
6 |
|
$this->password = $pass; |
|
54
|
6 |
|
$this->slave = $slave; |
|
55
|
6 |
|
parent::__construct(); |
|
56
|
6 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
6 |
|
protected function configure() |
|
62
|
|
|
{ |
|
63
|
6 |
|
$this->slave ? $this->configureMasterSlaveDsn($this->qualifer, $this->dsn, $this->user, $this->password, $this->slave) |
|
64
|
3 |
|
: $this->configureSingleDsn($this->qualifer, $this->dsn, $this->user, $this->password); |
|
65
|
6 |
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
private function configureSingleDsn($qualifer, $dsn, $user, $password) |
|
68
|
|
|
{ |
|
69
|
3 |
|
$this->bind(ExtendedPdoInterface::class) |
|
70
|
3 |
|
->annotatedWith($qualifer) |
|
71
|
3 |
|
->toConstructor( |
|
72
|
3 |
|
ExtendedPdo::class, |
|
73
|
3 |
|
"dsn={$qualifer}_dsn,username={$qualifer}_username,password={$qualifer}_password" |
|
74
|
|
|
); |
|
75
|
3 |
|
$this->bind()->annotatedWith("{$qualifer}_dsn")->toInstance($dsn); |
|
76
|
3 |
|
$this->bind()->annotatedWith("{$qualifer}_username")->toInstance($user); |
|
77
|
3 |
|
$this->bind()->annotatedWith("{$qualifer}_password")->toInstance($password); |
|
78
|
3 |
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
private function configureMasterSlaveDsn($qualifer, $dsn, $user, $password, $slaveList) |
|
81
|
|
|
{ |
|
82
|
3 |
|
$locator = new ConnectionLocator(); |
|
83
|
3 |
|
$locator->setWrite('master', new Connection($dsn, $user, $password)); |
|
84
|
3 |
|
$i = 1; |
|
85
|
3 |
|
$slaves = \explode(',', $slaveList); |
|
86
|
3 |
|
foreach ($slaves as $slave) { |
|
87
|
3 |
|
$slaveDsn = $this->changeHost($dsn, $slave); |
|
88
|
3 |
|
$name = 'slave' . (string) $i++; |
|
89
|
3 |
|
$locator->setRead($name, new Connection($slaveDsn, $user, $password)); |
|
90
|
|
|
} |
|
91
|
3 |
|
$this->install(new AuraSqlReplicationModule($locator, $qualifer)); |
|
|
|
|
|
|
92
|
3 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $dsn |
|
96
|
|
|
* @param string $host |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
3 |
|
private function changeHost($dsn, $host) |
|
101
|
|
|
{ |
|
102
|
3 |
|
\preg_match(self::PARSE_PDO_DSN_REGEX, $dsn, $parts); |
|
103
|
3 |
|
if (! $parts) { |
|
|
|
|
|
|
104
|
1 |
|
return $dsn; |
|
105
|
|
|
} |
|
106
|
2 |
|
$dsn = \sprintf('%s:%s=%s;%s', $parts[1], $parts[2], $host, $parts[3]); |
|
107
|
|
|
|
|
108
|
2 |
|
return $dsn; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: