Skip to content

Commit daa5d6f

Browse files
committed
Unified Injections
1 parent b7d60ae commit daa5d6f

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

src/Contracts/Injections.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Laudis Neo4j package.
7+
*
8+
* (c) Laudis technologies <http://laudis.tech>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Laudis\Neo4j\Contracts;
15+
16+
interface Injections
17+
{
18+
/**
19+
* @param string|callable():string $database
20+
*
21+
* @return static
22+
*/
23+
public function withDatabase($database): self;
24+
25+
/**
26+
* @param callable():bool|bool $routing
27+
*
28+
* @return static
29+
*/
30+
public function withAutoRouting($routing): self;
31+
32+
public function database(): string;
33+
34+
public function hasAutoRouting(): bool;
35+
}

src/Network/Bolt/BoltInjections.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use function call_user_func;
1717
use function is_callable;
18+
use Laudis\Neo4j\Contracts\Injections;
1819

1920
/**
2021
* @psalm-type SSLContextOptions = null|array{
@@ -39,7 +40,7 @@
3940
*
4041
* @psalm-type LazySSLContextOptions = callable():SSLContextOptions|SSLContextOptions
4142
*/
42-
final class BoltInjections
43+
final class BoltInjections implements Injections
4344
{
4445
/** @var callable():string|string */
4546
private $database;

src/Network/Http/HttpInjections.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515

1616
use Http\Discovery\Psr17FactoryDiscovery;
1717
use Http\Discovery\Psr18ClientDiscovery;
18+
use Laudis\Neo4j\Contracts\Injections;
1819
use Psr\Http\Client\ClientInterface;
1920
use Psr\Http\Message\RequestFactoryInterface;
2021
use Psr\Http\Message\StreamFactoryInterface;
2122

22-
final class HttpInjections
23+
final class HttpInjections implements Injections
2324
{
2425
/** @var ClientInterface|callable():ClientInterface */
2526
private $client;
@@ -29,6 +30,8 @@ final class HttpInjections
2930
private $requestFactory;
3031
/** @var string|callable():string */
3132
private $database;
33+
/** @var bool|callable():bool */
34+
private $autoRouting;
3235

3336
/**
3437
* Injector constructor.
@@ -37,8 +40,9 @@ final class HttpInjections
3740
* @param ClientInterface|callable():ClientInterface|null $client
3841
* @param StreamFactoryInterface|callable():StreamFactoryInterface|null $streamFactory
3942
* @param RequestFactoryInterface|callable():RequestFactoryInterface|null $requestFactory
43+
* @param bool|callable():bool $autoRouting
4044
*/
41-
public function __construct($database = null, $client = null, $streamFactory = null, $requestFactory = null)
45+
public function __construct($database = null, $client = null, $streamFactory = null, $requestFactory = null, $autoRouting = null)
4246
{
4347
$this->database = $database ?? static function (): string {
4448
return 'neo4j';
@@ -52,6 +56,7 @@ public function __construct($database = null, $client = null, $streamFactory = n
5256
$this->requestFactory = $requestFactory ?? static function (): RequestFactoryInterface {
5357
return Psr17FactoryDiscovery::findRequestFactory();
5458
};
59+
$this->autoRouting = $autoRouting ?? false;
5560
}
5661

5762
public static function create(): self
@@ -73,31 +78,31 @@ public function client(): ClientInterface
7378
*/
7479
public function withClient($client): self
7580
{
76-
return new self($this->database, $client, $this->streamFactory, $this->requestFactory);
81+
return new self($this->database, $client, $this->streamFactory, $this->requestFactory, $this->autoRouting);
7782
}
7883

7984
/**
8085
* @param StreamFactoryInterface|callable():StreamFactoryInterface $factory
8186
*/
8287
public function withStreamFactory($factory): self
8388
{
84-
return new self($this->database, $this->client, $factory, $this->requestFactory);
89+
return new self($this->database, $this->client, $factory, $this->requestFactory, $this->autoRouting);
8590
}
8691

8792
/**
8893
* @param RequestFactoryInterface|callable():RequestFactoryInterface $factory
8994
*/
9095
public function withRequestFactory($factory): self
9196
{
92-
return new self($this->database, $this->client, $this->streamFactory, $factory);
97+
return new self($this->database, $this->client, $this->streamFactory, $factory, $this->autoRouting);
9398
}
9499

95100
/**
96101
* @param string|callable():string $database
97102
*/
98103
public function withDatabase($database): self
99104
{
100-
return new self($database, $this->client, $this->streamFactory, $this->requestFactory);
105+
return new self($database, $this->client, $this->streamFactory, $this->requestFactory, $this->autoRouting);
101106
}
102107

103108
public function streamFactory(): StreamFactoryInterface
@@ -126,4 +131,24 @@ public function database(): string
126131

127132
return $this->database;
128133
}
134+
135+
public function withAutoRouting($routing): Injections
136+
{
137+
return new self(
138+
$this->database,
139+
$this->client,
140+
$this->streamFactory,
141+
$this->requestFactory,
142+
$routing
143+
);
144+
}
145+
146+
public function hasAutoRouting(): bool
147+
{
148+
if (is_callable($this->autoRouting)) {
149+
$this->autoRouting = call_user_func($this->autoRouting);
150+
}
151+
152+
return $this->autoRouting;
153+
}
129154
}

0 commit comments

Comments
 (0)