Skip to content

Commit 311b39b

Browse files
committed
winp
1 parent 70b206d commit 311b39b

File tree

5 files changed

+274
-5
lines changed

5 files changed

+274
-5
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"guzzlehttp/guzzle": "^7.9",
77
"psr/http-client": "^1.0",
88
"ext-json": "*",
9-
"php": "^8.1"
9+
"php": "^8.1",
10+
"nyholm/psr7": "^1.8"
1011
},
1112
"require-dev": {
1213
"phpunit/phpunit": "^11.0",
@@ -39,4 +40,4 @@
3940
"cs:fix": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
4041
}
4142

42-
}
43+
}

composer.lock

Lines changed: 79 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Neo4jRequestFactory.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI;
4+
5+
use InvalidArgumentException;
6+
7+
class Neo4jRequestFactory
8+
{
9+
private string $baseUri;
10+
private ?string $authHeader = null;
11+
12+
public function __construct(string $baseUri, ?string $authHeader = null)
13+
{
14+
$this->baseUri = $baseUri;
15+
$this->authHeader = $authHeader;
16+
}
17+
18+
/**
19+
* Builds a request for running a Cypher query.
20+
*/
21+
public function buildRunQueryRequest(
22+
string $database,
23+
string $cypher,
24+
array $parameters = [],
25+
bool $includeCounters = true,
26+
?array $bookmarks = null
27+
): array
28+
{
29+
$payload = [
30+
'statement' => $cypher,
31+
'parameters' => empty($parameters) ? new \stdClass() : $parameters,
32+
'includeCounters' => $includeCounters,
33+
];
34+
35+
if ($bookmarks !== null) {
36+
$payload['bookmarks'] = $bookmarks;
37+
}
38+
39+
$uri = rtrim($this->baseUri, '/') . "/db/{$database}/query/v2";
40+
41+
return $this->createRequest('POST', $uri, json_encode($payload));
42+
}
43+
44+
/**
45+
* Builds a request for starting a new transaction.
46+
*/
47+
public function buildBeginTransactionRequest(string $database): array
48+
{
49+
$uri = rtrim($this->baseUri, '/') . "/db/{$database}/query/v2/tx";
50+
51+
return $this->createRequest('POST', $uri);
52+
}
53+
54+
/**
55+
* Builds a request for committing a transaction.
56+
*/
57+
public function buildCommitRequest(string $database, string $transactionId): array
58+
{
59+
$uri = rtrim($this->baseUri, '/') . "/db/{$database}/query/v2/tx/{$transactionId}/commit";
60+
61+
return $this->createRequest('POST', $uri);
62+
}
63+
64+
/**
65+
* Builds a request for rolling back a transaction.
66+
*/
67+
public function buildRollbackRequest(string $database, string $transactionId): array
68+
{
69+
$uri = rtrim($this->baseUri, '/') . "/db/{$database}/query/v2/tx/{$transactionId}/rollback";
70+
71+
return $this->createRequest('POST', $uri);
72+
}
73+
74+
/**
75+
* Helper method to create a request manually.
76+
*/
77+
private function createRequest(string $method, string $uri, ?string $body = null): array
78+
{
79+
$headers = [
80+
'Content-Type' => 'application/json',
81+
'Accept' => 'application/json',
82+
];
83+
84+
if ($this->authHeader) {
85+
$headers['Authorization'] = $this->authHeader;
86+
}
87+
88+
return [
89+
'method' => $method,
90+
'uri' => $uri,
91+
'headers' => $headers,
92+
'body' => $body,
93+
];
94+
}
95+
}

