|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Connection\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
|
6
|
|
|
use Bdf\Prime\Sharding\ShardingConnection; |
|
7
|
|
|
use Doctrine\DBAL\Configuration; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* ShardingConnection |
|
11
|
|
|
*/ |
|
12
|
|
|
class ShardingConnectionFactory implements ConnectionFactoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The delegated loader |
|
16
|
|
|
* |
|
17
|
|
|
* @var ConnectionFactoryInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $connectionFactory; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Set default configuration |
|
23
|
|
|
* |
|
24
|
|
|
* @param ConnectionFactoryInterface $connectionFactory |
|
25
|
|
|
*/ |
|
26
|
123 |
|
public function __construct(ConnectionFactoryInterface $connectionFactory) |
|
27
|
|
|
{ |
|
28
|
123 |
|
$this->connectionFactory = $connectionFactory; |
|
29
|
123 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
|
|
|
|
|
32
|
|
|
* {@inheritDoc} |
|
33
|
|
|
*/ |
|
34
|
73 |
|
public function create(string $connectionName, array $parameters, Configuration $config): ConnectionInterface |
|
35
|
|
|
{ |
|
36
|
73 |
|
$allParameters = $parameters['shards']; |
|
37
|
73 |
|
unset($parameters['shards']); |
|
38
|
|
|
|
|
39
|
73 |
|
$globalParameters = $parameters; |
|
40
|
73 |
|
unset($globalParameters['wrapperClass']); |
|
41
|
73 |
|
unset($globalParameters['distributionKey']); |
|
42
|
73 |
|
unset($globalParameters['shardChoser']); |
|
43
|
|
|
|
|
44
|
73 |
|
$parameters['shard_connections'] = []; |
|
45
|
73 |
|
foreach ($allParameters as $shardId => $shardParameters) { |
|
46
|
73 |
|
$parameters['shard_connections'][$shardId] = $this->connectionFactory->create($connectionName.'.'.$shardId, array_merge($globalParameters, $shardParameters), $config); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
73 |
|
$parameters['wrapperClass'] = $parameters['wrapperClass'] ?? ShardingConnection::class; |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
73 |
|
return $this->connectionFactory->create($connectionName, $parameters, $config); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
|
|
|
|
|
55
|
|
|
* {@inheritDoc} |
|
56
|
|
|
*/ |
|
57
|
77 |
|
public function support(string $connectionName, array $parameters): bool |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
77 |
|
return isset($parameters['shards']); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|