rnd/gremlin-dsl

该软件包已被废弃,不再维护。作者建议使用 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

配置

此软件包提供了一个静态的 Configuration 类,其中包含一些配置选项。

选项 范围 类型 默认值 描述
GREMLIN_DSL_REGISTER_GLOBAL_FUNCTIONS 常量 布尔值 false 全局注册 Gremlin 的 短函数
例如,全局的 g 函数可用于启动遍历。
enableShortFunctions 配置 布尔值 false 全局注册 Gremlin 的 短函数
例如,全局的 g 函数可用于启动遍历。
sendClosure 配置 闭包 null 注册全局回调以用于 伪发送步骤

您可以从数组中进行配置

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 步骤。

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

<?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