定制 tourze/symfony-ecol-bundle 二次开发

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

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

tourze/symfony-ecol-bundle

Composer 安装命令:

composer require tourze/symfony-ecol-bundle

包简介

Expression and Condition Operations Language Bundle for Symfony

README 文档

README

English | 中文

Latest Version PHP Version License

Build Status Quality Score Coverage Status Total Downloads

This bundle provides an enhanced Expression Language experience for Symfony applications with additional functions, values, and Chinese syntax support.

Table of Contents

Features

  • Enhanced Symfony Expression Language engine with Chinese syntax support
  • Date and math function providers for expressions
  • Automatic expression validation via Doctrine event subscribers
  • Value providers for common date operations (today, current timestamp, etc.)
  • Support for custom expression functions and value providers
  • Attribute for expression validation on entity properties

Installation

To install the bundle, require it using Composer:

composer require tourze/symfony-ecol-bundle

Register the bundle in your config/bundles.php:

return [
    // ...
    Tourze\EcolBundle\EcolBundle::class => ['all' => true],
    // ...
];

Requirements

  • PHP 8.2 or higher
  • Symfony 7.3 or higher
  • Doctrine ORM

Configuration

The bundle works out of the box without additional configuration. However, you can customize it by creating custom function providers and value providers.

Custom Function Providers

Create a service that implements function provider interface and tag it:

# config/services.yaml
services:
    App\Expression\MyFunctionProvider:
        tags: ['ecol.function.provider']

Custom Value Providers

Create a service that implements value provider interface and tag it:

# config/services.yaml
services:
    App\Expression\MyValueProvider:
        tags: ['ecol.value.provider']

Quick Start

Basic Usage

Use the engine service directly in your code:

use Symfony\Component\ExpressionLanguage\Expression;
use Tourze\EcolBundle\Service\Engine;

class YourService
{
    public function __construct(
        private readonly Engine $engine
    ) {
    }
    
    public function evaluateExpression(): mixed
    {
        // Basic expression
        $result = $this->engine->evaluate('1 + 1');
        
        // Expression with variables
        $result = $this->engine->evaluate('a + b', ['a' => 5, 'b' => 10]);
        
        // Expression with Chinese syntax
        $result = $this->engine->evaluate('a 大于 b 并且 c 等于 d', [
            'a' => 10, 
            'b' => 5, 
            'c' => 'test', 
            'd' => 'test'
        ]);
        
        return $result;
    }
}

Validate Entity Properties

Use the Expression attribute to validate expressions in entity properties:

use Tourze\EcolBundle\Attribute\Expression;

class YourEntity
{
    #[Expression]
    private string $conditionExpression = 'a > b && c == 1';
    
    // ...
}

Available Function Providers

  • DateFunctionProvider: Date manipulation functions
  • MathFunctionProvider: Mathematical operations
  • ExceptionFunctionProvider: Exception handling in expressions
  • ServiceFunctionProvider: Service access in expressions

Available Value Providers

  • Today: Provides today's date (variable name: 当天日期)
  • TodayRange: Provides today's start and end timestamps (variable name: 当天日期范围)
  • CurrentTimestamp: Provides current timestamp (variable name: 当前时间戳)
  • CurrentYear: Provides current year
  • CurrentMonth: Provides current month
  • CurrentWeekday0 - CurrentWeekday6: Provides weekday dates (variable name: 本周周日日期 etc.)

Chinese Syntax Support

The engine automatically converts Chinese operators to their programming equivalents:

Chinese Programming
并且, 并, 与, 和 &&
或者, 或 ||
不是, 不等于 !=
等于, 相等于, 是 ==
全等于 ===
大于, 多于 >
小于, 少于 <
大于等于, 大于或等于 >=
小于等于, 小于或等于 <=
加上 +
减去 -
乘以 *
除以 /

Advanced Usage

Working with Complex Expressions

For complex business logic, you can combine multiple operators and functions:

$expression = '(age >= 18 并且 country == "CN") 或者 (vip_level > 3 且 balance >= 1000)';
$result = $engine->evaluate($expression, [
    'age' => 25,
    'country' => 'CN', 
    'vip_level' => 2,
    'balance' => 500
]);

Custom Expression Functions

Implement the ExpressionFunctionProviderInterface to add custom functions:

use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;

class CustomFunctionProvider implements ExpressionFunctionProviderInterface
{
    public function getFunctions(): array
    {
        return [
            new ExpressionFunction('custom_function', 
                function ($arg) { return sprintf('custom_function(%s)', $arg); },
                function ($arguments, $arg) { return $arg * 2; }
            ),
        ];
    }
}

Entity Validation with Complex Rules

Use expressions for complex entity validation rules:

#[Expression]
private string $businessRule = 'status == "active" && (priority > 5 || urgent == true)';

Contributing

Please see CONTRIBUTING.md for details.

License

The MIT License (MIT). Please see License File for more information.

tourze/symfony-ecol-bundle 适用场景与选型建议

tourze/symfony-ecol-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.43k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 tourze/symfony-ecol-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-31