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 Ray\AuraSqlModule\Annotation\AuraSql; |
11
|
|
|
use Ray\AuraSqlModule\Annotation\Read; |
12
|
|
|
use Ray\AuraSqlModule\Annotation\ReadOnlyConnection; |
13
|
|
|
use Ray\AuraSqlModule\Annotation\Write; |
14
|
|
|
use Ray\AuraSqlModule\Annotation\WriteConnection; |
15
|
|
|
use Ray\Di\AbstractModule; |
16
|
|
|
|
17
|
|
|
class AuraSqlLocatorModule extends AbstractModule |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ConnectionLocatorInterface |
21
|
|
|
*/ |
22
|
|
|
private $connectionLocator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string[] |
26
|
|
|
*/ |
27
|
|
|
private $readMethods; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string[] |
31
|
|
|
*/ |
32
|
|
|
private $writeMethods; |
33
|
|
|
|
34
|
3 |
|
public function __construct( |
35
|
|
|
ConnectionLocatorInterface $connectionLocator, |
36
|
|
|
array $readMethods = [], |
37
|
|
|
array $writeMethods = [], |
38
|
|
|
AbstractModule $module = null |
39
|
|
|
) { |
40
|
3 |
|
$this->connectionLocator = $connectionLocator; |
41
|
3 |
|
$this->readMethods = $readMethods; |
42
|
3 |
|
$this->writeMethods = $writeMethods; |
43
|
3 |
|
parent::__construct($module); |
|
|
|
|
44
|
3 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
3 |
|
protected function configure() |
50
|
|
|
{ |
51
|
3 |
|
if ((bool) $this->readMethods && (bool) $this->writeMethods) { |
52
|
3 |
|
$this->bind()->annotatedWith(Read::class)->toInstance($this->readMethods); |
53
|
3 |
|
$this->bind()->annotatedWith(Write::class)->toInstance($this->writeMethods); |
54
|
|
|
} |
55
|
3 |
|
if ($this->connectionLocator) { |
56
|
3 |
|
$this->bind(ConnectionLocatorInterface::class)->toInstance($this->connectionLocator); |
57
|
|
|
} |
58
|
3 |
|
$methods = \array_merge($this->readMethods, $this->writeMethods); |
59
|
|
|
// @AuraSql |
60
|
3 |
|
$this->installLocatorDb($methods); |
61
|
|
|
// @ReadOnlyConnection @WriteConnection |
62
|
3 |
|
$this->installReadWriteConnection(); |
63
|
|
|
// @Transactional |
64
|
3 |
|
$this->install(new TransactionalModule); |
|
|
|
|
65
|
3 |
|
} |
66
|
|
|
|
67
|
3 |
|
protected function installReadWriteConnection() |
68
|
|
|
{ |
69
|
|
|
// @ReadOnlyConnection |
70
|
3 |
|
$this->bindInterceptor( |
71
|
3 |
|
$this->matcher->any(), |
72
|
3 |
|
$this->matcher->annotatedWith(ReadOnlyConnection::class), |
73
|
3 |
|
[AuraSqlSlaveDbInterceptor::class] |
74
|
|
|
); |
75
|
|
|
// @WriteConnection |
76
|
3 |
|
$this->bindInterceptor( |
77
|
3 |
|
$this->matcher->any(), |
78
|
3 |
|
$this->matcher->annotatedWith(WriteConnection::class), |
79
|
3 |
|
[AuraSqlMasterDbInterceptor::class] |
80
|
|
|
); |
81
|
3 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string[] $methods |
85
|
|
|
*/ |
86
|
3 |
|
private function installLocatorDb(array $methods) |
87
|
|
|
{ |
88
|
|
|
// locator db |
89
|
3 |
|
$this->bindInterceptor( |
90
|
3 |
|
$this->matcher->annotatedWith(AuraSql::class), // @AuraSql in class |
91
|
3 |
|
$this->matcher->logicalAnd( |
92
|
3 |
|
new IsInMethodMatcher($methods), |
93
|
3 |
|
$this->matcher->logicalNot( |
94
|
3 |
|
$this->matcher->annotatedWith(ReadOnlyConnection::class) |
95
|
|
|
), |
96
|
3 |
|
$this->matcher->logicalNot( |
97
|
3 |
|
$this->matcher->annotatedWith(Connection::class) |
98
|
|
|
) |
99
|
|
|
), |
100
|
3 |
|
[AuraSqlConnectionInterceptor::class] |
101
|
|
|
); |
102
|
3 |
|
} |
103
|
|
|
} |
104
|
|
|
|
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.