定制 okvpn/expression-language 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

okvpn/expression-language

Composer 安装命令:

composer require okvpn/expression-language

包简介

Fast powerful PHP expression language based on twig syntax

README 文档

README

This library provides interfaces for safe evaluate expressions, compile them to native PHP code for performance.

Tests Latest Stable Version PHP Version Require

Purpose

It is an alternative to the Symfony Expression Language which allows more operations such as for if etc. You can to implement any logic and execute it safely and quickly. We removed PRINT_TOKEN TEXT_TOKEN from TWIG AST-tree to prevent output, added return statement and made a more improvement.

Features

  • Allow to execution scripts and evaluate expressions.
  • Debugging with PhpStorm twig debugger feature.
  • Support all twig features, like sandbox, custom extension, tokens, functions.
  • Minimum performance overhead - faster that twig.
  • Syntax highlighting is already built into the most popular IDEs.

Example Usage

Base evaluate Example

$lang = new TwigLanguage(options: ['cache' => __DIR__ . '/var/cache/twig']);
$context = ['var1' => 10, 'users' => [1, 2, 5], 'user' => 1];

$lang->evaluate('user in users ? users|length + 1 : var1')

Execute Script

{% set msg = 'New sms' %}
{% set newUsers = '' %}
{% set usersList = [] %}
{% set lastUserId = redis_get('sms-user') %} # redis_get - custom function

{% for user in users %}
    {% if user > lastUserId %}
        {% set newUsers = newUsers ~ user ~ ' ' %}
        {% do redis_set('sms-user', user, 0) %}
        {% set usersList[_key] = user %} # allow set to array 
    {% endif %}
{% endfor %}

{% if newUsers is not empty %}
    {% do telegram_send('chart111', msg ~ "\n" ~ newUsers|trim) %} # telegram_send - custom function
{% endif %}

{% return usersList %} # new return token 
$lang = new TwigLanguage();
$script = <<<TXT
...
{% return 10 + 15 %}
TXT;

var_dump($lang->execute($script, $context));

// execute with template 
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/scripts');
$lang = new TwigLanguage(loader: $loader);
var_dump($lang->execute('test22.twig', $context));

evaluate method - is faster than execute, because we skip additional exception handling and error logic. Also evaluate - accept code without {% token.

Benchmark Test

We use next the test to compare overhead between Symfony Expression Language and this library

$lang = new TwigLanguage(options: [
    'cache' => __DIR__ . '/var/cache/twig',
    'auto_reload' => true,
]);


$expr = 'user == 1 ? var1 + var2 : var1 + 1';
$lang->evaluate($expr, $context);

// Symfony EL 
$sf = new ExpressionLanguage();
$expression = $sf->parse($expr, array_keys($context));
$sf->evaluate($expression, $context);

// Native PHP

$fn = static function ($context) {
    return $context['user'] === 1 ? $context['var1'] + $context['var2'] : $context['var1'] + 1;
};

$t1 = microtime(true);
for ($i = 0; $i < 200000; $i++) {
    // comment line where needed
    $result = $fn($context);
    $result = $sf->evaluate($expression, $context);
    $result = $lang->evaluate($expr, $context);
}

echo (microtime(true) - $t1)/200000 * 1000000 . "\n";

Extends and sandbox mode.

See Twig documentation

Result

Lang Op. Time Overhead cost
Native PHP 0.058µs 0%
Symfony EL 0.461µs +800%
Twig lang 0.187µs +300%

License

MIT License.

okvpn/expression-language 适用场景与选型建议

okvpn/expression-language 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.1k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2023 年 05 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「expression-language」 「safe-eval」 「evaluate-expressions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-22