nicolasjourdan/business-logic-bundle

此组件帮助您创建和实现业务逻辑

1.1.0 2021-02-05 19:05 UTC

This package is auto-updated.

Last update: 2024-09-06 03:03:54 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

BusinessLogicBundle 帮助您创建和实现业务规则。

安装

步骤 1: 下载组件

打开命令行控制台,进入您的项目目录并执行

$ composer require nicolasjourdan/business-logic-bundle

步骤 2: 启用组件

然后,通过将其添加到项目中 config/bundles.php 文件中注册的组件列表中来启用组件

// config/bundles.php

return [
    // ...
    NicolasJourdan\BusinessLogicBundle\NicolasJourdanBusinessLogicBundle::class => ['all' => true],
];

文档

此组件中文档的源代码存储在 Resources/doc/ 文件夹中。完整的文档在这里:这里

此组件实现了 3 个设计模式(规范、规则和责任链)来定义一个系统,帮助您创建自己的业务规则。

本概念中有 3 个重要对象

  • 规范:这是您的业务条件。它用于确定当前候选者是否需要执行业务规则。
  • 规则:这是您的业务规则。
  • 规则引擎:此对象包含多个规则,并在当前候选者上执行需要执行的规则。

请自由查看 示例

基本用法

创建规范

规范类

<?php

namespace Your\Namespace\Specification;

use NicolasJourdan\BusinessLogicBundle\Service\Specification\CompositeSpecification;

class IsDummySpecification extends CompositeSpecification
{
    public function isSatisfiedBy($candidate): bool
    {
        // Your business condition...
    }
}

您可以使用以下命令生成此类:bin/console make:specification IsDummySpecification

创建规则

规则类

<?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...
    }
}

感谢私有常量 BUSINESS_LOGIC_TAGS,您可以在规则上动态添加标签。此常量必须是一个数组。每个数组对应一个标签。第一个参数必须是一个字符串,对应于标签的 名称。第二个参数是可选的,必须是一个整数,对应于 优先级

您可以使用以下命令生成此类:bin/console make:rule DummyRule

服务声明

由于指示 implements RuleInterface,您无需在 services.yml 文件中声明您的规则。如果您决定在文件中声明,您定义的标签将自动与常量中的标签合并。
例如,DummyRule 将带有以下标签:rule.user.viprule.user.basicrule.user.awesomerule.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

创建规则引擎

规则引擎声明

# 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`

使用您创建的规则引擎

您的服务类

<?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);
        
        //...
    }
}

服务声明

# config/services.yml
    Your\Namespace\Service\Dummy:
        arguments:
            $rulesEngine: '@rules.engine.user.vip'

许可证

此组件受 MIT 许可证的保护。请参阅此组件中的完整许可证:这里