uuf6429 / rune
PHP Rule Engine.
Installs: 11 655
Dependents: 0
Suggesters: 0
Security: 0
Stars: 67
Watchers: 13
Forks: 19
Open Issues: 7
Requires
- php: ^7.4 || ^8
- phpdocumentor/reflection-docblock: ^5.3
- symfony/expression-language: ^4 || ^5 || ^6
Requires (Dev)
- ext-json: *
- friendsofphp/php-cs-fixer: ^3
- jetbrains/phpstorm-attributes: ^1.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^6 || ^9
This package is auto-updated.
Last update: 2024-11-03 07:56:14 UTC
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 returnstrue
orfalse
) 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.
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); }