Skip to content

Commit 62ea691

Browse files
Unified logic for new bolt messages. Instead of updating Interface and Abstract class every new method has to ask if it exists. Refactored route method and moved destructor to last place as it should be.
1 parent 9dabf7d commit 62ea691

File tree

4 files changed

+25
-70
lines changed

4 files changed

+25
-70
lines changed

src/Bolt.php

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ public function discard(int $n = -1, int $qid = -1): bool
322322

323323
/**
324324
* Send BEGIN message
325+
* @version >=3
325326
* @param array $extra extra::Dictionary(bookmarks::List<String>, tx_timeout::Integer, tx_metadata::Dictionary, mode::String, db:String)
326327
<pre>The bookmarks is a list of strings containg some kind of bookmark identification e.g [“neo4j-bookmark-transaction:1”, “neo4j-bookmark-transaction:2”]
327328
The tx_timeout is an integer in that specifies a transaction timeout in ms.
@@ -335,31 +336,33 @@ public function begin(array $extra = []): bool
335336
{
336337
if (self::$debug)
337338
echo 'BEGIN';
338-
return $this->protocol->begin($extra);
339+
return method_exists($this->protocol, 'begin') && $this->protocol->begin($extra);
339340
}
340341

341342
/**
342343
* Send COMMIT message
344+
* @version >=3
343345
* @return bool
344346
* @throws Exception
345347
*/
346348
public function commit(): bool
347349
{
348350
if (self::$debug)
349351
echo 'COMMIT';
350-
return $this->protocol->commit();
352+
return method_exists($this->protocol, 'commit') && $this->protocol->commit();
351353
}
352354

353355
/**
354356
* Send ROLLBACK message
357+
* @version >=3
355358
* @return bool
356359
* @throws Exception
357360
*/
358361
public function rollback(): bool
359362
{
360363
if (self::$debug)
361364
echo 'ROLLBACK';
362-
return $this->protocol->rollback();
365+
return method_exists($this->protocol, 'rollback') && $this->protocol->rollback();
363366
}
364367

365368
/**
@@ -374,6 +377,22 @@ public function reset(): bool
374377
return $this->protocol->reset();
375378
}
376379

380+
/**
381+
* Send ROUTE message to instruct the server to return the current routing table.
382+
* @version >=4.3 In previous versions there was no explicit message for this and a procedure had to be invoked using Cypher through the RUN and PULL messages.
383+
* @param array|null $routing
384+
* @param array $bookmarks
385+
* @param array|string|null $extra
386+
* @return array|null
387+
*/
388+
public function route(?array $routing = null, array $bookmarks = [], $extra = null): ?array
389+
{
390+
if (self::$debug)
391+
echo 'ROUTE';
392+
$routing = $routing ?? ['address' => $this->connection->getIp() . ':' . $this->connection->getPort()];
393+
return method_exists($this->protocol, 'route') ? $this->protocol->route($routing, $bookmarks, $extra) : null;
394+
}
395+
377396
/**
378397
* Say goodbye
379398
*/
@@ -382,26 +401,9 @@ public function __destruct()
382401
if ($this->protocol instanceof AProtocol) {
383402
if (self::$debug)
384403
echo 'GOODBYE';
385-
$this->protocol->goodbye();
404+
method_exists($this->protocol, 'goodbye') && $this->protocol->goodbye();
386405
}
387406

388407
$this->connection->disconnect();
389408
}
390-
391-
/**
392-
* Fetch the current routing table, if the message specification allows it.
393-
* @param array|null $routing
394-
* @return array|null
395-
*/
396-
public function route(?array $routing = null): ?array
397-
{
398-
if (self::$debug)
399-
echo 'ROUTE';
400-
401-
if (!method_exists($this->protocol, 'route'))
402-
return null;
403-
404-
$routing = $routing ?? ['address' => $this->connection->getIp() . ':' . $this->connection->getPort()];
405-
return $this->protocol->route($routing);
406-
}
407409
}

src/protocol/AProtocol.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,6 @@ public function __construct(IPacker $packer, IUnpacker $unpacker, IConnection $c
5050
$this->connection = $connection;
5151
}
5252

53-
public function begin(...$args): bool
54-
{
55-
return false;
56-
}
57-
58-
public function commit(...$args): bool
59-
{
60-
return false;
61-
}
62-
63-
public function rollback(...$args): bool
64-
{
65-
return false;
66-
}
67-
68-
public function goodbye(...$args)
69-
{
70-
71-
}
72-
7353
/**
7454
* Write to connection
7555
* @param Generator $generator

src/protocol/IProtocol.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,4 @@ public function discardAll(...$args): bool;
4646
*/
4747
public function reset(...$args): bool;
4848

49-
/**
50-
* Send BEGIN message
51-
* @param mixed ...$args
52-
* @return bool
53-
*/
54-
public function begin(...$args): bool;
55-
56-
/**
57-
* Send COMMIT message
58-
* @param mixed ...$args
59-
* @return bool
60-
*/
61-
public function commit(...$args): bool;
62-
63-
/**
64-
* Send ROLLBACK message
65-
* @param mixed ...$args
66-
* @return bool
67-
*/
68-
public function rollback(...$args): bool;
69-
70-
/**
71-
* Send GOODBYE message
72-
* @param mixed ...$args
73-
*/
74-
public function goodbye(...$args);
75-
7649
}

src/protocol/V4_3.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Bolt\error\MessageException;
66
use Bolt\error\PackException;
7+
use Exception;
78

89
/**
910
* Class Protocol version 4.3
@@ -18,8 +19,7 @@ class V4_3 extends V4_2
1819
/**
1920
* @param array|string|null ...$args
2021
* @return array
21-
* @throws MessageException
22-
* @throws PackException
22+
* @throws Exception
2323
*/
2424
public function route(...$args): array
2525
{

0 commit comments

Comments
 (0)