Skip to content

Commit c74fbc2

Browse files
committed
further integrations
1 parent d557e05 commit c74fbc2

12 files changed

+155
-90
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"nyholm/psr7": "^1.3",
5050
"nyholm/psr7-server": "^1.0",
5151
"kriswallsmith/buzz": "^1.2",
52-
"vimeo/psalm": "4.23.0",
52+
"vimeo/psalm": "4.26.0",
5353
"friendsofphp/php-cs-fixer": "3.8.0",
5454
"psalm/plugin-phpunit": "^0.15.1",
5555
"monolog/monolog": "^2.2",

src/Bolt/BoltDriver.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Laudis\Neo4j\Contracts\DriverInterface;
2525
use Laudis\Neo4j\Contracts\FormatterInterface;
2626
use Laudis\Neo4j\Contracts\SessionInterface;
27+
use Laudis\Neo4j\Databags\ConnectionRequestData;
2728
use Laudis\Neo4j\Databags\DriverConfiguration;
2829
use Laudis\Neo4j\Databags\SessionConfiguration;
2930
use Laudis\Neo4j\Formatter\OGMFormatter;
@@ -53,7 +54,7 @@ final class BoltDriver implements DriverInterface
5354
public function __construct(
5455
UriInterface $parsedUrl,
5556
ConnectionPool $pool,
56-
FormatterInterface $formatter
57+
FormatterInterface $formatter,
5758
) {
5859
$this->parsedUrl = $parsedUrl;
5960
$this->pool = $pool;
@@ -80,6 +81,7 @@ public static function create($uri, ?DriverConfiguration $configuration = null,
8081

8182
$configuration ??= DriverConfiguration::default();
8283
$authenticate ??= Authenticate::fromUrl($uri);
84+
$sslFactory = new SslConfigurationFactory();
8385

8486
// Because interprocess switching of connections between PHP sessions is impossible,
8587
// we have to build a key to limit the amount of open connections, potentially between ALL sessions.
@@ -88,24 +90,36 @@ public static function create($uri, ?DriverConfiguration $configuration = null,
8890
// The combination is between the server and the user agent as it most closely resembles an "application"
8991
// connecting to a server. The application thus supports multiple authentication methods, but they have
9092
// to be shared between the same connection pool.
91-
$key = $uri->getHost().':'.$uri->getPort().':'.$config->getUserAgent();
93+
$key = $uri->getHost().':'.($uri->getPort() ?? '').':'.$configuration->getUserAgent();
94+
9295
if (extension_loaded('ext-sysvsem')) {
93-
$semaphore = SysVSemaphore::create($key, $config->getMaxPoolSize());
96+
$semaphore = SysVSemaphore::create($key, $configuration->getMaxPoolSize());
9497
} else {
95-
$semaphore = SingleThreadedSemaphore::create($key, $config->getMaxPoolSize());
98+
$semaphore = SingleThreadedSemaphore::create($key, $configuration->getMaxPoolSize());
9699
}
97100

101+
$pool = new ConnectionPool(
102+
$semaphore,
103+
SystemWideConnectionFactory::getInstance(),
104+
new ConnectionRequestData(
105+
$uri,
106+
$authenticate,
107+
$configuration->getUserAgent(),
108+
$sslFactory->create($uri, $configuration->getSslConfiguration())
109+
)
110+
);
111+
98112
if ($formatter !== null) {
99113
return new self(
100114
$uri,
101-
new ConnectionPool($uri, $configuration, $authenticate),
115+
$pool,
102116
$formatter
103117
);
104118
}
105119

106120
return new self(
107121
$uri,
108-
new ConnectionPool($uri, $configuration, $authenticate),
122+
$pool,
109123
OGMFormatter::create(),
110124
);
111125
}
@@ -122,12 +136,7 @@ public function createSession(?SessionConfiguration $config = null): SessionInte
122136
$sessionConfig = $sessionConfig->merge($config);
123137
}
124138

125-
return new Session(
126-
$sessionConfig,
127-
$this->pool,
128-
$this->formatter,
129-
$this->parsedUrl
130-
);
139+
return new Session($sessionConfig, $this->pool, $this->formatter);
131140
}
132141

