22
33namespace Bolt ;
44
5- use Bolt \error \{ ConnectException , PackException , UnpackException } ;
5+ use Bolt \error \ConnectException ;
66use Exception ;
7- use Bolt \packstream \{IPacker , IUnpacker };
87use Bolt \protocol \{AProtocol , ServerState };
98use Bolt \connection \IConnection ;
109
1918 */
2019final 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 {
0 commit comments