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