|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Facile\MongoDbBundle\Services; |
|
6
|
|
|
|
|
7
|
|
|
use Facile\MongoDbBundle\Capsule\Client as LoggerClient; |
|
8
|
|
|
use Facile\MongoDbBundle\Models\ClientConfiguration; |
|
9
|
|
|
use Facile\MongoDbBundle\Services\Loggers\DataCollectorLoggerInterface; |
|
10
|
|
|
use MongoDB\Client; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class ClientRegistry. |
|
14
|
|
|
*/ |
|
15
|
|
|
class ClientRegistry |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var Client[] */ |
|
18
|
|
|
private $clients; |
|
19
|
|
|
/** @var ClientConfiguration[] */ |
|
20
|
|
|
private $configurations; |
|
21
|
|
|
/** @var DataCollectorLoggerInterface */ |
|
22
|
|
|
private $logger; |
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
private $environment; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* ClientRegistry constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param DataCollectorLoggerInterface $logger |
|
30
|
|
|
* @param string $environment |
|
31
|
|
|
*/ |
|
32
|
7 |
|
public function __construct(DataCollectorLoggerInterface $logger, string $environment) |
|
33
|
|
|
{ |
|
34
|
7 |
|
$this->clients = []; |
|
35
|
7 |
|
$this->configurations = []; |
|
36
|
7 |
|
$this->logger = $logger; |
|
37
|
7 |
|
$this->environment = $environment; |
|
38
|
7 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param array $configurations |
|
42
|
|
|
*/ |
|
43
|
7 |
|
public function addClientsConfigurations(array $configurations) |
|
44
|
|
|
{ |
|
45
|
7 |
|
foreach ($configurations as $name => $conf) { |
|
46
|
7 |
|
$this->addClientConfiguration($name, $conf); |
|
47
|
|
|
} |
|
48
|
7 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $name |
|
52
|
|
|
* @param string $databaseName |
|
53
|
|
|
* |
|
54
|
|
|
* @return Client |
|
55
|
|
|
*/ |
|
56
|
7 |
|
public function getClientForDatabase(string $name, string $databaseName): Client |
|
57
|
|
|
{ |
|
58
|
7 |
|
return $this->getClient($name, $databaseName); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $name |
|
63
|
|
|
* @param string $databaseName |
|
64
|
|
|
* |
|
65
|
|
|
* @return Client |
|
66
|
|
|
*/ |
|
67
|
7 |
|
public function getClient(string $name, string $databaseName = null): Client |
|
68
|
|
|
{ |
|
69
|
7 |
|
$clientKey = !is_null($databaseName) ? $name.'.'.$databaseName : $name; |
|
70
|
|
|
|
|
71
|
7 |
|
if (!isset($this->clients[$clientKey])) { |
|
72
|
7 |
|
$conf = $this->configurations[$name]; |
|
73
|
7 |
|
$uri = sprintf('mongodb://%s:%d', $conf->getHost(), $conf->getPort()); |
|
74
|
7 |
|
$options = array_merge(['database' => $databaseName], $conf->getOptions()); |
|
75
|
7 |
|
$this->clients[$clientKey] = $this->buildClient($uri, $options, []); |
|
76
|
7 |
|
$this->logger->addConnection($clientKey); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
7 |
|
return $this->clients[$clientKey]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $name |
|
84
|
|
|
* @param array $conf |
|
85
|
|
|
*/ |
|
86
|
7 |
|
private function addClientConfiguration(string $name, array $conf) |
|
87
|
|
|
{ |
|
88
|
7 |
|
$this->configurations[$name] = $this->buildClientConfiguration($conf); |
|
89
|
7 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param $uri |
|
93
|
|
|
* @param array $options |
|
94
|
|
|
* @param array $driverOptions |
|
95
|
|
|
* |
|
96
|
|
|
* @return Client |
|
97
|
|
|
*/ |
|
98
|
7 |
|
private function buildClient($uri, array $options, array $driverOptions): Client |
|
99
|
|
|
{ |
|
100
|
7 |
|
if ('dev' === $this->environment) { |
|
101
|
2 |
|
return new LoggerClient($uri, $options, $driverOptions, $this->logger); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
5 |
|
return new Client($uri, $options, $driverOptions); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param array $conf |
|
109
|
|
|
* |
|
110
|
|
|
* @return ClientConfiguration |
|
111
|
|
|
*/ |
|
112
|
7 |
|
private function buildClientConfiguration(array $conf): ClientConfiguration |
|
113
|
|
|
{ |
|
114
|
7 |
|
return new ClientConfiguration( |
|
115
|
7 |
|
$conf['host'], |
|
116
|
7 |
|
$conf['port'], |
|
117
|
7 |
|
$conf['username'], |
|
118
|
7 |
|
$conf['password'], |
|
119
|
|
|
[ |
|
120
|
7 |
|
'replicaSet' => $conf['replicaSet'], |
|
121
|
7 |
|
'ssl' => $conf['ssl'], |
|
122
|
7 |
|
'connectTimeoutMS' => $conf['connectTimeoutMS'], |
|
123
|
|
|
] |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|