133142
public function verifyConnectivity(): bool

src/Bolt/ConnectionPool.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@
2020
use Laudis\Neo4j\Contracts\SemaphoreInterface;
2121
use Laudis\Neo4j\Databags\ConnectionRequestData;
2222
use Laudis\Neo4j\Databags\SessionConfiguration;
23+
use Bolt\protocol\V3;
24+
use Laudis\Neo4j\Bolt\Connection;
2325
use function method_exists;
2426
use function microtime;
2527
use function shuffle;
2628

2729
/**
28-
* @template T
29-
* @implements ConnectionPoolInterface<T>
30+
* @implements ConnectionPoolInterface<array{0: V3, 1: Connection}>
3031
*/
3132
final class ConnectionPool implements ConnectionPoolInterface
3233
{
3334
private SemaphoreInterface $semaphore;
34-
35-
/** @var list<ConnectionInterface<T>> */
35+
/** @var list<ConnectionInterface<array{0: V3, 1: Connection}>> */
3636
private array $activeConnections = [];
37-
/** @var ConnectionFactoryInterface<T> */
37+
/** @var ConnectionFactoryInterface<array{0: V3, 1: Connection}> */
3838
private ConnectionFactoryInterface $factory;
3939
private ConnectionRequestData $data;
4040

4141
/**
42-
* @param ConnectionFactoryInterface<T> $factory
42+
* @param ConnectionFactoryInterface<array{0: V3, 1: Connection}> $factory
4343
*/
4444
public function __construct(SemaphoreInterface $semaphore, ConnectionFactoryInterface $factory, ConnectionRequestData $data)
4545
{
@@ -57,6 +57,7 @@ public function acquire(SessionConfiguration $config): Generator
5757
// If the generator is valid, it means we are waiting to acquire a new connection.
5858
// This means we can use this time to check if we can reuse a connection or should throw a timeout exception.
5959
while ($generator->valid()) {
60+
/** @var bool $continue */
6061
$continue = yield microtime(true) - $start;
6162
$generator->send($continue);
6263
if ($continue === false) {
@@ -95,7 +96,7 @@ public function release(ConnectionInterface $connection): void
9596
}
9697

9798
/**
98-
* @return ConnectionInterface<T>|null
99+
* @return ConnectionInterface<array{0: V3, 1: Connection}>|null
99100
*/
100101
private function returnAnyAvailableConnection(SessionConfiguration $config): ?ConnectionInterface
101102
{

src/Bolt/Session.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ final class Session implements SessionInterface
4747
/**
4848
* @psalm-readonly
4949
*
50-
* @var BoltConnectionPool|Neo4jConnectionPool
50+
* @var ConnectionPool|Neo4jConnectionPool
5151
*/
5252
private ConnectionPoolInterface $pool;
5353
/**
@@ -57,10 +57,6 @@ final class Session implements SessionInterface
5757
*/
5858
private FormatterInterface $formatter;
5959
/** @psalm-readonly */
60-
private UriInterface $uri;
61-
/** @psalm-readonly */
62-
private AuthenticateInterface $auth;
63-
/** @psalm-readonly */
6460
private BookmarkHolder $bookmarkHolder;
6561

6662
/**
@@ -72,13 +68,11 @@ final class Session implements SessionInterface
7268
public function __construct(
7369
SessionConfiguration $config,
7470
ConnectionPoolInterface $pool,
75-
FormatterInterface $formatter,
76-
UriInterface $uri
71+
FormatterInterface $formatter
7772
) {
7873
$this->config = $config;
7974
$this->pool = $pool;
8075
$this->formatter = $formatter;
81-
$this->uri = $uri;
8276
$this->bookmarkHolder = new BookmarkHolder(Bookmark::from($config->getBookmarks()));
8377
}
8478

src/Bolt/SocketConnectionFactory.php

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

1616
use Bolt\connection\Socket;
1717
use Laudis\Neo4j\Contracts\BasicConnectionFactoryInterface;
18+
use Laudis\Neo4j\Databags\TransactionConfiguration;
1819

1920
final class SocketConnectionFactory implements BasicConnectionFactoryInterface
2021
{
@@ -31,7 +32,7 @@ public function create(UriConfiguration $config): Connection
3132
return $this->factory->create($config);
3233
}
3334

34-
$connection = new Socket($config->getHost(), $config->getPort(), $config->getTimeout());
35+
$connection = new Socket($config->getHost(), $config->getPort() ?? 7687, $config->getTimeout() ?? TransactionConfiguration::DEFAULT_TIMEOUT);
3536

3637
return new Connection($connection, '');
3738
}

src/Bolt/StreamConnectionFactory.php

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

1616
use Bolt\connection\StreamSocket;
1717
use Laudis\Neo4j\Contracts\BasicConnectionFactoryInterface;
18+
use Laudis\Neo4j\Databags\TransactionConfiguration;
1819

1920
final class StreamConnectionFactory implements BasicConnectionFactoryInterface
2021
{
2122
public function create(UriConfiguration $config): Connection
2223
{
23-
$connection = new StreamSocket($config->getHost(), $config->getPort(), $config->getTimeout());
24+
$connection = new StreamSocket($config->getHost(), $config->getPort() ?? 7687, $config->getTimeout() ?? TransactionConfiguration::DEFAULT_TIMEOUT);
2425
if ($config->getSslLevel() !== '') {
2526
$connection->setSslContextOptions($config->getSslConfiguration());
2627
}

src/Bolt/UriConfiguration.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ final class UriConfiguration
1717
{
1818
private string $host;
1919
private ?int $port;
20+
/** @var ''|'s'|'ssc' */
2021
private string $sslLevel;
2122
private array $sslConfiguration;
2223
private ?float $timeout;
2324

25+
/**
26+
* @param string $host
27+
* @param int|null $port
28+
* @param ''|'s'|'ssc' $sslLevel
29+
* @param array $sslConfiguration
30+
* @param float|null $timeout
31+
*/
2432
public function __construct(string $host, ?int $port, string $sslLevel, array $sslConfiguration, ?float $timeout)
2533
{
2634
$this->host = $host;

src/Common/ConnectionConfiguration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ final class ConnectionConfiguration
3232
/** @var 's'|'ssc'|'' */
3333
private string $encryptionLevel;
3434

35+
/**
36+
* @param ''|'s'|'ssc' $encryptionLevel
37+
*/
3538
public function __construct(
3639
string $serverAgent,
3740
UriInterface $serverAddress,

src/Common/SingleThreadedSemaphore.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the Neo4j PHP Client and Driver package.
57
*
@@ -19,6 +21,7 @@ class SingleThreadedSemaphore implements SemaphoreInterface
1921
{
2022
private int $max;
2123
private int $amount = 0;
24+
/** @var array<string, self> */
2225
private static array $instances = [];
2326

2427
private function __construct(int $max)
@@ -39,6 +42,7 @@ public function wait(): Generator
3942
{
4043
$start = microtime(true);
4144
while ($this->amount >= $this->max) {
45+
/** @var bool $continue */
4246
$continue = yield $start - microtime(true);
4347
if (!$continue) {
4448
return;

src/Common/SysVSemaphore.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function wait(): Generator
4545
{
4646
$start = microtime(true);
4747
while (!sem_acquire($this->semaphore, true)) {
48+
/** @var bool $continue */
4849
$continue = yield $start - microtime(true);
4950
if (!$continue) {
5051
return;

0 commit comments

Comments
 (0)