Skip to content

Commit 411fdb4

Browse files
Removed unnecessary annotations. Moved packer and unpacker instantiation into protocol.
1 parent e035136 commit 411fdb4

Some content is hidden

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

76 files changed

+428
-632
lines changed

src/Bolt.php

Lines changed: 16 additions & 23 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};
5+
use Bolt\error\ConnectException;
66
use Exception;
7-
use Bolt\packstream\{IPacker, IUnpacker};
87
use Bolt\protocol\{AProtocol, ServerState};
98
use Bolt\connection\IConnection;
109

@@ -19,16 +18,15 @@
1918
*/
2019
final class Bolt
2120
{
22-
private IPacker $packer;
23-
private IUnpacker $unpacker;
24-
private array $versions = [];
21+
private array $protocolVersions = [];
22+
private int $packStreamVersion = 1;
23+
2524
public static bool $debug = false;
2625
public ServerState $serverState;
2726

2827
public function __construct(private IConnection $connection)
2928
{
3029
$this->setProtocolVersions(5, 4.4, 4.3);
31-
$this->setPackStreamVersion();
3230
}
3331

3432
/**
@@ -57,31 +55,26 @@ public function build(): AProtocol
5755
}
5856

5957
$this->serverState->set(ServerState::CONNECTED);
60-
return new $protocolClass($this->packer, $this->unpacker, $this->connection, $this->serverState);
58+
return new $protocolClass($this->packStreamVersion, $this->connection, $this->serverState);
6159
}
6260

6361
public function setProtocolVersions(int|float|string ...$v): Bolt
6462
{
65-
$this->versions = array_slice($v, 0, 4);
66-
while (count($this->versions) < 4)
67-
$this->versions[] = 0;
63+
$this->protocolVersions = array_slice($v, 0, 4);
64+
while (count($this->protocolVersions) < 4)
65+
$this->protocolVersions[] = 0;
6866
return $this;
6967
}
7068

7169
public function setPackStreamVersion(int $version = 1): Bolt
7270
{
73-
$packerClass = "\\Bolt\\packstream\\v" . $version . "\\Packer";
74-
if (!class_exists($packerClass)) {
75-
throw new PackException('Requested PackStream version (' . $version . ') not yet implemented');
76-
}
77-
$this->packer = new $packerClass();
78-
79-
$unpackerClass = "\\Bolt\\packstream\\v" . $version . "\\Unpacker";
80-
if (!class_exists($unpackerClass)) {
81-
throw new UnpackException('Requested PackStream version (' . $version . ') not yet implemented');
82-
}
83-
$this->unpacker = new $unpackerClass();
71+
$this->packStreamVersion = $version;
72+
return $this;
73+
}
8474

75+
public function setConnection(IConnection $connection): Bolt
76+
{
77+
$this->connection = $connection;
8578
return $this;
8679
}
8780

@@ -122,7 +115,7 @@ private function unpackProtocolVersion(string $bytes): ?string
122115
}
123116

124117
$version = implode('.', array_reverse($result));
125-
return in_array($version, $this->versions) ? $version : null;
118+
return in_array($version, $this->protocolVersions) ? $version : null;
126119
}
127120

128121
/**
@@ -132,7 +125,7 @@ private function packProtocolVersions(): string
132125
{
133126
$versions = [];
134127

135-
foreach ($this->versions as $v) {
128+
foreach ($this->protocolVersions as $v) {
136129
if (is_int($v))
137130
$versions[] = pack('N', $v);
138131
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: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public function __construct(
2727

2828
/**
2929
* Print buffer as HEX
30-
* @param string $str
31-
* @param string $prefix
3230
*/
3331
protected function printHex(string $str, string $prefix = 'C: '): void
3432
{

src/connection/IConnection.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
*/
1212
interface IConnection
1313
{
14-
/**
15-
* @param string $ip
16-
* @param int $port
17-
* @param float $timeout
18-
*/
1914
public function __construct(string $ip = '127.0.0.1', int $port = 7687, float $timeout = 15);
2015

2116
public function connect(): bool;

src/connection/Socket.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ class Socket extends AConnection
2323
private const POSSIBLE_TIMEOUTS_CODES = [11, 10060];
2424

2525
/**
26-
* Create socket connection
27-
* @return bool
2826
* @throws ConnectException
2927
*/
3028
public function connect(): bool
@@ -56,8 +54,6 @@ public function connect(): bool
5654
}
5755

5856
/**
59-
* Write buffer to socket
60-
* @param string $buffer
6157
* @throws ConnectException
6258
*/
6359
public function write(string $buffer): void
@@ -80,9 +76,6 @@ public function write(string $buffer): void
8076
}
8177

8278
/**
83-
* Read buffer from socket
84-
* @param int $length
85-
* @return string
8679
* @throws ConnectException
8780
*/
8881
public function read(int $length = 2048): string
@@ -92,7 +85,7 @@ public function read(int $length = 2048): string
9285

9386
$output = '';
9487
do {
95-
$readed = @socket_read($this->socket, $length - mb_strlen($output, '8bit'), PHP_BINARY_READ);
88+
$readed = @socket_read($this->socket, $length - mb_strlen($output, '8bit'));
9689
if ($readed === false)
9790
$this->throwConnectException();
9891
$output .= $readed;
@@ -104,9 +97,6 @@ public function read(int $length = 2048): string
10497
return $output;
10598
}
10699

107-
/**
108-
* Close socket connection
109-
*/
110100
public function disconnect(): void
111101
{
112102
if ($this->socket !== false) {

src/connection/StreamSocket.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,13 @@ class StreamSocket extends AConnection
2626
/**
2727
* Set SSL Context options
2828
* @link https://www.php.net/manual/en/context.ssl.php
29-
* @param array $options
3029
*/
3130
public function setSslContextOptions(array $options): void
3231
{
3332
$this->sslContextOptions = $options;
3433
}
3534

3635
/**
37-
* ConnectException
38-
* @return bool
3936
* @throws ConnectException
4037
*/
4138
public function connect(): bool
@@ -69,8 +66,6 @@ public function connect(): bool
6966
}
7067

7168
/**
72-
* Write to connection
73-
* @param string $buffer
7469
* @throws ConnectException
7570
*/
7671
public function write(string $buffer): void
@@ -97,9 +92,6 @@ public function write(string $buffer): void
9792
}
9893

9994
/**
100-
* Read from connection
101-
* @param int $length
102-
* @return string
10395
* @throws ConnectException
10496
*/
10597
public function read(int $length = 2048): string
@@ -122,23 +114,22 @@ public function read(int $length = 2048): string
122114
return $output;
123115
}
124116