src/requestFactory.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php'; // Autoload dependencies (assuming you're using Composer)
4+
5+
use Neo4j\QueryAPI\Neo4jRequestFactory;
6+
use GuzzleHttp\Client;
7+
8+
// Neo4j configuration
9+
$baseUri = "https://6f72daa1.databases.neo4j.io";
10+
$username = "neo4j";
11+
$password = "9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0";
12+
$authHeader = "Basic " . base64_encode("{$username}:{$password}");
13+
14+
// Initialize the Neo4jRequestFactory
15+
$requestFactory = new Neo4jRequestFactory($baseUri, $authHeader);
16+
17+
// Initialize Guzzle HTTP client
18+
$client = new Client();
19+
20+
// Database and Cypher query configuration
21+
$database = 'neo4j';
22+
$cypher = 'MATCH (n) RETURN n LIMIT 10';
23+
$parameters = []; // Optional query parameters
24+
25+
try {
26+
// Step 1: Start a new transaction
27+
$beginTxRequest = $requestFactory->buildBeginTransactionRequest($database);
28+
$beginTxResponse = $client->request(
29+
$beginTxRequest['method'],
30+
$beginTxRequest['uri'],
31+
[
32+
'headers' => $beginTxRequest['headers'],
33+
'body' => $beginTxRequest['body'] ?? null,
34+
]
35+
);
36+
37+
$beginTxData = json_decode($beginTxResponse->getBody()->getContents(), true);
38+
39+
// Extract the transaction ID
40+
$transactionId = $beginTxData['transaction']['id'] ?? null;
41+
if (!$transactionId) {
42+
throw new RuntimeException("Transaction ID not found in response.");
43+
}
44+
45+
echo "Transaction ID: {$transactionId}" . PHP_EOL;
46+
47+
// Step 2: Run the Cypher query within the transaction
48+
$runQueryRequest = $requestFactory->buildRunQueryRequest($database, $cypher, $parameters);
49+
$runQueryResponse = $client->request(
50+
$runQueryRequest['method'],
51+
$runQueryRequest['uri'],
52+
[
53+
'headers' => $runQueryRequest['headers'],
54+
'body' => $runQueryRequest['body'] ?? null,
55+
]
56+
);
57+
58+
$queryResults = json_decode($runQueryResponse->getBody()->getContents(), true);
59+
echo "Query Results: " . json_encode($queryResults, JSON_PRETTY_PRINT) . PHP_EOL;
60+
61+
// Step 3: Commit the transaction
62+
$commitRequest = $requestFactory->buildCommitRequest($database, $transactionId);
63+
$commitResponse = $client->request(
64+
$commitRequest['method'],
65+
$commitRequest['uri'],
66+
[
67+
'headers' => $commitRequest['headers'],
68+
'body' => $commitRequest['body'] ?? null,
69+
]
70+
);
71+
72+
echo "Transaction committed successfully!" . PHP_EOL;
73+
74+
// Optional: Output commit response
75+
echo "Commit Response: " . $commitResponse->getBody()->getContents() . PHP_EOL;
76+
77+
} catch (Exception $e) {
78+
echo "Error: " . $e->getMessage() . PHP_EOL;
79+
80+
// Rollback the transaction in case of failure
81+
if (isset($transactionId)) {
82+
$rollbackRequest = $requestFactory->buildRollbackRequest($database, $transactionId);
83+
$rollbackResponse = $client->request(
84+
$rollbackRequest['method'],
85+
$rollbackRequest['uri'],
86+
[
87+
'headers' => $rollbackRequest['headers'],
88+
'body' => $rollbackRequest['body'] ?? null,
89+
]
90+
);
91+
92+
echo "Transaction rolled back." . PHP_EOL;
93+
echo "Rollback Response: " . $rollbackResponse->getBody()->getContents() . PHP_EOL;
94+
}
95+
}

tests/Integration/Neo4jQueryAPIIntegrationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public function testChildQueryPlanExistence(): void
230230

231231

232232

233-
/* public function testTransactionCommit(): void
233+
public function testTransactionCommit(): void
234234
{
235235
// Begin a new transaction
236236
$tsx = $this->api->beginTransaction();
@@ -255,7 +255,7 @@ public function testChildQueryPlanExistence(): void
255255
// Validate that the node now exists in the database
256256
$results = $this->api->run("MATCH (x:Human {name: \$name}) RETURN x", ['name' => $name]);
257257
$this->assertCount(1, $results); // Updated to expect 1 result
258-
}*/
258+
}
259259

260260

261261
/**

0 commit comments

Comments
 (0)