7
7
use Neo4j \QueryAPI \Exception \Neo4jException ;
8
8
use Neo4j \QueryAPI \Objects \Authentication ;
9
9
use Neo4j \QueryAPI \Results \ResultSet ;
10
+ use Neo4j \QueryAPI \Configuration ;
10
11
use Nyholm \Psr7 \Factory \Psr17Factory ;
11
12
use Psr \Http \Client \ClientInterface ;
12
13
use Psr \Http \Client \RequestExceptionInterface ;
13
14
use Psr \Http \Message \ResponseInterface ;
14
15
15
16
class Neo4jQueryAPI
16
17
{
18
+ private Configuration $ config ;
19
+
17
20
public function __construct (
18
21
private ClientInterface $ client ,
19
22
private ResponseParser $ responseParser ,
20
- private Neo4jRequestFactory $ requestFactory
23
+ private Neo4jRequestFactory $ requestFactory ,
24
+ ?Configuration $ config = null
21
25
) {
26
+ $ this ->config = $ config ?? new Configuration (getenv ("NEO4J_ADDRESS " )); // Default configuration if not provided
22
27
}
23
28
24
29
/**
25
30
* @api
26
31
*/
27
- public static function login (string $ address , AuthenticateInterface $ auth = null ): self
32
+ public static function login (string $ address , ? AuthenticateInterface $ auth = null , ? Configuration $ config = null ): self
28
33
{
29
34
$ client = new Client ();
35
+ $ config = $ config ?? new Configuration (baseUri: $ address );
30
36
31
37
return new self (
32
38
client: $ client ,
33
- responseParser: new ResponseParser (
34
- ogm: new OGM ()
35
- ),
39
+ responseParser: new ResponseParser (new OGM ()),
36
40
requestFactory: new Neo4jRequestFactory (
37
41
psr17Factory: new Psr17Factory (),
38
42
streamFactory: new Psr17Factory (),
39
- configuration: new Configuration (
40
- baseUri: $ address
41
- ),
43
+ configuration: $ config ,
42
44
auth: $ auth ?? Authentication::fromEnvironment ()
43
- )
45
+ ),
46
+ config: $ config
44
47
);
45
48
}
46
49
50
+ public function getConfig (): Configuration
51
+ {
52
+ return $ this ->config ;
53
+ }
54
+
47
55
/**
48
56
* Executes a Cypher query.
49
57
*/
@@ -59,7 +67,6 @@ public function run(string $cypher, array $parameters = []): ResultSet
59
67
60
68
return $ this ->responseParser ->parseRunQueryResponse ($ response );
61
69
}
62
-
63
70
/**
64
71
* Starts a transaction.
65
72
*/
@@ -86,6 +93,7 @@ public function beginTransaction(): Transaction
86
93
);
87
94
}
88
95
96
+
89
97
/**
90
98
* Handles request exceptions by parsing error details and throwing a Neo4jException.
91
99
*
@@ -95,7 +103,7 @@ private function handleRequestException(RequestExceptionInterface $e): void
95
103
{
96
104
$ response = $ e ->getResponse ();
97
105
if ($ response instanceof ResponseInterface) {
98
- $ errorResponse = json_decode ((string )$ response ->getBody (), true );
106
+ $ errorResponse = json_decode ((string ) $ response ->getBody (), true );
99
107
throw Neo4jException::fromNeo4jResponse ($ errorResponse , $ e );
100
108
}
101
109
0 commit comments