1111 * file that was distributed with this source code.
1212 */
1313
14- namespace Laudis \Neo4j \Network \ Bolt ;
14+ namespace Laudis \Neo4j \Network ;
1515
1616use Ds \Map ;
1717use Ds \Vector ;
1818use Exception ;
1919use Laudis \Neo4j \ClientBuilder ;
2020use Laudis \Neo4j \Contracts \ClientInterface ;
21+ use Laudis \Neo4j \Contracts \Injections ;
2122use Laudis \Neo4j \Contracts \SessionInterface ;
2223use Laudis \Neo4j \Contracts \TransactionInterface ;
2324use Laudis \Neo4j \Databags \Statement ;
2425use Laudis \Neo4j \Enum \RoutingRoles ;
26+ use Laudis \Neo4j \Network \Bolt \BoltInjections ;
27+ use Laudis \Neo4j \Network \Http \HttpInjections ;
2528use function parse_url ;
2629use function preg_match ;
2730use function random_int ;
@@ -32,12 +35,16 @@ final class AutoRoutedSession implements SessionInterface
3235 private SessionInterface $ referenceSession ;
3336 private ?ClientInterface $ client = null ;
3437 private ?RoutingTable $ table = null ;
35- private BoltInjections $ injections ;
38+ /** @var BoltInjections|HttpInjections */
39+ private Injections $ injections ;
3640 private int $ maxLeader = 0 ;
3741 private int $ maxFollower = 0 ;
3842 private array $ parsedUrl ;
3943
40- public function __construct (SessionInterface $ referenceSession , BoltInjections $ injections , array $ parsedUrl )
44+ /**
45+ * @param BoltInjections|HttpInjections $injections
46+ */
47+ public function __construct (SessionInterface $ referenceSession , Injections $ injections , array $ parsedUrl )
4148 {
4249 $ this ->referenceSession = $ referenceSession ;
4350 $ this ->injections = $ injections ;
@@ -81,20 +88,20 @@ private function setupClient(): ClientInterface
8188 $ values = $ response ->get ('servers ' );
8289 /** @var int $ttl */
8390 $ ttl = $ response ->get ('ttl ' );
91+ if ($ this ->injections instanceof HttpInjections) {
92+ $ values = $ this ->translateTableToHttp ($ values );
93+ }
8494 $ this ->table = new RoutingTable ($ values , time () + $ ttl );
8595
8696 $ builder = ClientBuilder::create ();
8797 $ leaders = $ this ->table ->getWithRole (RoutingRoles::LEADER ());
8898 $ followers = $ this ->table ->getWithRole (RoutingRoles::FOLLOWER ());
8999 $ injections = $ this ->injections ->withAutoRouting (false );
90100
91- foreach ($ leaders as $ i => $ leader ) {
92- $ builder = $ builder ->addBoltConnection ('leader- ' .$ i , $ this ->rebuildUrl ($ leader ), $ injections );
93- $ this ->maxLeader = $ i ;
94- }
95- foreach ($ followers as $ i => $ follower ) {
96- $ builder = $ builder ->addBoltConnection ('follower- ' .$ i , $ this ->rebuildUrl ($ follower ), $ injections );
97- $ this ->maxFollower = $ i ;
101+ if ($ injections instanceof BoltInjections) {
102+ $ builder = $ this ->buildBoltConnections ($ leaders , $ builder , $ injections , $ followers );
103+ } else {
104+ $ builder = $ this ->buildHttpConnections ($ leaders , $ builder , $ injections , $ followers );
98105 }
99106
100107 $ this ->client = $ builder ->build ();
@@ -183,9 +190,9 @@ public function commitTransaction(TransactionInterface $transaction, iterable $s
183190 return $ transaction ->commit ($ statements );
184191 }
185192
186- private function rebuildUrl (string $ url ): string
193+ private function rebuildUrl (array $ parsedUrl ): string
187194 {
188- $ parts = array_merge ($ this ->parsedUrl , parse_url ( $ url ) );
195+ $ parts = array_merge ($ this ->parsedUrl , $ parsedUrl );
189196
190197 return (isset ($ parts ['scheme ' ]) ? "{$ parts ['scheme ' ]}: " : '' ).
191198 ((isset ($ parts ['user ' ]) || isset ($ parts ['host ' ])) ? '// ' : '' ).
@@ -198,4 +205,69 @@ private function rebuildUrl(string $url): string
198205 (isset ($ parts ['query ' ]) ? "? {$ parts ['query ' ]}" : '' ).
199206 (isset ($ parts ['fragment ' ]) ? "# {$ parts ['fragment ' ]}" : '' );
200207 }
208+
209+ /**
210+ * @param Vector<string> $leaders
211+ * @param Vector<string> $followers
212+ */
213+ private function buildBoltConnections (
214+ Vector $ leaders ,
215+ ClientBuilder $ builder ,
216+ BoltInjections $ injections ,
217+ Vector $ followers
218+ ): ClientBuilder {
219+ foreach ($ leaders as $ i => $ leader ) {
220+ $ builder = $ builder ->addBoltConnection ('leader- ' .$ i , $ this ->rebuildUrl (parse_url ($ leader )), $ injections );
221+ $ this ->maxLeader = $ i ;
222+ }
223+ foreach ($ followers as $ i => $ follower ) {
224+ $ builder = $ builder ->addBoltConnection ('follower- ' .$ i , $ this ->rebuildUrl (parse_url ($ follower )), $ injections );
225+ $ this ->maxFollower = $ i ;
226+ }
227+
228+ return $ builder ;
229+ }
230+
231+ /**
232+ * @param Vector<string> $leaders
233+ * @param Vector<string> $followers
234+ */
235+ private function buildHttpConnections (
236+ Vector $ leaders ,
237+ ClientBuilder $ builder ,
238+ HttpInjections $ injections ,
239+ Vector $ followers
240+ ): ClientBuilder {
241+ foreach ($ leaders as $ i => $ leader ) {
242+ $ builder = $ builder ->addHttpConnection ('leader- ' .$ i , $ this ->rebuildUrl (parse_url ($ leader )), $ injections );
243+ $ this ->maxLeader = $ i ;
244+ }
245+ foreach ($ followers as $ i => $ follower ) {
246+ $ builder = $ builder ->addHttpConnection ('follower- ' .$ i , $ this ->rebuildUrl (parse_url ($ follower )), $ injections );
247+ $ this ->maxFollower = $ i ;
248+ }
249+
250+ return $ builder ;
251+ }
252+
253+ /**
254+ * @param iterable<array{addresses: list<string>, role:string}> $servers
255+ *
256+ * @return iterable<array{addresses: list<string>, role:string}>
257+ */
258+ private function translateTableToHttp (iterable $ servers ): iterable
259+ {
260+ /** @var list<array{addresses: list<string>, role:string}> */
261+ $ tbr = [];
262+
263+ foreach ($ servers as $ server ) {
264+ $ row = ['addresses ' => [], 'role ' => $ server ['role ' ]];
265+ foreach ($ server ['addresses ' ] as $ address ) {
266+ $ row ['addresses ' ][] = $ this ->rebuildUrl (['host ' => parse_url ($ address , PHP_URL_HOST )]);
267+ }
268+ $ tbr [] = $ row ;
269+ }
270+
271+ return $ tbr ;
272+ }
201273}
0 commit comments