mcneely / rulesengine
description
dev-master
2021-12-08 12:56 UTC
Requires
- php: >=7.4
- psr/log: ^1.1
- symfony/config: ^5.3
- symfony/event-dispatcher: ^5.3
- symfony/expression-language: ^5.3
- symfony/yaml: ^5.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- infection/infection: ^0.20.2
- phpstan/phpstan: ^0.12.4
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.10
This package is auto-updated.
Last update: 2025-05-08 21:02:28 UTC
README
Proof Of Concept for a PHP Rules engine with Drools inspired rules.
Usage:
// initialise rules engines with the top of the directory containing namespaced rule files. Ex: __DIR__ . "/../Fixtures/namespaces" $rulesEngine = new RulesEngine(__DIR__ . "/../Fixtures/namespaces"[, EventDispatcher $eventDispatcher]); // add facts to provide information to the rules engine via class, array, etc. $rulesEngine->addFact( 'SimpleObject', [ $value => 0, $hasPassed => false ]); //Set the Namespace, this loads all rules in the hierarchy of the namespace. $rulesEngine->setNamespace('Org\Test'); // Optionally pass in a PSR-3 Compliant logger. $rulesEngine->setLogger(new Logger()); // Run the rules on the facts $rulesEngine->run();
Sample Rule File (.rf.yml):
namespace: Org\Test rules: 'Simple Object Rule': when: # When an evaluation line is prefixed with "<key>:" the result # is stored as a fact that can be referenced later. - obj: 'SimpleObject' - 'obj.value == 42' then: - 'SimpleObject.hasPassed = true' 'Simple Object Two Rule': when: 'SimpleObjectTwo.getValue() == 5555' then: - 'SimpleObjectTwo.setString("Woo!")'
Note: More examples can be found in the tests/Fixtures/namespaces
directory.
Event Listeners:
Several are triggered throughout the lifecycle of a rule. These events currently exist in the src/Events
folder and can be listened for by optionally passing the Symfony EventDispatcher to the constructor. Future Versions may allow for the trigger of custom events from within rules.