mcneely / rulesengine
描述
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: 2024-09-08 19:23:17 UTC
README
基于 Drools 激励的 PHP 规则引擎的概念证明。
用法
// 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();
示例规则文件 (.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!")'
注意:更多示例可以在 tests/Fixtures/namespaces
目录中找到。
事件监听器
在规则的整个生命周期中会触发多个事件。这些事件目前存在于 src/Events
目录中,可以通过将 Symfony 事件调度器作为可选参数传递给构造函数来监听。未来的版本可能会允许在规则中触发自定义事件。