17
17
use function in_array ;
18
18
use Laudis \Neo4j \Authentication \Authenticate ;
19
19
use Laudis \Neo4j \Bolt \BoltConfiguration ;
20
+ use Laudis \Neo4j \Common \DriverSetupManager ;
20
21
use Laudis \Neo4j \Common \Uri ;
21
22
use Laudis \Neo4j \Contracts \AuthenticateInterface ;
22
23
use Laudis \Neo4j \Contracts \ClientInterface ;
28
29
use Laudis \Neo4j \Databags \SummarizedResult ;
29
30
use Laudis \Neo4j \Databags \TransactionConfiguration ;
30
31
use Laudis \Neo4j \Exception \UnsupportedScheme ;
32
+ use Laudis \Neo4j \Formatter \OGMFormatter ;
31
33
use Laudis \Neo4j \Formatter \SummarizedResultFormatter ;
32
34
use Laudis \Neo4j \Http \HttpConfig ;
33
35
use Laudis \Neo4j \Types \CypherMap ;
37
39
*
38
40
* @template T
39
41
*
40
- * @psalm-import-type OGMTypes from \Laudis\Neo4j\Formatter\ OGMFormatter
42
+ * @psalm-import-type OGMTypes from OGMFormatter
41
43
*/
42
44
final class ClientBuilder
43
45
{
44
46
public const SUPPORTED_SCHEMES = ['' , 'bolt ' , 'bolt+s ' , 'bolt+ssc ' , 'neo4j ' , 'neo4j+s ' , 'neo4j+ssc ' , 'http ' , 'https ' ];
45
47
46
- /**
47
- * @psalm-readonly
48
- *
49
- * @var array<string, DriverSetup>
50
- */
51
- private array $ driverConfigurations ;
52
48
/** @psalm-readonly */
53
49
private DriverConfiguration $ defaultDriverConfig ;
54
50
/** @psalm-readonly */
55
51
private TransactionConfiguration $ defaultTransactionConfig ;
56
52
/** @psalm-readonly */
57
53
private SessionConfiguration $ defaultSessionConfig ;
58
- /** @psalm-readonly */
59
- private ?string $ defaultDriver ;
60
- /** @psalm-readonly */
61
- private FormatterInterface $ formatter ;
54
+ /** @var DriverSetupManager<T> */
55
+ private DriverSetupManager $ driverSetups ;
62
56
63
57
/**
64
58
* @psalm-mutation-free
65
59
*
66
- * @param array<string, DriverSetup> $driverConfigurations
67
- * @param FormatterInterface<T> $formatter
60
+ * @param DriverSetupManager<T> $driverSetups
68
61
*/
69
- public function __construct (DriverConfiguration $ configuration , SessionConfiguration $ sessionConfiguration , TransactionConfiguration $ transactionConfiguration , FormatterInterface $ formatter , array $ driverConfigurations , ? string $ defaultDriver )
62
+ public function __construct (DriverConfiguration $ configuration , SessionConfiguration $ sessionConfiguration , TransactionConfiguration $ transactionConfiguration , DriverSetupManager $ driverSetups )
70
63
{
71
- $ this ->driverConfigurations = $ driverConfigurations ;
72
64
$ this ->defaultDriverConfig = $ configuration ;
73
- $ this ->defaultDriver = $ defaultDriver ;
74
- $ this ->formatter = $ formatter ;
75
65
$ this ->defaultSessionConfig = $ sessionConfiguration ;
76
66
$ this ->defaultTransactionConfig = $ transactionConfiguration ;
67
+ $ this ->driverSetups = $ driverSetups ;
77
68
}
78
69
79
70
/**
@@ -89,9 +80,7 @@ public static function create(): ClientBuilder
89
80
DriverConfiguration::default (),
90
81
SessionConfiguration::default (),
91
82
TransactionConfiguration::default (),
92
- SummarizedResultFormatter::create (),
93
- [],
94
- null
83
+ new DriverSetupManager (SummarizedResultFormatter::create (), DriverConfiguration::default ())
95
84
);
96
85
}
97
86
@@ -100,39 +89,32 @@ public static function create(): ClientBuilder
100
89
*
101
90
* @return self<T>
102
91
*/
103
- public function withDriver (string $ alias , string $ url , ?AuthenticateInterface $ authentication = null ): self
92
+ public function withDriver (string $ alias , string $ url , ?AuthenticateInterface $ authentication = null , ? int $ priority = 0 ): self
104
93
{
105
94
$ uri = Uri::create ($ url );
106
95
107
96
$ authentication ??= Authenticate::fromUrl ($ uri );
108
97
109
- return $ this ->withParsedUrl ($ alias , $ uri , $ authentication );
98
+ return $ this ->withParsedUrl ($ alias , $ uri , $ authentication, $ priority ?? 0 );
110
99
}
111
100
112
101
/**
113
- * @psalm-mutation-free
102
+ * @psalm-external- mutation-free
114
103
*
115
104
* @return self<T>
116
105
*/
117
- private function withParsedUrl (string $ alias , Uri $ uri , AuthenticateInterface $ authentication ): self
106
+ private function withParsedUrl (string $ alias , Uri $ uri , AuthenticateInterface $ authentication, int $ priority ): self
118
107
{
119
108
$ scheme = $ uri ->getScheme ();
120
109
121
110
if (!in_array ($ scheme , self ::SUPPORTED_SCHEMES , true )) {
122
111
throw UnsupportedScheme::make ($ scheme , self ::SUPPORTED_SCHEMES );
123
112
}
124
113
125
- $ setup = new DriverSetup ( $ uri , $ authentication ) ;
126
- $ configs = array_merge ( $ this ->driverConfigurations , [ $ alias => $ setup ] );
114
+ $ tbr = clone $ this ;
115
+ $ tbr -> driverSetups = $ this ->driverSetups -> withSetup ( new DriverSetup ( $ uri , $ authentication ), $ alias, $ priority );
127
116
128
- return new self (
129
- $ this ->defaultDriverConfig ,
130
- $ this ->defaultSessionConfig ,
131
- $ this ->defaultTransactionConfig ,
132
- $ this ->formatter ,
133
- $ configs ,
134
- $ this ->defaultDriver
135
- );
117
+ return $ tbr ;
136
118
}
137
119
138
120
/**
@@ -171,7 +153,7 @@ public function addBoltConnection(string $alias, string $url, BoltConfiguration
171
153
$ parsedUrl = $ parsedUrl ->withScheme ('bolt ' .$ postScheme );
172
154
}
173
155
174
- return $ this ->withParsedUrl ($ alias , $ parsedUrl , Authenticate::fromUrl ($ parsedUrl ));
156
+ return $ this ->withParsedUrl ($ alias , $ parsedUrl , Authenticate::fromUrl ($ parsedUrl ), 0 );
175
157
}
176
158
177
159
/**
@@ -196,16 +178,10 @@ public function addHttpConnection(string $alias, string $url, HttpConfig $config
196
178
$ uri = $ uri ->withScheme ($ scheme === '' ? 'http ' : $ scheme );
197
179
$ uri = $ uri ->withPort ($ uri ->getPort () === 7687 ? 7474 : $ uri ->getPort ());
198
180
199
- $ self = new self (
200
- $ this ->defaultDriverConfig ->withHttpPsrBindings ($ bindings ),
201
- $ this ->defaultSessionConfig ,
202
- $ this ->defaultTransactionConfig ,
203
- $ this ->formatter ,
204
- $ this ->driverConfigurations ,
205
- $ this ->defaultDriver
206
- );
181
+ $ tbr = clone $ this ;
182
+ $ tbr ->defaultDriverConfig = $ this ->defaultDriverConfig ->withHttpPsrBindings ($ bindings );
207
183
208
- return $ self ->withParsedUrl ($ alias , $ uri , Authenticate::fromUrl ($ uri ));
184
+ return $ tbr ->withParsedUrl ($ alias , $ uri , Authenticate::fromUrl ($ uri ), 0 );
209
185
}
210
186
211
187
/**
@@ -230,14 +206,10 @@ public function setDefaultConnection(string $alias): self
230
206
*/
231
207
public function withDefaultDriver (string $ alias ): self
232
208
{
233
- return new self (
234
- $ this ->defaultDriverConfig ,
235
- $ this ->defaultSessionConfig ,
236
- $ this ->defaultTransactionConfig ,
237
- $ this ->formatter ,
238
- $ this ->driverConfigurations ,
239
- $ alias
240
- );
209
+ $ tbr = clone $ this ;
210
+ $ tbr ->driverSetups = $ this ->driverSetups ->withDefault ($ alias );
211
+
212
+ return $ tbr ;
241
213
}
242
214
243
215
/**
@@ -254,9 +226,7 @@ public function withFormatter(FormatterInterface $formatter): self
254
226
$ this ->defaultDriverConfig ,
255
227
$ this ->defaultSessionConfig ,
256
228
$ this ->defaultTransactionConfig ,
257
- $ formatter ,
258
- $ this ->driverConfigurations ,
259
- $ this ->defaultDriver
229
+ $ this ->driverSetups ->withFormatter ($ formatter )
260
230
);
261
231
}
262
232
@@ -268,12 +238,9 @@ public function withFormatter(FormatterInterface $formatter): self
268
238
public function build (): ClientInterface
269
239
{
270
240
return new Client (
271
- $ this ->driverConfigurations ,
272
- $ this ->defaultDriverConfig ,
241
+ $ this ->driverSetups ,
273
242
$ this ->defaultSessionConfig ,
274
243
$ this ->defaultTransactionConfig ,
275
- $ this ->formatter ,
276
- $ this ->defaultDriver
277
244
);
278
245
}
279
246
@@ -287,14 +254,10 @@ public function withHttpPsrBindings(HttpPsrBindings $bindings): self
287
254
{
288
255
$ config = $ this ->defaultDriverConfig ->withHttpPsrBindings ($ bindings );
289
256
290
- return new self (
291
- $ config ,
292
- $ this ->defaultSessionConfig ,
293
- $ this ->defaultTransactionConfig ,
294
- $ this ->formatter ,
295
- $ this ->driverConfigurations ,
296
- $ this ->defaultDriver
297
- );
257
+ $ tbr = clone $ this ;
258
+ $ tbr ->defaultDriverConfig = $ config ;
259
+
260
+ return $ tbr ;
298
261
}
299
262
300
263
/**
@@ -304,14 +267,10 @@ public function withHttpPsrBindings(HttpPsrBindings $bindings): self
304
267
*/
305
268
public function withDefaultDriverConfiguration (DriverConfiguration $ config ): self
306
269
{
307
- return new self (
308
- $ config ,
309
- $ this ->defaultSessionConfig ,
310
- $ this ->defaultTransactionConfig ,
311
- $ this ->formatter ,
312
- $ this ->driverConfigurations ,
313
- $ this ->defaultDriver
314
- );
270
+ $ tbr = clone $ this ;
271
+ $ tbr ->defaultDriverConfig = $ config ;
272
+
273
+ return $ tbr ;
315
274
}
316
275
317
276
/**
@@ -321,14 +280,10 @@ public function withDefaultDriverConfiguration(DriverConfiguration $config): sel
321
280
*/
322
281
public function withDefaultSessionConfiguration (SessionConfiguration $ config ): self
323
282
{
324
- return new self (
325
- $ this ->defaultDriverConfig ,
326
- $ config ,
327
- $ this ->defaultTransactionConfig ,
328
- $ this ->formatter ,
329
- $ this ->driverConfigurations ,
330
- $ this ->defaultDriver
331
- );
283
+ $ tbr = clone $ this ;
284
+ $ tbr ->defaultSessionConfig = $ config ;
285
+
286
+ return $ tbr ;
332
287
}
333
288
334
289
/**
@@ -338,13 +293,9 @@ public function withDefaultSessionConfiguration(SessionConfiguration $config): s
338
293
*/
339
294
public function withDefaultTransactionConfiguration (TransactionConfiguration $ config ): self
340
295
{
341
- return new self (
342
- $ this ->defaultDriverConfig ,
343
- $ this ->defaultSessionConfig ,
344
- $ config ,
345
- $ this ->formatter ,
346
- $ this ->driverConfigurations ,
347
- $ this ->defaultDriver
348
- );
296
+ $ tbr = clone $ this ;
297
+ $ tbr ->defaultTransactionConfig = $ config ;
298
+
299
+ return $ tbr ;
349
300
}
350
301
}
0 commit comments