forked from basemkhirat/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ConnectionManager.php
167 lines (143 loc) · 4.14 KB
/
ConnectionManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
declare(strict_types=1);
namespace Matchory\Elasticsearch;
use InvalidArgumentException;
use Matchory\Elasticsearch\Interfaces\ClientFactoryInterface;
use Matchory\Elasticsearch\Interfaces\ConnectionInterface;
use Matchory\Elasticsearch\Interfaces\ConnectionResolverInterface;
use Psr\SimpleCache\CacheInterface;
use function is_null;
/**
* Connection Manager
* ==================
* Resolver intended to manage connections to one or more Elasticsearch servers
* at runtime. It creates connections lazily as requested by the application.
*
* @package Matchory\Elasticsearch
*/
class ConnectionManager implements ConnectionResolverInterface
{
public const CONFIG_KEY_CONNECTIONS = 'connections';
public const CONFIG_KEY_DEFAULT_CONNECTION = 'default';
public const CONFIG_KEY_HANDLER = 'handler';
public const CONFIG_KEY_INDEX = 'index';
public const CONFIG_KEY_REPORT_QUERIES = 'report_queries';
public const CONFIG_KEY_SERVERS = 'servers';
/**
* All the registered connections.
*
* @var array<string, ConnectionInterface>
*/
protected array $connections = [];
/**
* Create a new connection resolver instance.
*
* @param array $configuration
* @param ClientFactoryInterface $clientFactory
* @param CacheInterface|null $cache
*/
public function __construct(
protected array $configuration,
protected readonly ClientFactoryInterface $clientFactory,
protected readonly CacheInterface|null $cache = null,
) {
}
/**
* Dynamically pass methods to the default connection.
*
* @param string $method
* @param array $parameters
*
* @return mixed
* @throws InvalidArgumentException
*/
public function __call(string $method, array $parameters)
{
return $this->connection()->$method(...$parameters);
}
/**
* Get a connection instance by name.
*
* @param string|null $name
*
* @return ConnectionInterface
* @throws InvalidArgumentException
*/
public function connection(string|null $name = null): ConnectionInterface
{
if (is_null($name)) {
$name = $this->getDefaultConnection();
}
if (!isset($this->connections[$name])) {
$this->connections[$name] = $this->makeConnection($name);
}
return $this->connections[$name];
}
/**
* Get the default connection name.
*
* @return string
*/
public function getDefaultConnection(): string
{
return $this->configuration[self::CONFIG_KEY_DEFAULT_CONNECTION] ?? '';
}
/**
* @param string $name
*
* @return ConnectionInterface
* @throws InvalidArgumentException
*/
protected function makeConnection(string $name): ConnectionInterface
{
$config = $this->configuration[self::CONFIG_KEY_CONNECTIONS][$name] ?? null;
if (!$config) {
throw new InvalidArgumentException(
"Elasticsearch connection [{$name}] not configured."
);
}
$client = $this->clientFactory->createClient($config);
return new Connection(
$client,
$this->cache,
$config[self::CONFIG_KEY_INDEX] ?? null,
$config[self::CONFIG_KEY_REPORT_QUERIES] ?? true,
);
}
/**
* Add a connection to the resolver.
*
* @param string $name
* @param ConnectionInterface $connection
*
* @return void
*/
public function addConnection(
string $name,
ConnectionInterface $connection
): void {
$this->connections[$name] = $connection;
}
/**
* Set the default connection name.
*
* @param string $name
*
* @return void
*/
public function setDefaultConnection(string $name): void
{
$this->configuration[self::CONFIG_KEY_DEFAULT_CONNECTION] = $name;
}
/**
* Check if a connection has been registered.
*
* @param string $name
*
* @return bool
*/
public function hasConnection(string $name): bool
{
return isset($this->connections[$name]);
}
}