nicolasjourdan/business-logic-bundle
Composer 安装命令:
composer require nicolasjourdan/business-logic-bundle
包简介
This bundle helps you to create and to implement your business logic
README 文档
README
The BusinessLogicBundle helps you to create and to implement your business rules.
Installation
Step 1: Download the Bundle
Open a command console, enter your project directory and execute:
$ composer require nicolasjourdan/business-logic-bundle
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php file of your project:
// config/bundles.php return [ // ... NicolasJourdan\BusinessLogicBundle\NicolasJourdanBusinessLogicBundle::class => ['all' => true], ];
Documentation
The source of the documentation is stored in the Resources/doc/ folder in this bundle.
The full documentation is here.
This bundle implements 3 design patterns (Specification, Rules and Chain of responsibility) in order to define a system to help you to create your own business rules.
There are 3 important objects in this conception :
- Specification : This is your business condition. It is used to define if a business rule has to be executed on the current candidate.
- Rule : This is your business rule.
- RulesEngine : This object contains several rules and executes ones which have to be executed on the current candidate.
Please, feel free to see the demo.
Basic Usage
Create a Specification
The specification class
<?php namespace Your\Namespace\Specification; use NicolasJourdan\BusinessLogicBundle\Service\Specification\CompositeSpecification; class IsDummySpecification extends CompositeSpecification { public function isSatisfiedBy($candidate): bool { // Your business condition... } }
You can generate this class with the next command bin/console make:specification IsDummySpecification.
Create a Rule
The rule class
<?php namespace Your\Namespace\Rule; use NicolasJourdan\BusinessLogicBundle\Service\Rule\RuleInterface; use Your\Namespace\Specification\IsDummySpecification; class DummyRule implements RuleInterface { private const BUSINESS_LOGIC_TAGS = [ ['rule.user.awesome', 10], ['rule.user.another_tag'], ]; /** @var IsDummySpecification */ private $specification; public function __construct(IsDummySpecification $specification) { $this->specification = $specification; } public function shouldRun($candidate): bool { return $this->specification ->not() ->isSatisfiedBy($candidate); } public function execute($candidate) { // Your business rule... } }
Thanks to the private constant BUSINESS_LOGIC_TAGS you can dynamically add tags on your rule.
This constant must be an array of arrays. Each array corresponds to a tag. The first argument must be a string
and it is corresponding to the tag name. The second one is optional, it must be an integer and it is corresponding to the priority.
You can generate this class with the next command bin/console make:rule DummyRule.
The service declaration
You dont need to declare your rules into the services.yml file thanks to the indication implements RuleInterface.
If you decide to declare it into this file, the tags that you defined will be automatically merged with those of the constant.
For instance, the DummyRule will be tagged with : rule.user.vip, rule.user.basic, rule.user.awesome and rule.user.another_tag.
# config/services.yml Your\Namespace\Rule\DummyRule: arguments: $specification: '@Your\Namespace\Specification\IsDummySpecification' tags: - { name: 'rule.user.vip', priority: 20 } # Tag your rule in order to include it into the related RulesEngine - 'rule.user.basic' # You can add several tags to a single rule
Create a RulesEngine
RulesEngine declaration
# config/services.yml rules.engine.user.vip: class: NicolasJourdan\BusinessLogicBundle\Service\Rule\RulesEngine arguments: $rules: !tagged rule.user.vip # Get all Rules tagged `rule.user.vip`
Use your created RulesEngine
Your service class
<?php namespace Your\Namespace\Service; use NicolasJourdan\BusinessLogicBundle\Service\Rule\RulesEngine; class Dummy { /** @var RulesEngine */ private $rulesEngine; public function __construct(RulesEngine $rulesEngine) { $this->rulesEngine = $rulesEngine; } public function foo(): void { $candidate = ... // The RulesEngine will execute all the business rules which have to be executed on the candidate. $updatedCandidate = $this->rulesEngine->execute($candidate); //... } }
The service declaration
# config/services.yml Your\Namespace\Service\Dummy: arguments: $rulesEngine: '@rules.engine.user.vip'
License
This bundle is under the MIT license. See the complete license in this bundle.
nicolasjourdan/business-logic-bundle 适用场景与选型建议
nicolasjourdan/business-logic-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 12 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「logic」 「business」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nicolasjourdan/business-logic-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nicolasjourdan/business-logic-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nicolasjourdan/business-logic-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Addons to the display-logic module by UncleCheese
Business rules engine tools.
Routes HTTP requests to your code.
Extension provide very simply use enum for models (and others) in yii2
Tests of exceptions hierarchy
A basic wrapper for citco/carbon which returns business days between two given dates.
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-05