exprml/exprml-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

exprml/exprml-php

Composer 安装命令:

composer require exprml/exprml-php

包简介

The ExprML Interpreter in PHP

README 文档

README

exprml-php is a PHP library implementing an ExprML interpreter. The ExprML is a programming language that can evaluate expressions represented in JSON (and JSON-compatible YAML).

The ExprML language specification is available at https://github.com/exprml/exprml-language .

Installation

composer require exprml/exprml-php
composer dump-autoload

Example

Evaluate an expression

<?php

use Exprml\Decoder;
use Exprml\Encoder;
use Exprml\Evaluator;
use Exprml\Evaluator\Config;
use Exprml\Parser;
use Exprml\PB\Exprml\V1\DecodeInput;
use Exprml\PB\Exprml\V1\EncodeInput;
use Exprml\PB\Exprml\V1\EvaluateInput;
use Exprml\PB\Exprml\V1\ParseInput;

require 'vendor/autoload.php';

// Decode the input source code.
$decodeResult = (new Decoder())
    ->decode((new DecodeInput())->setText("cat: ['`Hello`', '`, `', '`ExprML`', '`!`']"));

// Parse AST from the decoded value.
$parseResult = (new Parser())
    ->parse((new ParseInput())->setValue($decodeResult->getValue()));

// Evaluate the parsed AST to get the result value.
$evaluator = new Evaluator(new Config());
$evaluateResult = $evaluator
    ->evaluate((new EvaluateInput())->setExpr($parseResult->getExpr()));

// Encode the evaluated result to get the final output.
$encodeResult = (new Encoder())
    ->encode((new EncodeInput())->setValue($evaluateResult->getValue()));

printf($encodeResult->getText() . "\n");
// => Hello, ExprML!

Call PHP functions from ExprML

<?php

use Exprml\Decoder;
use Exprml\Encoder;
use Exprml\Evaluator;
use Exprml\Evaluator\Config;
use Exprml\Parser;
use Exprml\PB\Exprml\V1\DecodeInput;
use Exprml\PB\Exprml\V1\EncodeInput;
use Exprml\PB\Exprml\V1\EvaluateInput;
use Exprml\PB\Exprml\V1\EvaluateOutput;
use Exprml\PB\Exprml\V1\Expr\Path;
use Exprml\PB\Exprml\V1\ParseInput;
use Exprml\PB\Exprml\V1\Value;

require 'vendor/autoload.php';

$decodeResult = (new Decoder())
    ->decode((new DecodeInput())->setText('$hello: { $name: "`ExprML Extension`" }'));
$parseResult = (new Parser())
    ->parse((new ParseInput())->setValue($decodeResult->getValue()));

$config = (new Config())->setExtension([
    // Define an extension function named $hello, which takes an argument $name and returns a greeting string.
    '$hello' => function (Path $path, array $args) {
        /** @var Value $name */
        $name = $args['$name'];
        $ret = (new Value())
            ->setType(Value\Type::STR)
            ->setStr('Hello, ' . $name->getStr() . '!');
        return (new EvaluateOutput())->setValue($name);
    },
]);

$evaluateResult = (new Evaluator($config))
    ->evaluate((new EvaluateInput())->setExpr($parseResult->getExpr()));
$encodeResult = (new Encoder())
    ->encode((new EncodeInput())->setValue($evaluateResult->getValue()));
printf($encodeResult->getText() . "\n");
// => 'Hello, ExprML Extension!'

Hook PHP functions before and after each evaluation of nested expressions

<?php

use Exprml\Decoder;
use Exprml\Evaluator;
use Exprml\Evaluator\Config;
use Exprml\Parser;
use Exprml\Path;
use Exprml\PB\Exprml\V1\DecodeInput;
use Exprml\PB\Exprml\V1\EvaluateInput;
use Exprml\PB\Exprml\V1\EvaluateOutput;
use Exprml\PB\Exprml\V1\ParseInput;
use Exprml\PB\Exprml\V1\Value;

require 'vendor/autoload.php';

$decodeResult = (new Decoder())
    ->decode((new DecodeInput())->setText("cat: ['`Hello`', '`, `', '`ExprML`', '`!`']"));
$parseResult = (new Parser())
    ->parse((new ParseInput())->setValue($decodeResult->getValue()));

$config = (new Config())
    ->setBeforeEvaluate(
        /* Hook a function before the evaluation of each expression. */
        function (EvaluateInput $input) {
            printf("before:\t%s\n", Path::format($input->getExpr()->getPath()));
        })
    ->setAfterEvaluate(
        /* Hook a function after the evaluation of each expression. */
        function (EvaluateInput $input, EvaluateOutput $output) {
            printf("after:\t%s --> %s\n",
                Path::format($input->getExpr()->getPath()),
                Value\Type::name($output->getValue()->getType()));
        });

(new Evaluator($config))
    ->evaluate((new EvaluateInput())->setExpr($parseResult->getExpr()));
# =>
# before: /
# before: /cat/0
# after:  /cat/0 --> STR
# before: /cat/1
# after:  /cat/1 --> STR
# before: /cat/2
# after:  /cat/2 --> STR
# before: /cat/3
# after:  /cat/3 --> STR
# after:  / --> STR

API documentation

https://exprml.github.io/exprml-php/docs/api/

Packagist

https://packagist.org/packages/exprml/exprml-php

exprml/exprml-php 适用场景与选型建议

exprml/exprml-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 10 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 exprml/exprml-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 exprml/exprml-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 exprml/exprml-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2024-10-18