Skip to content

Commit 725b58e

Browse files
committed
created basic driver for easy IDE integration
1 parent 4d98aff commit 725b58e

File tree

6 files changed

+365
-2
lines changed

6 files changed

+365
-2
lines changed

src/Basic/Client.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Laudis Neo4j package.
5+
*
6+
* (c) Laudis technologies <http://laudis.tech>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Laudis\Neo4j\Basic;
13+
14+
use Laudis\Neo4j\Contracts\ClientInterface;
15+
use Laudis\Neo4j\Contracts\DriverInterface;
16+
use Laudis\Neo4j\Databags\Statement;
17+
use Laudis\Neo4j\Databags\SummarizedResult;
18+
use Laudis\Neo4j\Databags\TransactionConfiguration;
19+
use Laudis\Neo4j\Types\CypherList;
20+
use Laudis\Neo4j\Types\CypherMap;
21+
22+
/**
23+
* @implements ClientInterface<SummarizedResult<CypherMap>>
24+
*/
25+
final class Client implements ClientInterface
26+
{
27+
/** @var ClientInterface<SummarizedResult<CypherMap>> */
28+
private ClientInterface $client;
29+
30+
/**
31+
* @param ClientInterface<SummarizedResult<CypherMap>> $client
32+
*/
33+
public function __construct(ClientInterface $client)
34+
{
35+
$this->client = $client;
36+
}
37+
38+
public function run(string $statement, iterable $parameters = [], ?string $alias = null): SummarizedResult
39+
{
40+
return $this->client->run($statement, $parameters, $alias);
41+
}
42+
43+
public function runStatement(Statement $statement, ?string $alias = null): SummarizedResult
44+
{
45+
return $this->client->runStatement($statement, $alias);
46+
}
47+
48+
public function runStatements(iterable $statements, ?string $alias = null): CypherList
49+
{
50+
return $this->client->runStatements($statements, $alias);
51+
}
52+
53+
public function beginTransaction(?iterable $statements = null, ?string $alias = null, ?TransactionConfiguration $config = null): UnmanagedTransaction
54+
{
55+
return new UnmanagedTransaction($this->client->beginTransaction($statements, $alias, $config));
56+
}
57+
58+
/**
59+
* @psalm-mutation-free
60+
*/
61+
public function getDriver(?string $alias): DriverInterface
62+
{
63+
return $this->client->getDriver($alias);
64+
}
65+
66+
public function writeTransaction(callable $tsxHandler, ?string $alias = null, ?TransactionConfiguration $config = null)
67+
{
68+
return $this->client->writeTransaction($tsxHandler, $alias, $config);
69+
}
70+
71+
public function readTransaction(callable $tsxHandler, ?string $alias = null, ?TransactionConfiguration $config = null)
72+
{
73+
return $this->client->readTransaction($tsxHandler, $alias, $config);
74+
}
75+
76+
public function transaction(callable $tsxHandler, ?string $alias = null, ?TransactionConfiguration $config = null)
77+
{
78+
return $this->client->transaction($tsxHandler, $alias, $config);
79+
}
80+
81+
public function verifyConnectivity(?string $driver = null): bool
82+
{
83+
return $this->client->verifyConnectivity($driver);
84+
}
85+
}

src/Basic/Driver.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Laudis Neo4j package.
5+
*
6+
* (c) Laudis technologies <http://laudis.tech>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Laudis\Neo4j\Basic;
13+
14+
use Laudis\Neo4j\Contracts\AuthenticateInterface;
15+
use Laudis\Neo4j\Contracts\DriverInterface;
16+
use Laudis\Neo4j\Databags\DriverConfiguration;
17+
use Laudis\Neo4j\Databags\SessionConfiguration;
18+
use Laudis\Neo4j\Databags\SummarizedResult;
19+
use Laudis\Neo4j\DriverFactory;
20+
use Laudis\Neo4j\Formatter\SummarizedResultFormatter;
21+
use Laudis\Neo4j\Types\CypherMap;
22+
use Psr\Http\Message\UriInterface;
23+
24+
/**
25+
* @implements DriverInterface<SummarizedResult<CypherMap>>
26+
*/
27+
final class Driver implements DriverInterface
28+
{
29+
/** @var DriverInterface DriverInterface<SummarizedResult<CypherMap>> */
30+
private DriverInterface $driver;
31+
32+
/**
33+
* @param DriverInterface<SummarizedResult<CypherMap>> $driver
34+
*
35+
* @psalm-external-mutation-free
36+
*/
37+
public function __construct(DriverInterface $driver)
38+
{
39+
$this->driver = $driver;
40+
}
41+
42+
/**
43+
* @psalm-mutation-free
44+
*/
45+
public function createSession(?SessionConfiguration $config = null): Session
46+
{
47+
return new Session($this->driver->createSession());
48+
}
49+
50+
public function verifyConnectivity(): bool
51+
{
52+
return $this->driver->verifyConnectivity();
53+
}
54+
55+
/**
56+
* @param string|UriInterface $uri
57+
* @pure
58+
*/
59+
public static function create($uri, ?DriverConfiguration $configuration = null, ?AuthenticateInterface $authenticate = null, ?float $socketTimeout = null): self
60+
{
61+
/** @var DriverInterface<SummarizedResult<CypherMap>> */
62+
$driver = DriverFactory::create($uri, $configuration, $authenticate, $socketTimeout, SummarizedResultFormatter::create());
63+
64+
return new self($driver);
65+
}
66+
}

