adminmatrix/matrix_rule

Rule engine for adminmatrix

Maintainers

Details

gitee.com/adminmatrix/matrix_rule

Installs: 3

Dependents: 0

Suggesters: 0

Security: 0

pkg:composer/adminmatrix/matrix_rule

dev-master 2026-02-02 07:23 UTC

This package is auto-updated.

Last update: 2026-02-02 07:24:12 UTC


README

Matrix Rule Engine 是一个基于 PHP 的规则引擎,专为 AdminMatrix 后台系统设计,用于管理和执行各种业务规则。

功能特性

  • 灵活的规则定义和执行机制
  • 支持多种规则类型
  • 简单易用的 API 接口
  • 可扩展的规则系统
  • 与 AdminMatrix 无缝集成

目录结构

src/
├── engine/          # 规则引擎核心
│   └── Engine.php   # 引擎实现
├── rule/            # 规则定义
│   ├── RuleInterface.php  # 规则接口
│   ├── BaseRule.php       # 基础规则类
│   └── NumericRule.php    # 数值比较规则示例
├── service/         # 服务层
│   └── RuleService.php    # 规则服务
└── config/          # 配置目录

安装

通过 Composer 安装

composer require adminmatrix/matrix_rule

手动安装

将本目录复制到 vendor/adminmatrix/ 目录下,并在 composer.json 中添加:

"repositories": [
    {
        "type": "path",
        "url": "vendor/adminmatrix/matrix_rule"
    }
],
"require": {
    "adminmatrix/matrix_rule": "dev-master"
}

然后运行:

composer dump-autoload

使用示例

基本用法

// 引入命名空间
use adminmatrix\matrix_rule\service\RuleService;
use adminmatrix\matrix_rule\rule\NumericRule;

// 添加规则
RuleService::addRule(new NumericRule('数值大于10', '检查数值是否大于10', '>', 10));

// 执行规则
$result = RuleService::execute(15);
print_r($result);
// 输出:Array ( [0] => Array ( [rule] => adminmatrix\matrix_rule\rule\NumericRule [result] => 1 ) )

执行指定规则

// 执行指定规则
$result = RuleService::executeRule('adminmatrix\matrix_rule\rule\NumericRule', 5);
echo $result; // 输出:

添加多个规则

// 引入其他规则类
use adminmatrix\matrix_rule\rule\StringRule;
use adminmatrix\matrix_rule\rule\ArrayRule;

// 添加多个规则
RuleService::addRules([
    new NumericRule('数值大于10', '检查数值是否大于10', '>', 10),
    new StringRule('字符串长度大于5', '检查字符串长度是否大于5', '>', 5),
    new ArrayRule('数组元素大于3', '检查数组元素是否大于3', '>', 3)
]);

// 执行所有规则
$result = RuleService::execute(['value' => 15, 'text' => 'Hello World', 'items' => [1, 2, 3, 4, 5]]);
print_r($result);

自定义规则

要创建自定义规则,只需实现 RuleInterface 接口或继承 BaseRule 类:

<?php

namespace adminmatrix\matrix_rule\rule;

class CustomRule extends BaseRule
{
    protected $condition;

    public function __construct($name = '自定义规则', $description = '自定义规则描述', $condition = null)
    {
        parent::__construct($name, $description);
        $this->condition = $condition;
    }

    public function execute($data)
    {
        // 实现自定义规则逻辑
        if (is_callable($this->condition)) {
            return call_user_func($this->condition, $data);
        }
        return false;
    }

    public function setCondition(callable $condition)
    {
        $this->condition = $condition;
        return $this;
    }
}

配置

规则引擎支持通过配置文件进行配置,配置文件位于 config/rule.php

<?php

return [
    // 规则引擎配置
    'engine' => [
        // 规则缓存设置
        'cache' => [
            'enabled' => false,
            'ttl' => 3600
        ]
    ],
    
    // 默认规则配置
    'rules' => [
        // 在这里配置默认规则
    ]
];

许可证

本项目采用 GNU General Public License v2.0 许可证,详见 LICENSE 文件。

贡献

欢迎提交 Issue 和 Pull Request 来改进这个项目。

联系方式