|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Connection\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
|
6
|
|
|
use Bdf\Prime\Connection\MasterSlaveConnection; |
|
7
|
|
|
use Doctrine\DBAL\Configuration; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* MasterSlaveConnectionLoader |
|
11
|
|
|
*/ |
|
12
|
|
|
class MasterSlaveConnectionFactory implements ConnectionFactoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The delegated connectionFactory |
|
16
|
|
|
* |
|
17
|
|
|
* @var ConnectionFactoryInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $connectionFactory; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Set default configuration |
|
23
|
|
|
* |
|
24
|
|
|
* @param ConnectionFactoryInterface $connectionFactory |
|
25
|
|
|
*/ |
|
26
|
80 |
|
public function __construct(ConnectionFactoryInterface $connectionFactory) |
|
27
|
|
|
{ |
|
28
|
80 |
|
$this->connectionFactory = $connectionFactory; |
|
29
|
80 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
|
|
|
|
|
32
|
|
|
* {@inheritDoc} |
|
33
|
|
|
*/ |
|
34
|
8 |
|
public function create(string $connectionName, array $parameters, Configuration $config): ConnectionInterface |
|
35
|
|
|
{ |
|
36
|
8 |
|
$masterParameters = $parameters; |
|
37
|
8 |
|
unset($masterParameters['read']); |
|
38
|
8 |
|
unset($masterParameters['wrapperClass']); |
|
39
|
8 |
|
$readParameters = array_merge($masterParameters, $parameters['read']); |
|
40
|
|
|
|
|
41
|
8 |
|
$parameters['read'] = $this->connectionFactory->create($connectionName.'.read', $readParameters, $config); |
|
42
|
8 |
|
$parameters['wrapperClass'] = $parameters['wrapperClass'] ?? MasterSlaveConnection::class; |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
8 |
|
return $this->connectionFactory->create($connectionName, $parameters, $config); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
|
|
|
|
|
48
|
|
|
* {@inheritDoc} |
|
49
|
|
|
*/ |
|
50
|
77 |
|
public function support(string $connectionName, array $parameters): bool |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
77 |
|
return isset($parameters['read']); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|