src/Basic/Session.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Laudis Neo4j package.
5+
*
6+
* (c) Laudis technologies <http://laudis.tech>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Laudis\Neo4j\Basic;
13+
14+
use Laudis\Neo4j\Contracts\SessionInterface;
15+
use Laudis\Neo4j\Databags\Statement;
16+
use Laudis\Neo4j\Databags\SummarizedResult;
17+
use Laudis\Neo4j\Databags\TransactionConfiguration;
18+
use Laudis\Neo4j\Types\CypherList;
19+
use Laudis\Neo4j\Types\CypherMap;
20+
21+
/**
22+
* @implements SessionInterface<SummarizedResult<CypherMap>>
23+
*/
24+
final class Session implements SessionInterface
25+
{
26+
/** @var SessionInterface<SummarizedResult<CypherMap>> */
27+
private SessionInterface $session;
28+
29+
/**
30+
* @param SessionInterface<SummarizedResult<CypherMap>> $session
31+
*/
32+
public function __construct(SessionInterface $session)
33+
{
34+
$this->session = $session;
35+
}
36+
37+
public function runStatements(iterable $statements, ?TransactionConfiguration $config = null): CypherList
38+
{
39+
return $this->session->runStatements($statements, $config);
40+
}
41+
42+
public function runStatement(Statement $statement, ?TransactionConfiguration $config = null): SummarizedResult
43+
{
44+
return $this->session->runStatement($statement, $config);
45+
}
46+
47+
public function run(string $statement, iterable $parameters = [], ?TransactionConfiguration $config = null): SummarizedResult
48+
{
49+
return $this->session->run($statement, $parameters, $config);
50+
}
51+
52+
/**
53+
* @deprecated
54+
*/
55+
public function openTransaction(?iterable $statements = null, ?TransactionConfiguration $config = null): UnmanagedTransaction
56+
{
57+
/** @psalm-suppress DeprecatedMethod */
58+
return new UnmanagedTransaction($this->session->openTransaction($statements, $config));
59+
}
60+
61+
public function beginTransaction(?iterable $statements = null, ?TransactionConfiguration $config = null): UnmanagedTransaction
62+
{
63+
return new UnmanagedTransaction($this->session->beginTransaction($statements, $config));
64+
}
65+
66+
public function writeTransaction(callable $tsxHandler, ?TransactionConfiguration $config = null)
67+
{
68+
return $this->session->writeTransaction($tsxHandler, $config);
69+
}
70+
71+
public function readTransaction(callable $tsxHandler, ?TransactionConfiguration $config = null)
72+
{
73+
return $this->session->readTransaction($tsxHandler, $config);
74+
}
75+
76+
public function transaction(callable $tsxHandler, ?TransactionConfiguration $config = null)
77+
{
78+
return $this->session->writeTransaction($tsxHandler, $config);
79+
}
80+
}

src/Basic/UnmanagedTransaction.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Laudis Neo4j package.
5+
*
6+
* (c) Laudis technologies <http://laudis.tech>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Laudis\Neo4j\Basic;
13+
14+
use Laudis\Neo4j\Contracts\UnmanagedTransactionInterface;
15+
use Laudis\Neo4j\Databags\Statement;
16+
use Laudis\Neo4j\Databags\SummarizedResult;
17+
use Laudis\Neo4j\Types\CypherList;
18+
use Laudis\Neo4j\Types\CypherMap;
19+
20+
/**
21+
* @implements UnmanagedTransactionInterface<SummarizedResult<CypherMap>>
22+
*/
23+
final class UnmanagedTransaction implements UnmanagedTransactionInterface
24+
{
25+
/** @var UnmanagedTransactionInterface<SummarizedResult<CypherMap>> */
26+
private UnmanagedTransactionInterface $tsx;
27+
28+
/**
29+
* @param UnmanagedTransactionInterface<SummarizedResult<CypherMap>> $tsx
30+
*/
31+
public function __construct(UnmanagedTransactionInterface $tsx)
32+
{
33+
$this->tsx = $tsx;
34+
}
35+
36+
public function run(string $statement, iterable $parameters = []): SummarizedResult
37+
{
38+
return $this->tsx->run($statement, $parameters);
39+
}
40+
41+
public function runStatement(Statement $statement): SummarizedResult
42+
{
43+
return $this->tsx->runStatement($statement);
44+
}
45+
46+
/**
47+
* @param iterable<Statement> $statements
48+
*
49+
* @return CypherList<SummarizedResult<CypherMap>>
50+
*/
51+
public function runStatements(iterable $statements): CypherList
52+
{
53+
return $this->tsx->runStatements($statements);
54+
}
55+
56+
/**
57+
* @param iterable<Statement> $statements
58+
*
59+
* @return CypherList<SummarizedResult<CypherMap>>
60+
*/
61+
public function commit(iterable $statements = []): CypherList
62+
{
63+
return $this->tsx->commit($statements);
64+
}
65+
66+
public function rollback(): void
67+
{
68+
$this->tsx->rollback();
69+
}
70+
}

src/ClientBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public function addBoltConnection(string $alias, string $url, BoltConfiguration
180180
* @return self<T>
181181
*
182182
* @deprecated
183-
* @see ClientBuilder::withDriver()
183+
* @see Driver::withDriver()
184184
*
185185
* @psalm-suppress DeprecatedClass
186186
*/
@@ -214,7 +214,7 @@ public function addHttpConnection(string $alias, string $url, HttpConfig $config
214214
* @return self<T>
215215
*
216216
* @deprecated
217-
* @see ClientBuilder::withDefaultDriver()
217+
* @see Driver::withDefaultDriver()
218218
* @psalm-mutation-free
219219
*/
220220
public function setDefaultConnection(string $alias): self
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Laudis Neo4j package.
5+
*
6+
* (c) Laudis technologies <http://laudis.tech>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Laudis\Neo4j\Tests\Integration;
13+
14+
use function explode;
15+
use function is_string;
16+
use Laudis\Neo4j\Basic\Driver;
17+
use Laudis\Neo4j\Types\CypherMap;
18+
use PHPUnit\Framework\TestCase;
19+
20+
final class BasicDriverTest extends TestCase
21+
{
22+
/**
23+
* @return list<array{0: string}>
24+
*/
25+
public function getConnections(): array
26+
{
27+
/** @var string|mixed $connections */
28+
$connections = $_ENV['NEO4J_CONNECTIONS'] ?? false;
29+
if (!is_string($connections)) {
30+
$connections = 'bolt://neo4j:test@neo4j,neo4j://neo4j:test@core1,http://neo4j:test@neo4j';
31+
}
32+
33+
$tbr = [];
34+
foreach (explode(',', $connections) as $connection) {
35+
$tbr[] = [$connection];
36+
}
37+
38+
return $tbr;
39+
}
40+
41+
/**
42+
* @dataProvider getConnections
43+
*/
44+
public function testFullWalk(string $connection): void
45+
{
46+
$driver = Driver::create($connection);
47+
48+
$session = $driver->createSession();
49+
50+
$session->run('MATCH (x) DETACH DELETE x');
51+
$session->run('CREATE (x:X {id: 0})');
52+
53+
$id = 1;
54+
$result = $session->run('MATCH (x) RETURN x');
55+
$result->each(static function (CypherMap $map) use (&$id) {
56+
/** @psalm-suppress all */
57+
$id = $map->getAsNode('x')->getProperties()->getAsInt('id');
58+
});
59+
60+
self::assertEquals(0, $id);
61+
}
62+
}

0 commit comments

Comments
 (0)