1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ray\AuraSqlModule; |
6
|
|
|
|
7
|
|
|
use Aura\Sql\ConnectionLocatorInterface; |
8
|
|
|
use Aura\Sql\ExtendedPdo; |
9
|
|
|
use Aura\Sql\ExtendedPdoInterface; |
10
|
|
|
use Ray\AuraSqlModule\Pagerfanta\AuraSqlPagerModule; |
11
|
|
|
use Ray\Di\AbstractModule; |
12
|
|
|
use Ray\Di\Scope; |
13
|
|
|
|
14
|
|
|
use function preg_match; |
15
|
|
|
|
16
|
|
|
class AuraSqlModule extends AbstractModule |
17
|
|
|
{ |
18
|
|
|
public const PARSE_PDO_DSN_REGEX = '/(.*?):(?:(host|server)=.*?;)?(.*)/i'; |
19
|
|
|
|
20
|
|
|
private string $dsn; |
21
|
|
|
private string $user; |
22
|
|
|
private string $password; |
23
|
|
|
private string $slave; |
24
|
|
|
|
25
|
|
|
/** @var array<mixed> */ |
26
|
|
|
private array $options; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $dsn Data Source Name (DSN) |
30
|
|
|
* @param string $user User name for the DSN string |
31
|
|
|
* @param string $password Password for the DSN string |
32
|
|
|
* @param string $slave Comma separated slave host list |
33
|
|
|
* @param array<mixed> $options A key=>value array of driver-specific connection options |
34
|
|
|
*/ |
35
|
|
|
public function __construct(string $dsn, string $user = '', string $password = '', string $slave = '', array $options = []) |
36
|
|
|
{ |
37
|
|
|
$this->dsn = $dsn; |
38
|
|
|
$this->user = $user; |
39
|
|
|
$this->password = $password; |
40
|
|
|
$this->slave = $slave; |
41
|
|
|
$this->options = $options; |
42
|
|
|
parent::__construct(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
protected function configure(): void |
49
|
|
|
{ |
50
|
|
|
$this->slave ? $this->configureMasterSlaveDsn() : $this->configureSingleDsn(); |
51
|
|
|
// @Transactional |
52
|
|
|
$this->install(new TransactionalModule()); |
53
|
|
|
$this->install(new AuraSqlPagerModule()); |
54
|
|
|
preg_match(self::PARSE_PDO_DSN_REGEX, $this->dsn, $parts); |
55
|
|
|
$dbType = $parts[1] ?? ''; |
56
|
|
|
$this->install(new AuraSqlQueryModule($dbType)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function configureSingleDsn(): void |
60
|
|
|
{ |
61
|
|
|
$this->bind()->annotatedWith('pdo_dsn')->toInstance($this->dsn); |
62
|
|
|
$this->bind()->annotatedWith('pdo_user')->toInstance($this->user); |
63
|
|
|
$this->bind()->annotatedWith('pdo_pass')->toInstance($this->password); |
64
|
|
|
$this->bind()->annotatedWith('pdo_slave')->toInstance($this->slave); |
65
|
|
|
$this->bind()->annotatedWith('pdo_options')->toInstance($this->options); |
66
|
|
|
$this->bind()->annotatedWith('pdo_attributes')->toInstance($this->options); |
67
|
|
|
$this->bind(ExtendedPdoInterface::class)->toConstructor(ExtendedPdo::class, 'dsn=pdo_dsn,username=pdo_user,password=pdo_pass,options=pdo_options,attributes=pdo_attributes')->in(Scope::SINGLETON); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function configureMasterSlaveDsn(): void |
71
|
|
|
{ |
72
|
|
|
$context = ''; |
73
|
|
|
$this->bind()->annotatedWith('pdo_locator_dsn')->toInstance([$context => $this->dsn]); |
74
|
|
|
$this->bind()->annotatedWith('pdo_locator_user')->toInstance([$context => $this->user]); |
75
|
|
|
$this->bind()->annotatedWith('pdo_locator_pass')->toInstance([$context => $this->password]); |
76
|
|
|
$this->bind()->annotatedWith('pdo_locator_slave')->toInstance([$context => $this->slave]); |
77
|
|
|
$this->bind(ConnectionLocatorInterface::class)->annotatedWith($context)->toProvider(ConnectionLocatorProvider::class, $context); |
78
|
|
|
// ReadOnlyConnection when GET, otherwise WriteConnection |
79
|
|
|
$this->bind(ExtendedPdoInterface::class)->toProvider(AuraSqlReplicationDbProvider::class)->in(Scope::SINGLETON); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|