|
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\AuraSqlModule\Pagerfanta\AuraSqlPagerModule; |
|
13
|
|
|
use Ray\Di\AbstractModule; |
|
14
|
|
|
use Ray\Di\Scope; |
|
15
|
|
|
|
|
16
|
|
|
class AuraSqlModule extends AbstractModule |
|
17
|
|
|
{ |
|
18
|
|
|
const PARSE_PDO_DSN_REGEX = '/(.*?)\:(?:(host|server)=.*?;)?(.*)/i'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $dsn; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $user; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $password; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $slave; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
private $options; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
private $attributes; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $dsn |
|
52
|
|
|
* @param string $user |
|
53
|
|
|
* @param string $password |
|
54
|
|
|
* @param string $slave comma separated slave host list |
|
55
|
|
|
* @param array $options |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct(string $dsn, string $user = '', string $password = '', string $slave = '', array $options = [], array $attributes) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->dsn = $dsn; |
|
60
|
|
|
$this->user = $user; |
|
61
|
|
|
$this->password = $password; |
|
62
|
|
|
$this->slave = $slave; |
|
63
|
|
|
$this->options = $options; |
|
64
|
|
|
$this->attributes = $attributes; |
|
65
|
|
|
parent::__construct(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function configure() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->slave ? $this->configureMasterSlaveDsn() : $this->configureSingleDsn(); |
|
74
|
|
|
// @Transactional |
|
75
|
|
|
$this->install(new TransactionalModule); |
|
|
|
|
|
|
76
|
|
|
$this->install(new AuraSqlPagerModule()); |
|
|
|
|
|
|
77
|
|
|
\preg_match(self::PARSE_PDO_DSN_REGEX, $this->dsn, $parts); |
|
78
|
|
|
$dbType = $parts[1] ?? ''; |
|
79
|
|
|
$this->install(new AuraSqlQueryModule($dbType)); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function configureSingleDsn() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->bind(ExtendedPdoInterface::class)->toConstructor(ExtendedPdo::class, 'dsn=pdo_dsn,username=pdo_user,password=pdo_pass,options=pdo_option')->in(Scope::SINGLETON); |
|
85
|
|
|
$this->bind()->annotatedWith('pdo_dsn')->toInstance($this->dsn); |
|
86
|
|
|
$this->bind()->annotatedWith('pdo_user')->toInstance($this->user); |
|
87
|
|
|
$this->bind()->annotatedWith('pdo_pass')->toInstance($this->password); |
|
88
|
|
|
$this->bind()->annotatedWith('pdo_option')->toInstance($this->options); |
|
89
|
|
|
$this->bind()->annotatedWith('pdo_attributes')->toInstance($this->options); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function configureMasterSlaveDsn() |
|
93
|
|
|
{ |
|
94
|
|
|
$locator = new ConnectionLocator; |
|
95
|
|
|
$locator->setWrite('master', new Connection($this->dsn, $this->user, $this->password)); |
|
96
|
|
|
$i = 1; |
|
97
|
|
|
$slaves = \explode(',', $this->slave); |
|
98
|
|
|
foreach ($slaves as $slave) { |
|
99
|
|
|
$slaveDsn = $this->changeHost($this->dsn, $slave); |
|
100
|
|
|
$name = 'slave' . (string) $i++; |
|
101
|
|
|
$locator->setRead($name, new Connection($slaveDsn, $this->user, $this->password)); |
|
102
|
|
|
} |
|
103
|
|
|
$this->install(new AuraSqlReplicationModule($locator)); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private function changeHost($dsn, $host) : string |
|
107
|
|
|
{ |
|
108
|
|
|
\preg_match(self::PARSE_PDO_DSN_REGEX, $dsn, $parts); |
|
109
|
|
|
if (! $parts) { |
|
|
|
|
|
|
110
|
|
|
return $dsn; |
|
111
|
|
|
} |
|
112
|
|
|
$dsn = \sprintf('%s:%s=%s;%s', $parts[1], $parts[2], $host, $parts[3]); |
|
113
|
|
|
|
|
114
|
|
|
return $dsn; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
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: