laudis / neo4j-php-client
Neo4j-PHP-Client is the most advanced PHP Client for Neo4j
Installs: 357 081
Dependents: 20
Suggesters: 1
Security: 0
Stars: 165
Watchers: 13
Forks: 41
Open Issues: 15
Requires
- php: ^8.1
- ext-json: *
- ext-mbstring: *
- laudis/typed-enum: ^1.3.2
- php-http/discovery: ^1.13
- php-http/message: ^1.0
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0|^2.0
- psr/simple-cache: >=2.0
- stefanak-michal/bolt: ^7.0.1
- symfony/polyfill-php80: ^1.2
Requires (Dev)
- cache/integration-tests: dev-master
- friendsofphp/php-cs-fixer: 3.15.0
- kriswallsmith/buzz: ^1.2
- kubawerlos/php-cs-fixer-custom-fixers: 3.13.*
- monolog/monolog: ^2.2
- nyholm/psr7: ^1.3
- nyholm/psr7-server: ^1.0
- phpunit/phpunit: ^10.0
- psalm/plugin-phpunit: ^0.18
- psr/log: ^1.1
- rector/rector: ^1.0
- symfony/uid: ^5.0
- symfony/var-dumper: ^5.0
- vimeo/psalm: ^5.0
Suggests
- composer-runtime-api: Install composer 2 for auto detection of version in user agent
- ext-bcmath: Needed to implement bolt protocol
- ext-sysvsem: Needed for enabling connection pooling between processes
Provides
- psr/simple-cache-implementation: 2.0|3.0
- dev-main
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.x-dev
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.8.3
- 2.8.2
- 2.8.1
- 2.8.0
- 2.7.0
- 2.6.1
- 2.6.0
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.9
- 2.3.8
- 2.3.7
- 2.3.6
- 2.3.5
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.5.0
- dev-feat/improved-connection-failure-handling
- dev-fix/pass-logger-to-client-builder
- dev-feat/update-bolt-7.1.4
- dev-ci/php-8.3
- dev-fix/slash-crash
- dev-feat/remove-service-containers
- dev-feat/close-pool
- dev-fix/ssl-error
- dev-fix/flaky-enterprise-integration-tests
- dev-bolt-update
- dev-kitlabs-cn-main
- dev-issue-193
- dev-issue-191
- dev-issue-183
- dev-client-enhancement
- dev-updated-documentation
- dev-transaction-client
- dev-testing
- dev-test-simplify
- dev-parameterhelper
- dev-redundant-acquire-bugfix
- dev-readthedocs
- dev-cacheing
- dev-fetchsize
- dev-146-errors-after-upgrading-from-244-to-25-and-higher
- dev-connection-resolver
- dev-graceful-driver-fallback
- dev-bookmarks
- dev-transaction-commit-bug
- dev-timeout-fixes
- dev-nodeid-typehint
- dev-dump-errors
- dev-suggestions-wgevaert
- dev-regression-fixes
- dev-skippable-results
This package is auto-updated.
Last update: 2024-11-25 11:22:09 UTC
README
Control to worlds' most powerful graph database
- Pick and choose your drivers with easy configuration
- Intuitive API
- Extensible
- Designed, built and tested under close supervision with the official neo4j driver team
- Validated with testkit
- Fully typed with psalm
- Bolt, HTTP and auto routed drivers available
See the driver in action
- An implementation of the class movie database. It uses Slim and neo4j-php-client to build an API for the classic movie's example of neo4j.
- An complete implementation of the realworld example. It uses Laravel to implement the real world example project, the mother of all demo apps.
- The friends api for the world's most corny example project leveraging the power of Neo4j.
For some more detailed write-ups you can refer to these blogposts:
- How to build a JSON RESTful API with Neo4j, PHP and Open API
- Building a Web App with Neo4j, AuraDB and PHP
- Connect to Neo4j with PHP
- Enterprise level PHP and Neo4j
Or watch any of these videos.
Start your driving experience in three easy steps
Step 1: install via composer
composer require laudis/neo4j-php-client
Find more details here
Step 2: create a client
use Laudis\Neo4j\Authentication\Authenticate; use Laudis\Neo4j\ClientBuilder; $client = ClientBuilder::create() ->withDriver('bolt', 'bolt+s://user:password@localhost') // creates a bolt driver ->withDriver('https', 'https://test.com', Authenticate::basic('user', 'password')) // creates an http driver ->withDriver('neo4j', 'neo4j://neo4j.test.com?database=my-database', Authenticate::oidc('token')) // creates an auto routed driver with an OpenID Connect token ->withDefaultDriver('bolt') ->build();
You have now created a client with bolt, HTTPS and neo4j drivers. The default driver that the client will use is bolt.
Read more about the URLs and how to use them to configure drivers here.
Step 3: run a transaction
use Laudis\Neo4j\Contracts\TransactionInterface; $result = $client->writeTransaction(static function (TransactionInterface $tsx) { $result = $tsx->run('MERGE (x {y: "z"}:X) return x'); return $result->first()->get('x')['y']; }); echo $result; // echos 'z'
Decide how to send your Cypher queries
You can control the driver using three different approaches:
- Auto committed queries (easiest and most intuitive)
- Transaction functions (most portable)
- Unmanaged transactions (for the highest degree of control)
Auto committed queries
Auto committed queries are the most straightforward and most intuitive but have many drawbacks when running complex business logic or within a high availability environment.
Run a simple cypher query
$client->run( 'MERGE (user {email: $email})', //The query is a required parameter ['email' => 'abc@hotmail.com'], //Requests can be optionally added 'backup' //The default connection can be overridden );
Run a statement object:
use Laudis\Neo4j\Databags\Statement; $statement = new Statement('MERGE (user {email: $email})', ['email' => 'abc@hotmail.com']); $client->runStatement($statement, 'default');
Running multiple queries at once
The runStatements
method will run all the statements at once. This method is an essential tool to reduce the number of database calls, especially when using the HTTP protocol.
use Laudis\Neo4j\Databags\Statement; $results = $client->runStatements([ Statement::create('MATCH (x) RETURN x LIMIT 100'), Statement::create('MERGE (x:Person {email: $email})', ['email' => 'abc@hotmail.com']) ]);
Transaction functions
Transaction functions are the de facto standard when using the driver. It is the most portable as it is resistant to a lot of the pitfalls when first developing with high availability solutions such as Neo4j aura or a cluster.
The driver manages transaction functions:
- It re-executes the function in case of a transient error.
- It commits the transaction on successful execution
- It rolls back the transaction in case of a timeout.
- It routes the execution to a relevant follower or leader server when the neo4j protocol is enabled.
ATTENTION: Because of the automatic retry functionality, the function should produce the same result on subsequent recalls, or in more technical terms: should be idempotent. Always remember this when designing the execution logic within the function.
Some examples:
use Laudis\Neo4j\Contracts\TransactionInterface; // Do a simple merge and return the result $result = $client->writeTransaction(static function (TransactionInterface $tsx) { $result = $tsx->run('MERGE (x {y: "z"}:X) return x'); return $result->first()->get('x')['y']; }); // Will result in an error $client->readTransaction(static function (TransactionInterface $tsx) { $tsx->run('MERGE (x {y: "z"}:X) return x'); }); // This is a poorly designed transaction function $client->writeTransaction(static function (TransactionInterface $tsx) use ($externalCounter) { $externalCounter->incrementNodesCreated(); $tsx->run('MERGE (x {y: $id}:X) return x', ['id' => Uuid::v4()]); }); // This achieves the same effect but is safe in case it should be retried. The function is now idempotent. $id = Uuid::v4(); $client->writeTransaction(static function (TransactionInterface $tsx) use ($id) { $tsx->run('MERGE (x {y: $id}:X) return x', ['id' => $id]); }); $externalCounter->incrementNodesCreated();
Unmanaged transactions
If you need lower-level access to the drivers' capabilities, then you want unmanaged transactions. They allow for completely controllable commits and rollbacks.
Opening a transaction
The beginTransaction
method will start a transaction with the relevant driver.
use Laudis\Neo4j\Databags\Statement; $tsx = $client->beginTransaction( // This is an optional set of statements to execute while opening the transaction [Statement::create('MERGE (x:Person({email: $email})', ['email' => 'abc@hotmail.com'])], 'backup' // This is the optional connection alias );
Note that
beginTransaction
only returns the transaction object, not the results of the provided statements.
Running statements within a transaction
The transaction can run statements just like the client object as long as it is still open.
$result = $tsx->run('MATCH (x) RETURN x LIMIT 100'); $result = $tsx->runStatement(Statement::create('MATCH (x) RETURN x LIMIT 100')); $results = $tsx->runStatements([Statement::create('MATCH (x) RETURN x LIMIT 100')]);
Finish a transaction
Rollback a transaction:
$tsx->rollback();
Commit a transaction:
$tsx->commit([Statement::create('MATCH (x) RETURN x LIMIT 100')]);
Accessing the results
Results are returned in a standard format of rows and columns:
// Results are a CypherList $results = $client->run('MATCH (node:Node) RETURN node, node.id AS id'); // A row is a CypherMap foreach ($results as $result) { // Returns a Node $node = $result->get('node'); echo $node->getProperty('id'); echo $result->get('id'); }
Cypher values and types map to these php types and classes:
(*) These items can also be used as parameters in the bolt protocol and will automatically be converted by the driver, so they can be used in Cypher.
Besides these examples, \DateTimeInterface
will map to DateTimeZoneId
in Cypher. An empty or list-type array
will be converted to a cypher List
, and an associative array
will be converted to a map
.
(**) A point can be one of four types implementing PointInterface: \Laudis\Neo4j\Types\CartesianPoint
\Laudis\Neo4j\Types\Cartesian3DPoint
\Laudis\Neo4j\Types\WGS84Point
\Laudis\Neo4j\Types\WGS843DPoint
Diving Deeper
Differentiating between parameter type
Cypher has lists and maps. This notion can be problematic as the standard php arrays encapsulate both. When you provide an empty array as a parameter, it will be impossible to determine an empty list or map.
The ParameterHelper
class is the ideal companion for this:
use Laudis\Neo4j\ParameterHelper; $client->run('MATCH (x) WHERE x.slug in $listOrMap RETURN x', ['listOrMap' => ParameterHelper::asList([])]); // will return an empty CypherList $client->run('MATCH (x) WHERE x.slug in $listOrMap RETURN x', ['listOrMap' => ParameterHelper::asMap([])]); // will error $client->run('MATCH (x) WHERE x.slug in $listOrMap RETURN x', ['listOrMap' => []]); // will return an empty CypherList
Version compatibility matrix
Neo4j Feature Support
In-depth requirements
- PHP >= 7.4
- A Neo4j database (minimum version 3.5)
- ext-bcmath *
- ext-json **
- ext-sockets ***
(*) Needed to implement the bolt protocol
(**) Needed to implement the http protocol
(***) Can be installed for optimal bolt protocol performance
If you plan on using the HTTP drivers, make sure you have psr-7, psr-17 and psr-18 implementations included into the project. If you don't have any, you can install them via composer:
composer require nyholm/psr7 nyholm/psr7-server kriswallsmith/buzz
Result formats/hydration
In order to make the results of the bolt protocol and the http uniform, the driver provides result formatters (aka hydrators). The client is configurable with these formatters. You can even implement your own.
The default formatter is the \Laudis\Neo4j\Formatters\OGMFormatter
, which is explained extensively in the result format section.
The driver provides three formatters by default, which are all found in the Formatter namespace:
\Laudis\Neo4j\Formatter\BasicFormatter
which erases all the Cypher types and simply returns every value in the resulting map as a scalar, null or array value.\Laudis\Neo4j\Formatter\OGMFormatter
which maps the cypher types to php types as explained here.\Laudis\Neo4j\Formatter\SummarizedResultFormatter
which decorates any formatter and adds an extensive result summary.
The client builder provides an easy way to change the formatter:
$client = \Laudis\Neo4j\ClientBuilder::create() ->withFormatter(\Laudis\Neo4j\Formatter\SummarizedResultFormatter::create()) ->build(); /** * The client will now return a result, decorated with a summary. * * @var \Laudis\Neo4j\Databags\SummarizedResult $results */ $summarisedResult = $client->run('MATCH (x) RETURN x'); // The summary contains extensive information such as counters for changed values in the database, // information on the database, potential notifications, timing, a (profiled) plan, the type of query // and information on the server itself. $summary = $summarisedResult->getSummary(); // The result is exactly the same as the default. $result = $summarisedResult->getResult();
In order to use a custom formatter, implement the Laudis\Neo4j\Contracts\FormatterInterface
and provide it when using the client builder.
Concepts
The driver API described here is the main target of the driver. Because of this, the client is nothing more than a driver manager. The driver creates sessions. A session runs queries through a transaction.
Because of this behaviour, you can access each concept starting from the client like this:
use Laudis\Neo4j\ClientBuilder; // A builder is responsible for configuring the client on a high level. $builder = ClientBuilder::create(); // A client manages the drivers as configured by the builder. $client = $builder->build(); // A driver manages connections and sessions. $driver = $client->getDriver('default'); // A session manages transactions. $session = $driver->createSession(); // A transaction is the atomic unit of the driver where are the cypher queries are chained. $transaction = $session->beginTransaction(); // A transaction runs the actual queries $transaction->run('MATCH (x) RETURN count(x)');
If you need complete control, you can control each object with custom configuration objects.
Client
A client manages drivers and routes the queries to the correct drivers based on preconfigured aliases.
Driver
The driver object is the thread-safe backbone that gives access to Neo4j. It owns a connection pool and can spawn sessions for carrying out work.
use Laudis\Neo4j\Basic\Driver; use Laudis\Neo4j\Databags\DriverConfiguration; $driver = Driver::create( uri: 'neo4j://user:mypassword@Localhost:7687', configuration: DriverConfiguration::create()->withUserAgent('MyApp/1.0.0') );
Session
Sessions are lightweight containers for causally chained sequences of transactions. They borrow connections from the connection pool as required and chain transactions using bookmarks.
use Laudis\Neo4j\Databags\SessionConfiguration; use Laudis\Neo4j\Enum\AccessMode; $session = $driver->createSession(SessionConfiguration::create() ->withDatabase('my-database') ->withAccessMode(AccessMode::READ()) );
Transaction
Transactions are atomic units of work that may contain one or more query. Each transaction is bound to a single connection and is represented in the causal chain by a bookmark.
Statement
Queries are executable units within transactions and consist of a Cypher string and a keyed parameter set. Each query outputs a result that may contain zero or more records.
Result
A result contains the output from a query, made up of header metadata, content records and summary metadata. In Neo4j 4.0 and above, applications have control over the flow of result data.
In-depth configuration
Url Schemes
The URL scheme is the easiest way to configure the driver.
Configuration format:
'<scheme>://<user>:<password>@<host>:<port>?database=<database>'
Default configuration:
bolt://localhost:7687?database=neo4j
Scheme configuration matrix
This library supports three drivers: bolt, HTTP and neo4j. The scheme part of the url determines the driver.
Configuration objects
A driver, session and transaction can be configured using configuration objects. An overview of the configuration options can be found here:
Code Example:
use \Laudis\Neo4j\Databags\DriverConfiguration; use Laudis\Neo4j\Databags\SessionConfiguration; use Laudis\Neo4j\Databags\TransactionConfiguration; $client = \Laudis\Neo4j\ClientBuilder::create() ->withDefaultDriverConfiguration(DriverConfiguration::default()->withUserAgent('MyApp/1.0.0')) ->withDefaultSessionConfiguration(SessionConfiguration::default()->withDatabase('app-database')) ->withDefaultTransactionConfiguration(TransactionConfiguration::default()->withTimeout(5.0)) ->build(); // The client will run the query on a driver with the provided config, // which spawns a session with the provided session config // and runs the query in a transaction with the provided transaction config $client->run('MATCH (x) RETURN count(x) AS count'); // More granular control can be achieved by requesting the concepts yourself: $tsx = $client->getDriver('default') ->createSession(SessionConfiguration::default()->withDatabase('management-database')) ->beginTransaction(null, TransactionConfiguration::default()->withTimeout(200)); $tsx->run('SOME REALLY LONG MANAGEMENT QUERY'); $tsx->commit();