Skip to content

Commit deeed45

Browse files
committed
feat: Add scheme for future scope
1 parent c7b2f50 commit deeed45

File tree

9 files changed

+83
-27
lines changed

9 files changed

+83
-27
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM php:8.3-cli
1+
FROM php:8.1-cli
22
RUN apt-get update \
33
&& apt-get install -y \
44
libzip-dev \

src/Collector/Neo4jDataCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private function recursiveToArray(mixed $obj): mixed
134134
{
135135
if (is_array($obj)) {
136136
return array_map(
137-
fn(mixed $x) => $this->recursiveToArray($x),
137+
fn (mixed $x) => $this->recursiveToArray($x),
138138
$obj
139139
);
140140
}

src/Event/FailureEvent.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,21 @@ class FailureEvent extends Event
1414

1515
protected bool $shouldThrowException = true;
1616

17-
public function __construct(private ?string $alias, private Statement $statement, private Neo4jException $exception, private \DateTimeInterface $time)
18-
{
17+
public function __construct(
18+
private readonly ?string $alias,
19+
private readonly Statement $statement,
20+
private readonly Neo4jException $exception,
21+
private readonly \DateTimeInterface $time,
22+
private readonly ?string $scheme
23+
) {
1924
}
2025

2126
public function getException(): Neo4jException
2227
{
2328
return $this->exception;
2429
}
2530

31+
/** @api */
2632
public function disableException(): void
2733
{
2834
$this->shouldThrowException = false;
@@ -47,4 +53,9 @@ public function getStatement(): Statement
4753
{
4854
return $this->statement;
4955
}
56+
57+
public function getScheme(): ?string
58+
{
59+
return $this->scheme;
60+
}
5061
}

src/Event/PostRunEvent.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class PostRunEvent extends Event
1414
public function __construct(
1515
private readonly ?string $alias,
1616
private readonly ResultSummary $result,
17-
private readonly \DateTimeInterface $time
17+
private readonly \DateTimeInterface $time,
18+
private readonly ?string $scheme
1819
) {
1920
}
2021

@@ -32,4 +33,9 @@ public function getAlias(): ?string
3233
{
3334
return $this->alias;
3435
}
36+
37+
public function getScheme(): ?string
38+
{
39+
return $this->scheme;
40+
}
3541
}

src/Event/PreRunEvent.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ class PreRunEvent extends Event
1111
{
1212
public const EVENT_ID = 'neo4j.pre_run';
1313

14-
public function __construct(private ?string $alias, private Statement $statement, private \DateTimeInterface $time)
15-
{
14+
public function __construct(
15+
private readonly ?string $alias,
16+
private readonly Statement $statement,
17+
private readonly \DateTimeInterface $time,
18+
private readonly ?string $scheme
19+
) {
1620
}
1721

22+
/** @api */
1823
public function getStatement(): Statement
1924
{
2025
return $this->statement;
@@ -29,4 +34,9 @@ public function getAlias(): ?string
2934
{
3035
return $this->alias;
3136
}
37+
38+
public function getScheme(): ?string
39+
{
40+
return $this->scheme;
41+
}
3242
}

src/EventHandler.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,34 @@ public function __construct(
4040
*
4141
* @return SummarizedResult<T>
4242
*/
43-
public function handle(callable $runHandler, Statement $statement, ?string $alias): SummarizedResult
43+
public function handle(callable $runHandler, Statement $statement, ?string $alias, ?string $scheme): SummarizedResult
4444
{
45+
$stopWatchName = sprintf('neo4j.%s.query', $alias ?? $this->alias);
4546
if (null === $this->dispatcher) {
46-
return $runHandler($statement);
47+
$this->stopwatch?->start($stopWatchName);
48+
$result = $runHandler($statement);
49+
$this->stopwatch?->stop($stopWatchName);
50+
51+
return $result;
4752
}
4853

4954
/** @noinspection PhpUnhandledExceptionInspection */
5055
$time = new \DateTimeImmutable('now', new \DateTimeZone(date_default_timezone_get()));
51-
$this->dispatcher->dispatch(new PreRunEvent($alias, $statement, $time), PreRunEvent::EVENT_ID);
56+
$this->dispatcher->dispatch(new PreRunEvent($alias, $statement, $time, $scheme), PreRunEvent::EVENT_ID);
5257

53-
$stopWatchName = sprintf('neo4j.%s.query', $alias ?? $this->alias);
5458
try {
5559
$this->stopwatch?->start($stopWatchName);
5660
$tbr = $runHandler($statement);
5761
$this->stopwatch?->stop($stopWatchName);
5862
$this->dispatcher->dispatch(
59-
new PostRunEvent($alias ?? $this->alias, $tbr->getSummary(), $time),
63+
new PostRunEvent($alias ?? $this->alias, $tbr->getSummary(), $time, $scheme),
6064
PostRunEvent::EVENT_ID
6165
);
6266
} catch (Neo4jException $e) {
6367
$this->stopwatch?->stop($stopWatchName);
6468
/** @noinspection PhpUnhandledExceptionInspection */
6569
$time = new \DateTimeImmutable('now', new \DateTimeZone(date_default_timezone_get()));
66-
$event = new FailureEvent($alias ?? $this->alias, $statement, $e, $time);
70+
$event = new FailureEvent($alias ?? $this->alias, $statement, $e, $time, $scheme);
6771
$event = $this->dispatcher->dispatch($event, FailureEvent::EVENT_ID);
6872

6973
if ($event->shouldThrowException()) {

src/Resources/views/web_profiler.html.twig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
<div>
9292
<strong class="font-normal text-small">Alias</strong>: {{ statement.alias|default('N/A') }}
9393
</div>
94+
{# TODO: Add scheme. Scheme is currently private in the underlying driver #}
95+
{# <div>#}
96+
{# <strong class="font-normal text-small">Scheme</strong>: {{ statement.scheme|default('N/A') }}#}
97+
{# </div>#}
9498
<div>
9599
<strong class="font-normal text-small">Statistics</strong>: {{ statement|json_encode }}
96100
</div>

src/SymfonyClient.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ public function run(string $statement, iterable $parameters = [], ?string $alias
3737

3838
public function runStatement(Statement $statement, ?string $alias = null): ?SummarizedResult
3939
{
40-
return $this->handler->handle(fn (Statement $statement) => $this->client->runStatement($statement, $alias), $statement, $alias);
40+
return $this->handler->handle(
41+
fn(Statement $statement) => $this->client->runStatement($statement, $alias),
42+
$statement,
43+
$alias,
44+
null
45+
);
4146
}
4247

4348
public function runStatements(iterable $statements, ?string $alias = null): CypherList
@@ -50,14 +55,17 @@ public function runStatements(iterable $statements, ?string $alias = null): Cyph
5055
return CypherList::fromIterable($tbr);
5156
}
5257

53-
public function beginTransaction(?iterable $statements = null, ?string $alias = null, ?TransactionConfiguration $config = null): UnmanagedTransactionInterface
54-
{
58+
public function beginTransaction(
59+
?iterable $statements = null,
60+
?string $alias = null,
61+
?TransactionConfiguration $config = null
62+
): UnmanagedTransactionInterface {
5563
$tsx = new SymfonyTransaction($this->client->beginTransaction(null, $alias, $config), $this->handler, $alias);
5664

57-
$runHandler = fn (Statement $statement): CypherList => $tsx->runStatement($statement);
65+
$runHandler = fn(Statement $statement): CypherList => $tsx->runStatement($statement);
5866

5967
foreach (($statements ?? []) as $statement) {
60-
$this->handler->handle($runHandler, $statement, $alias);
68+
$this->handler->handle($runHandler, $statement, $alias, null);
6169
}
6270

6371
return $tsx;
@@ -68,24 +76,30 @@ public function getDriver(?string $alias): DriverInterface
6876
return $this->client->getDriver($alias);
6977
}
7078

71-
public function writeTransaction(callable $tsxHandler, ?string $alias = null, ?TransactionConfiguration $config = null)
72-
{
79+
public function writeTransaction(
80+
callable $tsxHandler,
81+
?string $alias = null,
82+
?TransactionConfiguration $config = null
83+
) {
7384
$sessionConfig = SessionConfiguration::default()->withAccessMode(AccessMode::READ());
7485
$session = $this->client->getDriver($alias)->createSession($sessionConfig);
7586

7687
return TransactionHelper::retry(
77-
fn () => new SymfonyTransaction($session->beginTransaction([], $config), $this->handler, $alias),
88+
fn() => new SymfonyTransaction($session->beginTransaction([], $config), $this->handler, $alias),
7889
$tsxHandler
7990
);
8091
}
8192

82-
public function readTransaction(callable $tsxHandler, ?string $alias = null, ?TransactionConfiguration $config = null)
83-
{
93+
public function readTransaction(
94+
callable $tsxHandler,
95+
?string $alias = null,
96+
?TransactionConfiguration $config = null
97+
) {
8498
$sessionConfig = SessionConfiguration::default()->withAccessMode(AccessMode::WRITE());
8599
$session = $this->client->getDriver($alias)->createSession($sessionConfig);
86100

87101
return TransactionHelper::retry(
88-
fn () => new SymfonyTransaction($session->beginTransaction([], $config), $this->handler, $alias),
102+
fn() => new SymfonyTransaction($session->beginTransaction([], $config), $this->handler, $alias),
89103
$tsxHandler
90104
);
91105
}

src/SymfonyTransaction.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ class SymfonyTransaction implements UnmanagedTransactionInterface
1818
/**
1919
* @param UnmanagedTransactionInterface<SummarizedResult<CypherMap>> $tsx
2020
*/
21-
public function __construct(private UnmanagedTransactionInterface $tsx, private EventHandler $handler, private ?string $alias)
22-
{
21+
public function __construct(
22+
private readonly UnmanagedTransactionInterface $tsx,
23+
private readonly EventHandler $handler,
24+
private readonly ?string $alias
25+
) {
2326
}
2427

2528
public function run(string $statement, iterable $parameters = []): SummarizedResult
@@ -29,7 +32,11 @@ public function run(string $statement, iterable $parameters = []): SummarizedRes
2932

3033
public function runStatement(Statement $statement): SummarizedResult
3134
{
32-
return $this->handler->handle(fn ($statement) => $this->tsx->runStatement($statement), $statement, $this->alias);
35+
return $this->handler->handle(fn ($statement) => $this->tsx->runStatement($statement),
36+
$statement,
37+
$this->alias,
38+
null
39+
);
3340
}
3441

3542
/**

0 commit comments

Comments
 (0)