uuf6429/rune
Composer 安装命令:
composer require uuf6429/rune
包简介
PHP Rule Engine.
README 文档
README
Rune - A PHP Rule Engine Toolkit.
This library is an implementation of a Business Rule Engine (a type of Business Process Automation software).
Table Of Contents
Installation
The recommended and easiest way to install Rune is through Composer:
composer require uuf6429/rune
Architecture
The library is made up of the following main parts:
- Rule (impl.
Rule\RuleInterface) - object representing a business rule. For most use-cases, one can just useRule\GenericRule. Each rule must have a unique id, descriptive name, a condition (as an expression that returnstrueorfalse) and the action (see below) to be triggered when the condition is met. - Action (impl.
Action\ActionInterface) - an object that does something when the associated rule is triggered. Actions in general can be reused by multiple rules. For example, if you're using the rule engine in product sales, an action might automatically add a fee to the bill when a certain condition applies (e.g. a fee for specific delivery countries, or a negative fee for a discount). - Context (impl.
Context\ContextInterface) - an object that provides data to the rule engine and action to work with. This can be thought of as a collection of all the available data for the current situation. For example, when the current situation is about a user buying a product, you would have data about the user, the product, offers, and perhaps also higher level information such as time, locality etc. You almost always have to implement your own context since this always depends on your (business) scenario. - RuleEngine - essentially, the object that connects the others together to function.
flowchart LR
A("
<center><b>Rules</b></center>
Rule 1:
- Condition: <code>product.color == #quot;green#quot;</code>
- Action: <code>applyDiscount(10)</code>
Rule 2:
- Condition: <code>product.color == #quot;red#quot;</code>
- Action: <code>applyDiscount(20)</code>
") --> C
B("
<center><b>Context</b></center><pre><code>{
product: {
name: #quot;Scarf#quot;,
color: #quot;red#quot;
}
}</code></pre>
") --> C
subgraph RuleEngine ["<br><h2>Rule Engine</h2>"]
C{"Filter Rules"} --> D(["Execute Action(s)"])
end
D --> E("<code>applyDiscount(20)</code>")
style A text-align: left
style B text-align: left
Loading
Usage
Various examples can be found in uuf6429/rune-examples.
Live Example
Example Code
The following code is a very simple example of how Rune can be used. It defines one model (Product),
context (ProductContext) and uses CallbackAction to print out the rules that have been triggered.
namespace MyApplication; use uuf6429\Rune\Action\CallbackAction; use uuf6429\Rune\Context\ClassContext; use uuf6429\Rune\Engine; use uuf6429\Rune\Rule\GenericRule; use uuf6429\Rune\Rule\RuleInterface; // A class whose instances will be available inside the rule engine. class Product { public function __construct( public readonly string $name, public readonly string $colour, ) { } } // A class that represents the rule engine execution context. // Note that public properties will be available in the rule expressions, // in this case rules will have access to "product" as a variable (and all of product's public properties). class ProductContext extends ClassContext { public function __construct( public readonly Product $product ) { } } // Declare an action to be triggered when a rule matches against a product. $action = new CallbackAction( static fn ($eval, ProductContext $context, RuleInterface $rule) => printf( "Rule %s triggered for %s %s\n", $rule->getId(), ucwords($context->product->colour), $context->product->name ) ); // Declare some sample rules. $rules = [ new GenericRule(1, 'Red Products', 'product.colour == "red"', $action), new GenericRule(2, 'Red Socks', 'product.colour == "red" and product.name matches "/socks/i"', $action), new GenericRule(3, 'Green Socks', 'product.colour == "green" and product.name matches "/socks/i"', $action), new GenericRule(4, 'Socks', 'product.name matches "/socks/" > 0', $action), ]; // Declare available products (to run rules against). $products = [ new Product('Bricks', 'red'), new Product('Soft Socks', 'green'), new Product('Sporty Socks', 'yellow'), ]; // Create rule engine. $engine = new Engine(); // Run rules for each product. Note that each product should exist in a separate context. foreach ($products as $product) { $engine->execute(new ProductContext($product), $rules); }
uuf6429/rune 适用场景与选型建议
uuf6429/rune 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.79k 次下载、GitHub Stars 达 70, 最近一次更新时间为 2016 年 07 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「engine」 「expression」 「rule」 「business」 「uuf6429」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 uuf6429/rune 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 uuf6429/rune 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 uuf6429/rune 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
ContentElement for Contao to generate hr. Simply choose element type "divider" and save.
Magento 2 Discount Amount Limiter
A SilverStripe module to optimise the Meta, crawling, indexing, and sharing of your website content (forked from Cyber-Duck/Silverstripe-SEO)
Business rules engine tools.
A Processor for Markup written in PHP. Allows extraction of Markup into a data structure, orchestrated nested manipulation of said structure, and output as (optimized) Markup.
Condition Builder for logical expressions
统计信息
- 总下载量: 11.79k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 70
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-07-12