125-
/**
126-
* Close connection
127-
*/
128117
public function disconnect(): void
129118
{
130119
if (is_resource($this->stream))
131120
stream_socket_shutdown($this->stream, STREAM_SHUT_RDWR);
132121
}
133122

123+
/**
124+
* @throws ConnectException
125+
*/
134126
public function setTimeout(float $timeout): void
135127
{
136128
parent::setTimeout($timeout);
137129
$this->configureTimeout();
138130
}
139131

140132
/**
141-
* @return void
142133
* @throws ConnectException
143134
*/
144135
private function configureTimeout(): void

src/helpers/Auth.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ class Auth
1616

1717
/**
1818
* None authorization
19-
* @param string|null $userAgent
20-
* @return array
2119
*/
2220
public static function none(?string $userAgent = null): array
2321
{
@@ -29,10 +27,6 @@ public static function none(?string $userAgent = null): array
2927

3028
/**
3129
* Basic authorization with username and password
32-
* @param string $username
33-
* @param string $password
34-
* @param string|null $userAgent
35-
* @return array
3630
*/
3731
public static function basic(string $username, string $password, ?string $userAgent = null): array
3832
{
@@ -46,9 +40,6 @@ public static function basic(string $username, string $password, ?string $userAg
4640

4741
/**
4842
* OIDC authorization with token
49-
* @param string $token
50-
* @param string|null $userAgent
51-
* @return array
5243
*/
5344
public static function bearer(string $token, ?string $userAgent = null): array
5445
{
@@ -61,9 +52,6 @@ public static function bearer(string $token, ?string $userAgent = null): array
6152

6253
/**
6354
* Kerberos authorization with token
64-
* @param string $token
65-
* @param string|null $userAgent
66-
* @return array
6755
*/
6856
public static function kerberos(string $token, ?string $userAgent = null): array
6957
{

src/packstream/IPacker.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212
interface IPacker
1313
{
1414
/**
15-
* @param int $signature
16-
* @param mixed ...$params
17-
* @return iterable
15+
* @param array $structuresLt [signature => classFQN]
1816
*/
19-
public function pack(int $signature, mixed ...$params): iterable;
17+
public function __construct(array $structuresLt = []);
2018

2119
/**
22-
* @param array $structures [signature => classFQN]
20+
* Pack message
2321
*/
24-
public function setAvailableStructures(array $structures): void;
22+
public function pack(int $signature, mixed ...$params): iterable;
2523
}

src/packstream/IUnpacker.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,18 @@
1111
*/
1212
interface IUnpacker
1313
{
14+
/**
15+
* @param array $structuresLt [signature => classFQN]
16+
*/
17+
public function __construct(array $structuresLt = []);
18+
1419
/**
1520
* Unpack message
16-
* @param string $msg
17-
* @return mixed
1821
*/
1922
public function unpack(string $msg): mixed;
2023

2124
/**
2225
* Get unpacked message status signature
23-
* @return int
2426
*/
2527
public function getSignature(): int;
26-
27-
/**
28-
* @param array $structures [signature => classFQN]
29-
*/
30-
public function setAvailableStructures(array $structures): void;
3128
}

src/packstream/v1/Packer.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,21 @@ class Packer implements IPacker
2121
private const HUGE = 4294967295;
2222

2323
private bool $littleEndian;
24-
private array $structuresLt = [];
2524

2625
/**
2726
* @inheritDoc
2827
*/
29-
public function setAvailableStructures(array $structures): void
28+
public function __construct(private array $structuresLt = [])
3029
{
31-
$this->structuresLt = $structures;
30+
$this->littleEndian = unpack('S', "\x01\x00")[1] === 1;
3231
}
3332

3433
/**
3534
* Pack message with parameters
36-
* @inheritDoc
3735
* @throws PackException
3836
*/
3937
public function pack(int $signature, mixed ...$params): iterable
4038
{
41-
$this->littleEndian = unpack('S', "\x01\x00")[1] === 1;
42-
4339
//structure
4440
$length = count($params);
4541
if ($length < self::SMALL) { //TINY_STRUCT

0 commit comments

Comments
 (0)