specialweb/gremlin-dsl

Gremlin DSL PHP集成

1.0.1 2022-10-05 14:52 UTC

README

PHPCS PHPUnit License Downloads Latest version codecov

信息

由于原始仓库已停止维护,这是继续工作的包。

简介

Gremlin是由Apache TinkerPop开发的一种图遍历语言。

许多图数据库供应商,如Neo4jAzure CosmosAWS Neptune许多其他,都支持Gremlin。

此包为PHP应用程序提供Gremlin的基本集成。

此版本基于TinkerPop v3.6.1构建。

安装

composer require specialweb/gremlin-dsl

配置

此包提供了一些配置选项的静态配置类

您可以从数组中配置它

use SpecialWeb\GremlinDSL\Configuration;

/** @var \Brightzone\GremlinDriver\Connection $connection */
$connection = null;

Configuration::fromConfig([
    'sendClosure' => function (string $traversalString) use ($connection) {
         return $connection->send($traversalString);
     },
    'enableShortFunctions' => true,
]);

或直接设置所需的设置

use SpecialWeb\GremlinDSL\Configuration;

/** @var \Brightzone\GremlinDriver\Connection $connection */
$connection = null;

Configuration::getInstance()
    ->setSendClosure(function (string $traversalString) use ($connection) {
        return $connection->send($traversalString);
    })
    ->enableShortFunctions()
;

使用方法

只需安装此包即可开始遍历。

<?php
require_once 'vendor/autoload.php';

echo \SpecialWeb\GremlinDSL\Traversal\GraphTraversal::g()
    ->V(1)->out('knows')->has('age', new \SpecialWeb\GremlinDSL\Traversal\Predicates\Gt(30))->values('name');
# g.V(1).out("knows").has("age", gt(30)).values("name")

发送图遍历字符串

此包提供了一个伪send步骤。

您可以为send步骤全局配置一个闭包,或为每个调用提供它。

<?php
require_once 'vendor/autoload.php';

use SpecialWeb\GremlinDSL\Configuration;

/** @var \Brightzone\GremlinDriver\Connection $connection */
$connection = null;
$sendClosure = function (string $traversalString) use ($connection) {
    return $connection->send($traversalString);
};

Configuration::getInstance()->setSendClosure($sendClosure);
g()->V(1)->out("knows")->has("age", gt(30))->values("name")->send();

# or

g()->V(1)->out("knows")->has("age", gt(30))->values("name")->send($sendClosure);

您也可以提供一个SendClosureInterface的实例。

use SpecialWeb\GremlinDSL\Traversal\SendClosureInterface;
use SpecialWeb\GremlinDSL\Traversal\GraphTraversalInterface;

class SendClosure implements SendClosureInterface
{
    public function __invoke(GraphTraversalInterface $graphTraversal, string $traversalString) {
        // handle the send
    }
}

简短函数

简短函数简化了图遍历生成和谓词的使用。

您需要启用GREMLIN_DSL_REGISTER_GLOBAL_FUNCTIONS,手动加载例如resources/predicates.php或调用Configuration::enableShortFunctions()来使简短函数可用。

<?php

require_once 'vendor/autoload.php';

\SpecialWeb\GremlinDSL\Configuration::getInstance()->enableShortFunctions();
g()->V(1)->out('knows')->has('age', gt(30))->values('name');
# g.V(1).out("knows").has("age", gt(30)).values("name")

使用GREMLIN_DSL_REGISTER_GLOBAL_FUNCTIONS常量

<?php

define('GREMLIN_DSL_REGISTER_GLOBAL_FUNCTIONS', true);
require_once 'vendor/autoload.php';

# With GREMLIN_DSL_REGISTER_GLOBAL_FUNCTIONS enabled:
g()->V(1)->out('knows')->has('age', gt(30))->values('name');
# g.V(1).out("knows").has("age", gt(30)).values("name")

开发

DSL生成

DSL生成基于java基础类

要(重新)生成DSL,只需调用make generate,它将首先生成JSON方法结构,然后生成PHP文件。

仅生成JSON

只需调用make generate-jsonmvn -f generator -P glv-json compile

仅生成PHP

例如,要调整PHP文件生成,您可以调用php generate.php [dsl:generate [<in-file>]]make generate-php