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\ConnectionLocatorInterface; |
10
|
|
|
use Aura\Sql\ExtendedPdoInterface; |
11
|
|
|
use Ray\AuraSqlModule\Annotation\ReadOnlyConnection; |
12
|
|
|
use Ray\AuraSqlModule\Annotation\WriteConnection; |
13
|
|
|
use Ray\Di\AbstractModule; |
14
|
|
|
use Ray\Di\Scope; |
15
|
|
|
|
16
|
|
|
class AuraSqlReplicationModule extends AbstractModule |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ConnectionLocatorInterface |
20
|
|
|
*/ |
21
|
|
|
private $connectionLocator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $qualifer; |
27
|
|
|
|
28
|
8 |
|
public function __construct( |
29
|
|
|
ConnectionLocatorInterface $connectionLocator, |
30
|
|
|
string $qualifer = '', |
31
|
|
|
AbstractModule $module = null |
32
|
|
|
) { |
33
|
8 |
|
$this->connectionLocator = $connectionLocator; |
34
|
8 |
|
$this->qualifer = $qualifer; |
35
|
8 |
|
parent::__construct($module); |
|
|
|
|
36
|
8 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
8 |
|
protected function configure() |
42
|
|
|
{ |
43
|
8 |
|
if ($this->connectionLocator) { |
44
|
8 |
|
$this->bind(ConnectionLocatorInterface::class)->annotatedWith($this->qualifer)->toInstance($this->connectionLocator); |
45
|
|
|
} |
46
|
|
|
// ReadOnlyConnection when GET, otherwise WriteConnection |
47
|
8 |
|
$this->bind(ExtendedPdoInterface::class)->annotatedWith($this->qualifer)->toProvider(AuraSqlReplicationDbProvider::class, $this->qualifer)->in(Scope::SINGLETON); |
48
|
|
|
// @ReadOnlyConnection @WriteConnection |
49
|
8 |
|
$this->installReadWriteConnection(); |
50
|
|
|
// @Transactional |
51
|
8 |
|
$this->install(new TransactionalModule); |
|
|
|
|
52
|
8 |
|
} |
53
|
|
|
|
54
|
8 |
|
protected function installReadWriteConnection() |
55
|
|
|
{ |
56
|
|
|
// @ReadOnlyConnection |
57
|
8 |
|
$this->bindInterceptor( |
58
|
8 |
|
$this->matcher->any(), |
59
|
8 |
|
$this->matcher->annotatedWith(ReadOnlyConnection::class), |
60
|
8 |
|
[AuraSqlSlaveDbInterceptor::class] |
61
|
|
|
); |
62
|
|
|
// @WriteConnection |
63
|
8 |
|
$this->bindInterceptor( |
64
|
8 |
|
$this->matcher->any(), |
65
|
8 |
|
$this->matcher->annotatedWith(WriteConnection::class), |
66
|
8 |
|
[AuraSqlMasterDbInterceptor::class] |
67
|
|
|
); |
68
|
8 |
|
} |
69
|
|
|
} |
70
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.