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
|
|
|
* @param string $dsn |
42
|
|
|
* @param string $user |
43
|
|
|
* @param string $password |
44
|
|
|
* @param string $slave comma separated slave host list |
45
|
|
|
*/ |
46
|
8 |
|
public function __construct($dsn, $user = '', $password = '', $slave = null) |
47
|
|
|
{ |
48
|
8 |
|
$this->dsn = $dsn; |
49
|
8 |
|
$this->user = $user; |
50
|
8 |
|
$this->password = $password; |
51
|
8 |
|
$this->slave = $slave; |
52
|
8 |
|
parent::__construct(); |
53
|
8 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
8 |
|
protected function configure() |
59
|
|
|
{ |
60
|
8 |
|
$this->slave ? $this->configureMasterSlaveDsn() : $this->configureSingleDsn(); |
61
|
|
|
// @Transactional |
62
|
8 |
|
$this->install(new TransactionalModule); |
63
|
8 |
|
$this->install(new AuraSqlPagerModule()); |
64
|
8 |
|
preg_match(self::PARSE_PDO_DSN_REGEX, $this->dsn, $parts); |
65
|
8 |
|
$dbType = isset($parts[1]) ? $parts[1] : ''; |
66
|
8 |
|
$this->install(new AuraSqlQueryModule($dbType)); |
67
|
8 |
|
} |
68
|
|
|
|
69
|
6 |
|
private function configureSingleDsn() |
70
|
|
|
{ |
71
|
6 |
|
$this->bind(ExtendedPdoInterface::class)->toConstructor(ExtendedPdo::class, 'dsn=pdo_dsn,username=pdo_user,password=pdo_pass,options=pdo_option')->in(Scope::SINGLETON); |
72
|
6 |
|
$this->bind()->annotatedWith('pdo_dsn')->toInstance($this->dsn); |
73
|
6 |
|
$this->bind()->annotatedWith('pdo_user')->toInstance($this->user); |
74
|
6 |
|
$this->bind()->annotatedWith('pdo_pass')->toInstance($this->password); |
75
|
6 |
|
$this->bind()->annotatedWith('pdo_option')->toInstance([]); |
76
|
6 |
|
} |
77
|
|
|
|
78
|
2 |
|
private function configureMasterSlaveDsn() |
79
|
|
|
{ |
80
|
2 |
|
$locator = new ConnectionLocator; |
81
|
2 |
|
$locator->setWrite('master', new Connection($this->dsn, $this->user, $this->password)); |
82
|
2 |
|
$i = 1; |
83
|
2 |
|
$slaves = explode(',', $this->slave); |
84
|
2 |
|
foreach ($slaves as $slave) { |
85
|
2 |
|
$slaveDsn = $this->changeHost($this->dsn, $slave); |
86
|
2 |
|
$name = 'slave' . (string) $i++; |
87
|
2 |
|
$locator->setRead($name, new Connection($slaveDsn, $this->user, $this->password)); |
88
|
|
|
} |
89
|
2 |
|
$this->install(new AuraSqlReplicationModule($locator)); |
90
|
2 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $dsn |
94
|
|
|
* @param string $host |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
2 |
|
private function changeHost($dsn, $host) |
99
|
|
|
{ |
100
|
2 |
|
preg_match(self::PARSE_PDO_DSN_REGEX, $dsn, $parts); |
101
|
2 |
|
if (! $parts) { |
|
|
|
|
102
|
|
|
return $dsn; |
103
|
|
|
} |
104
|
2 |
|
$dsn = sprintf('%s:%s=%s;%s', $parts[1], $parts[2], $host, $parts[3]); |
105
|
|
|
|
106
|
2 |
|
return $dsn; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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.