1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ray\AuraSqlModule; |
6
|
|
|
|
7
|
|
|
use Aura\Sql\ConnectionLocatorInterface; |
8
|
|
|
use Ray\Di\Di\Named; |
9
|
|
|
use Ray\Di\ProviderInterface; |
10
|
|
|
use Ray\Di\SetContextInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @implements ProviderInterface<ConnectionLocatorInterface> |
14
|
|
|
*/ |
15
|
|
|
final class ConnectionLocatorProvider implements ProviderInterface, SetContextInterface |
16
|
|
|
{ |
17
|
|
|
/** @var array<string> */ |
18
|
|
|
private array $dsn; |
19
|
|
|
|
20
|
|
|
/** @var array<string> */ |
21
|
|
|
private array $user; |
22
|
|
|
|
23
|
|
|
/** @var array<string> */ |
24
|
|
|
private array $password; |
25
|
|
|
|
26
|
|
|
/** @var array<string> */ |
27
|
|
|
private array $slave; |
28
|
|
|
private string $context; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritDoc} |
32
|
|
|
*/ |
33
|
|
|
public function setContext($context) |
34
|
|
|
{ |
35
|
|
|
$this->context = $context; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array<string> $dsn |
40
|
|
|
* @param array<string> $user |
41
|
|
|
* @param array<string> $password |
42
|
|
|
* @param array<string> $slave |
43
|
|
|
* |
44
|
|
|
* @Named("dsn=pdo_locator_dsn,user=pdo_locator_user,password=pdo_locator_pass,slave=pdo_locator_slave") |
45
|
|
|
*/ |
46
|
|
|
#[Named('dsn=pdo_locator_dsn,user=pdo_locator_user,password=pdo_locator_pass,slave=pdo_locator_slave')] |
47
|
|
|
public function __construct(array $dsn, array $user, array $password, array $slave) |
48
|
|
|
{ |
49
|
|
|
$this->dsn = $dsn; |
50
|
|
|
$this->user = $user; |
51
|
|
|
$this->password = $password; |
52
|
|
|
$this->slave = $slave; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function get(): ConnectionLocatorInterface |
56
|
|
|
{ |
57
|
|
|
return ConnectionLocatorFactory::newInstance( |
|
|
|
|
58
|
|
|
$this->dsn[$this->context], |
59
|
|
|
$this->user[$this->context], |
60
|
|
|
$this->password[$this->context], |
61
|
|
|
$this->slave[$this->context] |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: