Skip to content

Commit e6f1bc2

Browse files
committed
finished wiring connection
1 parent fa5f520 commit e6f1bc2

File tree

9 files changed

+71
-60
lines changed

9 files changed

+71
-60
lines changed

src/Bolt/BoltDriver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@
1616
use Exception;
1717
use function is_string;
1818
use Laudis\Neo4j\Authentication\Authenticate;
19-
use Laudis\Neo4j\BoltFactory;
20-
use Laudis\Neo4j\Common\SemaphoreFactory;
2119
use Laudis\Neo4j\Common\Uri;
2220
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2321
use Laudis\Neo4j\Contracts\DriverInterface;
2422
use Laudis\Neo4j\Contracts\FormatterInterface;
2523
use Laudis\Neo4j\Contracts\SessionInterface;
26-
use Laudis\Neo4j\Databags\ConnectionRequestData;
2724
use Laudis\Neo4j\Databags\DriverConfiguration;
2825
use Laudis\Neo4j\Databags\SessionConfiguration;
2926
use Laudis\Neo4j\Formatter\OGMFormatter;
@@ -71,6 +68,8 @@ public function __construct(
7168
* ? self<U>
7269
* : self<OGMResults>
7370
* )
71+
*
72+
* @psalm-suppress MixedReturnTypeCoercion
7473
*/
7574
public static function create($uri, ?DriverConfiguration $configuration = null, ?AuthenticateInterface $authenticate = null, FormatterInterface $formatter = null): self
7675
{
@@ -81,6 +80,7 @@ public static function create($uri, ?DriverConfiguration $configuration = null,
8180
$configuration ??= DriverConfiguration::default();
8281
$authenticate ??= Authenticate::fromUrl($uri);
8382

83+
/** @psalm-suppress InvalidArgument */
8484
return new self(
8585
$uri,
8686
ConnectionPool::create($uri, $authenticate, $configuration),

src/BoltFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
namespace Laudis\Neo4j;
1515

1616
use Bolt\Bolt;
17-
use Laudis\Neo4j\Bolt\SystemWideConnectionFactory;
1817
use function explode;
1918
use Laudis\Neo4j\Bolt\BoltConnection;
2019
use Laudis\Neo4j\Bolt\ProtocolFactory;
2120
use Laudis\Neo4j\Bolt\SslConfigurationFactory;
21+
use Laudis\Neo4j\Bolt\SystemWideConnectionFactory;
2222
use Laudis\Neo4j\Bolt\UriConfiguration;
2323
use Laudis\Neo4j\Common\ConnectionConfiguration;
2424
use Laudis\Neo4j\Contracts\BasicConnectionFactoryInterface;

src/Common/SemaphoreFactory.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,11 @@ private function __construct($constructor)
3030
$this->constructor = $constructor;
3131
}
3232

33-
public static function getInstance(): SemaphoreFactory
33+
public static function getInstance(): self
3434
{
35-
if (self::$instance === null) {
36-
if (extension_loaded('ext-sysvsem')) {
37-
self::$instance = new self([SysVSemaphore::class, 'create']);
38-
} else {
39-
self::$instance = new self([SingleThreadedSemaphore::class, 'create']);
40-
}
41-
}
42-
43-
return self::$instance;
35+
return self::$instance ??= (extension_loaded('ext-sysvsem')) ?
36+
new self([SysVSemaphore::class, 'create']) :
37+
new self([SingleThreadedSemaphore::class, 'create']);
4438
}
4539

4640
public function create(UriInterface $uri, DriverConfiguration $config): SemaphoreInterface

src/DriverFactory.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@
2121
use Laudis\Neo4j\Contracts\DriverInterface;
2222
use Laudis\Neo4j\Contracts\FormatterInterface;
2323
use Laudis\Neo4j\Databags\DriverConfiguration;
24+
use Laudis\Neo4j\Formatter\OGMFormatter;
2425
use Laudis\Neo4j\Http\HttpDriver;
2526
use Laudis\Neo4j\Neo4j\Neo4jDriver;
2627
use Psr\Http\Message\UriInterface;
2728

2829
/**
2930
* Factory for creating drivers directly.
3031
*
31-
* @psalm-import-type OGMResults from \Laudis\Neo4j\Formatter\OGMFormatter
32-
*
33-
* @psalm-immutable
32+
* @psalm-import-type OGMResults from OGMFormatter
3433
*/
3534
final class DriverFactory
3635
{
@@ -98,9 +97,6 @@ private static function createBoltDriver($uri, ?DriverConfiguration $configurati
9897
* ? DriverInterface<U>
9998
* : DriverInterface<OGMResults>
10099
* )
101-
* @psalm-mutation-free
102-
*
103-
* @pure
104100
*/
105101
private static function createNeo4jDriver($uri, ?DriverConfiguration $configuration, ?AuthenticateInterface $authenticate, FormatterInterface $formatter = null): DriverInterface
106102
{

src/Http/HttpConnection.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,22 @@ final class HttpConnection implements ConnectionInterface
3333
private ConnectionConfiguration $config;
3434

3535
private bool $isOpen = true;
36+
private AuthenticateInterface $authenticate;
37+
private string $userAgent;
3638

3739
/**
3840
* @psalm-mutation-free
3941
*/
40-
public function __construct(ClientInterface $client, ConnectionConfiguration $config)
41-
{
42+
public function __construct(
43+
ClientInterface $client,
44+
ConnectionConfiguration $config,
45+
AuthenticateInterface $authenticate,
46+
string $userAgent
47+
) {
4248
$this->client = $client;
4349
$this->config = $config;
50+
$this->authenticate = $authenticate;
51+
$this->userAgent = $userAgent;
4452
}
4553

4654
/**
@@ -138,29 +146,30 @@ public function setTimeout(float $timeout): void
138146
*/
139147
public function getAuthentication(): AuthenticateInterface
140148
{
141-
// TODO: Implement getAuthentication() method.
149+
return $this->authenticate;
142150
}
143151

144152
/**
145-
* @psalm-immutable
153+
* @psalm-mutation-free
146154
*/
147155
public function getServerState(): string
148156
{
149-
// TODO: Implement getServerState() method.
157+
return 'UNKNOWN';
150158
}
151159

152160
/**
153-
* @psalm-immutable
161+
* @psalm-mutation-free
154162
*/
155163
public function getEncryptionLevel(): string
156164
{
157-
// TODO: Implement getEncryptionLevel() method.
165+
return $this->config->getEncryptionLevel();
158166
}
159167

160168
/**
161-
* @psalm-immutable
169+
* @psalm-mutation-free
162170
*/
163171
public function getUserAgent(): string
164172
{
173+
return $this->userAgent;
165174
}
166175
}

src/Http/HttpConnectionPool.php

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414
namespace Laudis\Neo4j\Http;
1515

16+
use Generator;
1617
use function json_encode;
1718
use Laudis\Neo4j\Common\ConnectionConfiguration;
1819
use Laudis\Neo4j\Common\Resolvable;
1920
use Laudis\Neo4j\Contracts\AuthenticateInterface;
21+
use Laudis\Neo4j\Contracts\ConnectionInterface;
2022
use Laudis\Neo4j\Contracts\ConnectionPoolInterface;
2123
use Laudis\Neo4j\Databags\DatabaseInfo;
22-
use Laudis\Neo4j\Databags\DriverConfiguration;
2324
use Laudis\Neo4j\Databags\SessionConfiguration;
2425
use Laudis\Neo4j\Enum\ConnectionProtocol;
2526
use Laudis\Neo4j\Formatter\BasicFormatter;
@@ -29,7 +30,7 @@
2930
use Throwable;
3031

3132
/**
32-
* @implements ConnectionPoolInterface<ClientInterface>
33+
* @implements ConnectionPoolInterface<HttpConnection>
3334
*/
3435
final class HttpConnectionPool implements ConnectionPoolInterface
3536
{
@@ -48,32 +49,41 @@ final class HttpConnectionPool implements ConnectionPoolInterface
4849
* @psalm-readonly
4950
*/
5051
private Resolvable $streamFactory;
51-
/** @psalm-readonly */
52-
private DriverConfiguration $config;
52+
private UriInterface $uri;
53+
private AuthenticateInterface $auth;
54+
private string $userAgent;
5355

5456
/**
5557
* @param Resolvable<StreamFactoryInterface> $streamFactory
5658
* @param Resolvable<RequestFactory> $requestFactory
5759
* @param Resolvable<ClientInterface> $client
60+
*
5861
* @psalm-mutation-free
5962
*/
60-
public function __construct(Resolvable $client, Resolvable $requestFactory, Resolvable $streamFactory, DriverConfiguration $config)
61-
{
63+
public function __construct(
64+
Resolvable $client,
65+
Resolvable $requestFactory,
66+
Resolvable $streamFactory,
67+
UriInterface $uri,
68+
AuthenticateInterface $auth,
69+
string $userAgent
70+
) {
6271
$this->client = $client;
6372
$this->requestFactory = $requestFactory;
6473
$this->streamFactory = $streamFactory;
65-
$this->config = $config;
74+
$this->uri = $uri;
75+
$this->auth = $auth;
76+
$this->userAgent = $userAgent;
6677
}
6778

68-
public function acquire(
69-
UriInterface $uri,
70-
AuthenticateInterface $authenticate,
71-
SessionConfiguration $config
72-
): HttpConnection {
73-
$request = $this->requestFactory->resolve()->createRequest('POST', $uri);
79+
public function acquire(SessionConfiguration $config): Generator
80+
{
81+
yield 0.0;
82+
83+
$request = $this->requestFactory->resolve()->createRequest('POST', $this->uri);
7484

7585
$path = $request->getUri()->getPath().'/commit';
76-
$uri = $request->getUri()->withPath($path);
86+
$uri = $this->uri->withPath($path);
7787
$request = $request->withUri($uri);
7888

7989
$body = json_encode([
@@ -84,6 +94,7 @@ public function acquire(
8494
YIELD name, versions, edition
8595
RETURN name, versions, edition
8696
CYPHER
97+
,
8798
],
8899
],
89100
'resultDataContents' => [],
@@ -105,11 +116,11 @@ public function acquire(
105116
$version,
106117
ConnectionProtocol::HTTP(),
107118
$config->getAccessMode(),
108-
$this->config,
109-
new DatabaseInfo($config->getDatabase() ?? '')
119+
new DatabaseInfo($config->getDatabase() ?? ''),
120+
''
110121
);
111122

112-
return new HttpConnection($this->client->resolve(), $config);
123+
return new HttpConnection($this->client->resolve(), $config, $this->auth, $this->userAgent);
113124
}
114125

115126
public function canConnect(UriInterface $uri, AuthenticateInterface $authenticate, ?string $userAgent = null): bool
@@ -123,4 +134,9 @@ public function canConnect(UriInterface $uri, AuthenticateInterface $authenticat
123134
return false;
124135
}
125136
}
137+
138+
public function release(ConnectionInterface $connection): void
139+
{
140+
// Nothing to release in the current HTTP Protocol implementation
141+
}
126142
}

src/Http/HttpDriver.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ private function getHttpConnectionPool(): HttpConnectionPool
159159
Resolvable::once($this->key.':client', fn () => $this->config->getHttpPsrBindings()->getClient()),
160160
$this->resolvableFactory(),
161161
$this->streamFactory(),
162-
$this->config
162+
$this->uri,
163+
$this->auth,
164+
$this->config->getUserAgent()
163165
);
164166
}
165167

src/Http/HttpSession.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Laudis\Neo4j\Http;
1515

1616
use JsonException;
17+
use Laudis\Neo4j\Common\GeneratorHelper;
1718
use Laudis\Neo4j\Common\Resolvable;
1819
use Laudis\Neo4j\Common\TransactionHelper;
1920
use Laudis\Neo4j\Contracts\AuthenticateInterface;
@@ -107,7 +108,9 @@ public function __construct(
107108
public function runStatements(iterable $statements, ?TransactionConfiguration $config = null): CypherList
108109
{
109110
$request = $this->requestFactory->resolve()->createRequest('POST', $this->uri->resolve());
110-
$connection = $this->pool->acquire($request->getUri(), $this->auth, $this->config);
111+
$connection = $this->pool->acquire($this->config);
112+
/** @var HttpConnection */
113+
$connection = GeneratorHelper::getReturnFromGenerator($connection);
111114
$content = HttpHelper::statementsToJson($connection, $this->formatter, $statements);
112115
$request = $this->formatter->decorateRequest($request, $connection);
113116
$request = $this->instantCommitRequest($request)->withBody($this->streamFactory->resolve()->createStream($content));
@@ -170,7 +173,9 @@ public function run(string $statement, iterable $parameters = [], ?TransactionCo
170173
public function beginTransaction(?iterable $statements = null, ?TransactionConfiguration $config = null): UnmanagedTransactionInterface
171174
{
172175
$request = $this->requestFactory->resolve()->createRequest('POST', $this->uri->resolve());
173-
$connection = $this->pool->acquire($request->getUri(), $this->auth, $this->config);
176+
$connection = $this->pool->acquire($this->config);
177+
/** @var HttpConnection */
178+
$connection = GeneratorHelper::getReturnFromGenerator($connection);
174179

175180
$request = $this->formatter->decorateRequest($request, $connection);
176181
$request->getBody()->write(HttpHelper::statementsToJson($connection, $this->formatter, $statements ?? []));

src/Neo4j/Neo4jDriver.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@
1717
use function is_string;
1818
use Laudis\Neo4j\Authentication\Authenticate;
1919
use Laudis\Neo4j\Bolt\Session;
20-
use Laudis\Neo4j\BoltFactory;
21-
use Laudis\Neo4j\Common\Cache;
22-
use Laudis\Neo4j\Common\SemaphoreFactory;
2320
use Laudis\Neo4j\Common\Uri;
2421
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2522
use Laudis\Neo4j\Contracts\DriverInterface;
2623
use Laudis\Neo4j\Contracts\FormatterInterface;
2724
use Laudis\Neo4j\Contracts\SessionInterface;
28-
use Laudis\Neo4j\Databags\ConnectionRequestData;
2925
use Laudis\Neo4j\Databags\DriverConfiguration;
3026
use Laudis\Neo4j\Databags\SessionConfiguration;
3127
use Laudis\Neo4j\Formatter\OGMFormatter;
@@ -44,7 +40,6 @@
4440
final class Neo4jDriver implements DriverInterface
4541
{
4642
private UriInterface $parsedUrl;
47-
private AuthenticateInterface $auth;
4843
private Neo4jConnectionPool $pool;
4944
private FormatterInterface $formatter;
5045

@@ -55,12 +50,10 @@ final class Neo4jDriver implements DriverInterface
5550
*/
5651
public function __construct(
5752
UriInterface $parsedUrl,
58-
AuthenticateInterface $auth,
5953
Neo4jConnectionPool $pool,
6054
FormatterInterface $formatter
6155
) {
6256
$this->parsedUrl = $parsedUrl;
63-
$this->auth = $auth;
6457
$this->pool = $pool;
6558
$this->formatter = $formatter;
6659
}
@@ -88,13 +81,9 @@ public static function create($uri, ?DriverConfiguration $configuration = null,
8881
$configuration ??= DriverConfiguration::default();
8982
$authenticate ??= Authenticate::fromUrl($uri);
9083

91-
/**
92-
* @psalm-suppress MixedReturnTypeCoercion
93-
* @psalm-suppress InvalidArgument
94-
*/
84+
/** @psalm-suppress InvalidArgument */
9585
return new self(
9686
$uri,
97-
$authenticate,
9887
Neo4jConnectionPool::create($uri, $authenticate, $configuration),
9988
$formatter ?? OGMFormatter::create(),
10089
);

0 commit comments

Comments
 (0)