forked from matchory/elasticsearch
/
ConnectionManager.php
172 lines (149 loc) · 4.32 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
168
169
170
171
172
<?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\Log\LoggerInterface;
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_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
* @param LoggerInterface|null $logger
*/
public function __construct(
private array $configuration,
private readonly ClientFactoryInterface $clientFactory,
private readonly CacheInterface|null $cache = null,
private readonly LoggerInterface|null $logger = 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);
}
/**
* 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;
}
/**
* 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] ?? '';
}
/**
* 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]);
}
/**
* @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[self::CONFIG_KEY_SERVERS],
$this->logger,
$config[self::CONFIG_KEY_HANDLER] ?? null
);
return new Connection(
$client,
$this->cache,
$config[self::CONFIG_KEY_INDEX] ?? null
);
}
}