Skip to content

Commit 84d6f88

Browse files
Merge pull request #107 from neo4j-php/php8
Php8
2 parents ab7374c + 30080da commit 84d6f88

File tree

95 files changed

+808
-1293
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+808
-1293
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ PHP library for communication with graph database over TCP socket with Bolt prot
1515

1616
## Version support
1717

18-
We are trying to keep up and this library supports **Neo4j <= 5.2** with **Bolt <= 5.2**.
18+
We are trying to keep up and this library supports **Neo4j <= 5.2** with **Bolt <= 5.0**.
1919

2020
https://www.neo4j.com/docs/bolt/current/bolt-compatibility/
2121

2222
## Requirements
2323

24-
Keep up with [PHP supported versions](https://www.php.net/supported-versions.php) means we are at **PHP >= 7.4**.
24+
Keep up with [PHP supported versions](https://www.php.net/supported-versions.php) means we are at **PHP >= 8.0**.
2525

26-
_If you need support for PHP < 7.4 you can use latest v3.x release. Not all new features are implement backwards and this readme is updated to latest released version._
26+
_If you need support for PHP < 7.4 you can use latest v3.x release and if you need support for PHP 7.4 you can use v5.x. Not all new features are implement backwards and this readme is updated to latest released version._
2727

2828
### Extensions
2929

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"license": "MIT",
99
"minimum-stability": "stable",
1010
"require": {
11-
"php": ">=7.4",
11+
"php": ">=8",
1212
"ext-mbstring": "*"
1313
},
1414
"require-dev": {

src/Bolt.php

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
namespace Bolt;
44

5-
use Bolt\error\{ConnectException, PackException, UnpackException};
6-
use Exception;
7-
use Bolt\packstream\{IPacker, IUnpacker};
5+
use Bolt\error\ConnectException;
6+
use Bolt\error\BoltException;
87
use Bolt\protocol\{AProtocol, ServerState};
98
use Bolt\connection\IConnection;
109

@@ -19,29 +18,20 @@
1918
*/
2019
final class Bolt
2120
{
22-
private IPacker $packer;
23-
private IUnpacker $unpacker;
24-
private IConnection $connection;
25-
private array $versions = [];
21+
private array $protocolVersions = [];
22+
private int $packStreamVersion = 1;
23+
2624
public static bool $debug = false;
2725
public ServerState $serverState;
2826

29-
/**
30-
* Bolt constructor
31-
* @param IConnection $connection
32-
* @throws Exception
33-
*/
34-
public function __construct(IConnection $connection)
27+
public function __construct(private IConnection $connection)
3528
{
36-
$this->connection = $connection;
3729
$this->setProtocolVersions(5, 4.4, 4.3);
38-
$this->setPackStreamVersion();
3930
}
4031

4132
/**
4233
* Connect via Connection, execute handshake on it, create and return protocol version class
43-
* @return AProtocol
44-
* @throws Exception
34+
* @throws BoltException
4535
*/
4636
public function build(): AProtocol
4737
{
@@ -65,47 +55,32 @@ public function build(): AProtocol
6555
}
6656

6757
$this->serverState->set(ServerState::CONNECTED);
68-
return new $protocolClass($this->packer, $this->unpacker, $this->connection, $this->serverState);
58+
return new $protocolClass($this->packStreamVersion, $this->connection, $this->serverState);
6959
}
7060

71-
/**
72-
* @param int|float|string ...$v
73-
* @return Bolt
74-
*/
75-
public function setProtocolVersions(...$v): Bolt
61+
public function setProtocolVersions(int|float|string ...$v): Bolt
7662
{
77-
$this->versions = array_slice($v, 0, 4);
78-
while (count($this->versions) < 4)
79-
$this->versions[] = 0;
63+
$this->protocolVersions = array_slice($v, 0, 4);
64+
while (count($this->protocolVersions) < 4)
65+
$this->protocolVersions[] = 0;
8066
return $this;
8167
}
8268

83-
/**
84-
* @param int $version
85-
* @return Bolt
86-
* @throws Exception
87-
*/
8869
public function setPackStreamVersion(int $version = 1): Bolt
8970
{
90-
$packerClass = "\\Bolt\\packstream\\v" . $version . "\\Packer";
91-
if (!class_exists($packerClass)) {
92-
throw new PackException('Requested PackStream version (' . $version . ') not yet implemented');
93-
}
94-
$this->packer = new $packerClass();
95-
96-
$unpackerClass = "\\Bolt\\packstream\\v" . $version . "\\Unpacker";
97-
if (!class_exists($unpackerClass)) {
98-
throw new UnpackException('Requested PackStream version (' . $version . ') not yet implemented');
99-
}
100-
$this->unpacker = new $unpackerClass();
71+
$this->packStreamVersion = $version;
72+
return $this;
73+
}
10174

75+
public function setConnection(IConnection $connection): Bolt
76+
{
77+
$this->connection = $connection;
10278
return $this;
10379
}
10480

10581
/**
10682
* @link https://www.neo4j.com/docs/bolt/current/bolt/handshake/
107-
* @return string
108-
* @throws Exception
83+
* @throws BoltException
10984
*/
11085
private function handshake(): string
11186
{
@@ -126,8 +101,6 @@ private function handshake(): string
126101

127102
/**
128103
* Read and compose selected protocol version
129-
* @param string $bytes
130-
* @return string|null
131104
*/
132105
private function unpackProtocolVersion(string $bytes): ?string
133106
{
@@ -142,18 +115,17 @@ private function unpackProtocolVersion(string $bytes): ?string
142115
}
143116

144117
$version = implode('.', array_reverse($result));
145-
return in_array($version, $this->versions) ? $version : null;
118+
return in_array($version, $this->protocolVersions) ? $version : null;
146119
}
147120

148121
/**
149122
* Pack requested protocol versions
150-
* @return string
151123
*/
152124
private function packProtocolVersions(): string
153125
{
154126
$versions = [];
155127

156-
foreach ($this->versions as $v) {
128+
foreach ($this->protocolVersions as $v) {
157129
if (is_int($v))
158130
$versions[] = pack('N', $v);
159131
else {

src/autoload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
define('DS', DIRECTORY_SEPARATOR);
3+
const DS = DIRECTORY_SEPARATOR;
44

55
spl_autoload_register(function ($name) {
66
$parts = explode("\\", $name);

src/connection/AConnection.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,24 @@
1111
*/
1212
abstract class AConnection implements IConnection
1313
{
14-
protected string $ip;
15-
protected int $port;
16-
protected float $timeout;
17-
18-
/**
19-
* @inheritDoc
20-
*/
21-
public function __construct(string $ip = '127.0.0.1', int $port = 7687, float $timeout = 15)
14+
public function __construct(
15+
protected string $ip = '127.0.0.1',
16+
protected int $port = 7687,
17+
protected float $timeout = 15
18+
)
2219
{
23-
if (filter_var($ip, FILTER_VALIDATE_URL)) {
24-
$scheme = parse_url($ip, PHP_URL_SCHEME);
20+
if (filter_var($this->ip, FILTER_VALIDATE_URL)) {
21+
$scheme = parse_url($this->ip, PHP_URL_SCHEME);
2522
if (!empty($scheme)) {
26-
$ip = str_replace($scheme . '://', '', $ip);
23+
$this->ip = str_replace($scheme . '://', '', $this->ip);
2724
}
2825
}
29-
30-
$this->ip = $ip;
31-
$this->port = $port;
32-
$this->timeout = $timeout;
3326
}
3427

3528
/**
3629
* Print buffer as HEX
37-
* @param string $str
38-
* @param string $prefix
3930
*/
40-
protected function printHex(string $str, string $prefix = 'C: ')
31+
protected function printHex(string $str, string $prefix = 'C: '): void
4132
{
4233
$str = implode(unpack('H*', $str));
4334
echo '<pre>' . $prefix;
@@ -63,7 +54,7 @@ public function getTimeout(): float
6354
return $this->timeout;
6455
}
6556

66-
public function setTimeout(float $timeout)
57+
public function setTimeout(float $timeout): void
6758
{
6859
$this->timeout = $timeout;
6960
}

src/connection/IConnection.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Bolt\connection;
44

5+
use Bolt\error\ConnectException;
6+
57
/**
68
* Interface IConnection
79
*
@@ -11,26 +13,30 @@
1113
*/
1214
interface IConnection
1315
{
14-
/**
15-
* @param string $ip
16-
* @param int $port
17-
* @param float $timeout
18-
*/
1916
public function __construct(string $ip = '127.0.0.1', int $port = 7687, float $timeout = 15);
2017

18+
/**
19+
* @throws ConnectException
20+
*/
2121
public function connect(): bool;
2222

23-
public function write(string $buffer);
23+
/**
24+
* @throws ConnectException
25+
*/
26+
public function write(string $buffer): void;
2427

28+
/**
29+
* @throws ConnectException
30+
*/
2531
public function read(int $length = 2048): string;
2632

27-
public function disconnect();
33+
public function disconnect(): void;
2834

2935
public function getIp(): string;
3036

3137
public function getPort(): int;
3238

3339
public function getTimeout(): float;
3440

35-
public function setTimeout(float $timeout);
41+
public function setTimeout(float $timeout): void;
3642
}

src/connection/Socket.php

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@
1616
class Socket extends AConnection
1717
{
1818
/**
19-
* @var resource|object|bool
19+
* @var resource|\Socket|bool
2020
*/
2121
private $socket = false;
2222

2323
private const POSSIBLE_TIMEOUTS_CODES = [11, 10060];
2424

25-
/**
26-
* Create socket connection
27-
* @return bool
28-
* @throws ConnectException
29-
*/
3025
public function connect(): bool
3126
{
3227
if (!extension_loaded('sockets')) {
@@ -55,12 +50,7 @@ public function connect(): bool
5550
return true;
5651
}
5752

58-
/**
59-
* Write buffer to socket
60-
* @param string $buffer
61-
* @throws ConnectException
62-
*/
63-
public function write(string $buffer)
53+
public function write(string $buffer): void
6454
{
6555
if ($this->socket === false) {
6656
throw new ConnectException('Not initialized socket');
@@ -79,20 +69,14 @@ public function write(string $buffer)
7969
}
8070
}
8171

82-
/**
83-
* Read buffer from socket
84-
* @param int $length
85-
* @return string
86-
* @throws ConnectException
87-
*/
8872
public function read(int $length = 2048): string
8973
{
9074
if ($this->socket === false)
9175
throw new ConnectException('Not initialized socket');
9276

9377
$output = '';
9478
do {
95-
$readed = @socket_read($this->socket, $length - mb_strlen($output, '8bit'), PHP_BINARY_READ);
79+
$readed = @socket_read($this->socket, $length - mb_strlen($output, '8bit'));
9680
if ($readed === false)
9781
$this->throwConnectException();
9882
$output .= $readed;
@@ -104,24 +88,21 @@ public function read(int $length = 2048): string
10488
return $output;
10589
}
10690

107-
/**
108-
* Close socket connection
109-
*/
110-
public function disconnect()
91+
public function disconnect(): void
11192
{
11293
if ($this->socket !== false) {
11394
@socket_shutdown($this->socket);
11495
@socket_close($this->socket);
11596
}
11697
}
11798

118-
public function setTimeout(float $timeout)
99+
public function setTimeout(float $timeout): void
119100
{
120101
parent::setTimeout($timeout);
121102
$this->configureTimeout();
122103
}
123104

124-
private function configureTimeout()
105+
private function configureTimeout(): void
125106
{
126107
if ($this->socket === false)
127108
return;

0 commit comments

Comments
 (0)