This is a Gremlin server client for PHP. It allows you to run gremlin queries against graph databases (including Neo4j, Titan, etc.). You can find a beginner tutorial by reading the Get up and running with Tinkerpop 3 and PHP article.
This driver currently supports TP3+.
For a TP2 compatible php driver please check rexpro-php
Prefered method is through composer.
Either run :
php composer.phar require brightzone/gremlin-php "*"
Or add:
"brightzone/gremlin-php": "*"
to the require
section of your composer.json
file
The Connection class exists within the rexpro
namespace. (history: rexpro used to be the old protocol used by the driver in Tinkerpop2).
require_once('vendor/autoload.php');
use \brightzone\rexpro\Connection;
$db = new Connection;
You can find more information by reading the API.
Here are a few basic usages.
Example 1 :
$db = new Connection;
//you can set $db->timeout = 0.5; if you wish
$db->open('localhost', 'graph');
$result = $db->send('g.V(2)');
//do something with result
$db->close();
Note that "graph" is the name of the graph configured in gremlin-server (not the reference to the traversal which is g = graph.traversal()
)
Example 1 bis (Writing the same with message object) :
$db = new Connection;
//you can set $db->timeout = 0.5; if you wish
$db->open('localhost', 'graph');
$db->message->gremlin = 'g.V(2)';
$result = $db->send(); //automatically fetches the message
//do something with result
$db->close();
Example 2 (with bindings) :
$db = new Connection;
$db->open('localhost:8182', 'graph');
$db->message->bindValue('CUSTO_BINDING', 2);
$result = $db->send('g.V(CUSTO_BINDING)'); //mix between Example 1 and 1B
//do something with result
$db->close();
Example 3 (with session) :
$db = new Connection;
$db->open('localhost:8182');
$db->send('cal = 5+5', 'session');
$result = $db->send('cal', 'session'); // result = [10]
//do something with result
$db->close();
Example 4 (transaction) :
$db = new Connection;
$db->open('localhost:8182','graphT');
$db->transactionStart();
$db->send('n.addVertex("name","michael")');
$db->send('n.addVertex("name","john")');
$db->transactionStop(FALSE); //rollback changes. Set to true to commit.
$db->close();
Note that "graphT" above refers to a graph that supports transactions. And that transactions start a session automatically.
Example 5 (Using message object) :
$message = new Messages;
$message->gremlin = 'g.V()';
$message->op = 'eval';
$message->processor = '';
$message->setArguments([
'language' => 'gremlin-groovy',
// .... etc
]);
$message->registerSerializer('\brightzone\rexpro\serializers\Json');
$db = new Connection;
$db->open();
$result = $db->send($message);
//do something with result
$db->close();
Of course you can affect the current db message in the same manner through $db->message.
This library comes with a Json serializer. Any other serializer that implements SerializerInterface can be added dynamically with:
$db = new Connection;
$serializer = $db->message->getSerializer() ; // returns an instance of the default JSON serializer
echo $serializer->getName(); // JSON
echo $serializer->getMimeType(); // application/json
$db->message->registerSerializer('namespace\to\my\CustomSerializer', TRUE); // sets this as default
$serializer = $db->message->getSerializer(); // returns an instance the CustomSerializer serializer (default)
$serializer = $db->message->getSerializer('application/json'); // returns an instance the JSON serializer
You can add many serializers in this fashion. When gremlin-server responds to your requests, gremlin-client-php will be capable of using the appropriate one to unserialize the message.
You can find the api here.
Neo4J is required for the full test suit. It is not bundled with gremlin-server by default so you will need to manually install it with:
bin/gremlin-server.sh -i org.apache.tinkerpop neo4j-gremlin 3.0.0-incubating
Copy the following files :
cp <gremlin-php-root-dir>/src/tests/gremlin-server-php.yaml <gremlin-server-root-dir>/conf/
cp <gremlin-php-root-dir>/src/tests/neo4j-empty.properties <gremlin-server-root-dir>/conf/
cp <gremlin-php-root-dir>/src/tests/gremlin-php-script.groovy <gremlin-server-root-dir>/scripts/
You will then need to run gremlin-server in the following manner :
bin/gremlin-server.sh conf/gremlin-server-php.yaml
Then run the unit test via:
phpunit --bootstrap src/tests/bootstrap.php src/tests/