-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathDriverSetupManager.php
More file actions
215 lines (172 loc) · 5.76 KB
/
Copy pathDriverSetupManager.php
File metadata and controls
215 lines (172 loc) · 5.76 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
declare(strict_types=1);
/*
* This file is part of the Neo4j PHP Client and Driver package.
*
* (c) Nagels <https://nagels.tech>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Laudis\Neo4j\Common;
use function array_key_exists;
use function array_key_first;
use function array_reduce;
use Bolt\error\ConnectException;
use Countable;
use InvalidArgumentException;
use Laudis\Neo4j\Authentication\Authenticate;
use Laudis\Neo4j\Contracts\DriverInterface;
use Laudis\Neo4j\Databags\DriverConfiguration;
use Laudis\Neo4j\Databags\DriverSetup;
use Laudis\Neo4j\Databags\SessionConfiguration;
use Laudis\Neo4j\DriverFactory;
use Laudis\Neo4j\Formatter\SummarizedResultFormatter;
use const PHP_INT_MIN;
use Psr\Log\LogLevel;
use RuntimeException;
use SplPriorityQueue;
use function sprintf;
class DriverSetupManager implements Countable
{
private const DEFAULT_DRIVER_CONFIG = 'bolt://localhost:7687';
/** @var array<string, SplPriorityQueue<int, DriverSetup>> */
private array $driverSetups = [];
/** @var array<string, DriverInterface> */
private array $drivers = [];
private ?string $default = null;
/**
* @psalm-mutation-free
*/
public function __construct(
private SummarizedResultFormatter $formatter,
private DriverConfiguration $configuration,
) {
}
public function getDriverConfiguration(): DriverConfiguration
{
return $this->configuration;
}
/**
* @psalm-mutation-free
*/
public function withDriverConfiguration(DriverConfiguration $config): self
{
$tbr = clone $this;
$tbr->configuration = $config;
return $tbr;
}
/**
* @psalm-mutation-free
*/
public function withSetup(DriverSetup $setup, ?string $alias = null, ?int $priority = 0): self
{
$alias ??= $this->decideAlias($alias);
$setups = $this->driverSetups;
/** @var SplPriorityQueue<int, DriverSetup> $splPriorityQueue */
$splPriorityQueue = new SplPriorityQueue();
$setups[$alias] ??= $splPriorityQueue;
/** @psalm-suppress ImpureMethodCall */
$setups[$alias]->insert($setup, $priority ?? 0);
$tbr = clone $this;
$tbr->driverSetups = $setups;
return $tbr;
}
public function hasDriver(string $alias): bool
{
return array_key_exists($alias, $this->driverSetups);
}
public function getDriver(SessionConfiguration $config, ?string $alias = null): DriverInterface
{
$alias ??= $this->decideAlias($alias);
if (!array_key_exists($alias, $this->driverSetups)) {
if ($alias !== 'default') {
throw new InvalidArgumentException(sprintf('Cannot find a driver setup with alias: "%s"', $alias));
}
/** @var SplPriorityQueue<int, DriverSetup> */
$this->driverSetups['default'] = new SplPriorityQueue();
$setup = new DriverSetup(Uri::create(self::DEFAULT_DRIVER_CONFIG), Authenticate::disabled($config->getLogger()));
$this->driverSetups['default']->insert($setup, PHP_INT_MIN);
return $this->getDriver($config);
}
if (array_key_exists($alias, $this->drivers)) {
return $this->drivers[$alias];
}
$urisTried = [];
foreach ($this->driverSetups[$alias] as $setup) {
$uri = $setup->getUri();
$auth = $setup->getAuth();
$driver = DriverFactory::create($uri, $this->configuration, $auth, $this->formatter);
$urisTried[] = $uri->__toString();
if ($driver->verifyConnectivity($config)) {
$this->drivers[$alias] = $driver;
return $driver;
}
}
throw new RuntimeException(sprintf('Cannot connect to any server on alias: %s with Uris: (\'%s\')', $alias, implode('\', ', array_unique($urisTried))));
}
/**
* Resets the driver for the given alias, so that it will be recreated on the next call to getDriver, verifying the connection again.
*/
public function resetDriver(?string $alias): void
{
$alias ??= $this->decideAlias($alias);
unset($this->drivers[$alias]);
}
public function verifyConnectivity(SessionConfiguration $config, ?string $alias = null): bool
{
try {
$this->getDriver($config, $alias);
} catch (ConnectException $e) {
$this->getLogger()?->log(
LogLevel::WARNING,
sprintf('Could not connect to server using alias (%s)', $alias ?? '<default>'),
['exception' => $e]
);
return false;
}
return true;
}
/**
* @psalm-mutation-free
*/
private function decideAlias(?string $alias): string
{
return $alias ?? $this->default ?? array_key_first($this->driverSetups) ?? 'default';
}
public function getDefaultAlias(): string
{
return $this->decideAlias(null);
}
/**
* @psalm-mutation-free
*/
public function withDefault(string $default): self
{
$tbr = clone $this;
$tbr->default = $default;
return $tbr;
}
public function count(): int
{
return array_reduce($this->driverSetups, static fn (int $acc, SplPriorityQueue $x) => $acc + $x->count(), 0);
}
/**
* @template U
*
* @psalm-mutation-free
*/
public function withFormatter(SummarizedResultFormatter $formatter): self
{
$tbr = clone $this;
$tbr->formatter = $formatter;
return $tbr;
}
/**
* @psalm-mutation-free
*/
public function getLogger(): ?Neo4jLogger
{
return $this->configuration->getLogger();
}
}