定制 ibrahimgunduz34/maria-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ibrahimgunduz34/maria-bundle

Composer 安装命令:

composer require ibrahimgunduz34/maria-bundle

包简介

A Rule Engine Implementation For Symfony Projects

README 文档

README

Build Status GitHub release License: MIT

What is MariaBundle

Maria is a simple and flexible business rule engine that you can integrate easily into your Symfony applications through Bundle mechanism. It allows taking an action based on the rules when the input data matched. You can trigger Maria by a trigger event you defined. It checks the input argument which comes through the trigger event by the rules and invokes the action handler when matching occurred. Action handlers might be a class or a reference that points to a service definition in the dependency injection system. So you can communicate with other Symfony components easily through action handlers on Maria scenarios.

Installation

You can install Maria through composer

$ composer require ibrahimgunduz34/maria-bundle

Add the bundle class to the bundle list by your Symfony version:

For Symfony 3.x users:

AppKernel.php

<?php
///...
public function registerBundles()
{
    $bundles = array(
        //...
        new SweetCode\MariaBundle\MariaBundle(),
        //...
    );
}
//...

For Symfony >= 4 users:

config/bundles.php

<?php
return [
  //...
   SweetCode\MariaBundle\MariaBundle::class => ['all' => true],
  //...  
];

Configuration Reference

# config/packages/maria.yaml
maria:
    scenarios_name:
      trigger: <The event name you will trigger>
      handler: <Class name or service reference>
#      handler:
#        reference: <Class or service reference from DI>
#        method: <handler method name, default: onAction>
#        serialize: <true | false, default: false>
      rules:
        # You can define matchers as any, all, none, first or last matcher 
        # in the first line. If you won't an iterable object, you can simply
        # use default or ignore the first line. 
        default: # [any | all | none | first | last | default ]
          # You can define rules by numeric or associative arrays.
          
          # Associative arrays indicate that you will apply AND logic between the 
          # elements in the array
          
          # Numeric arrays indicate that you will apply OR logic between the 
          # elements in the array.
          
          # amount > 100 AND category_id IN [1, 2, 3]
          amount: {gt: 100}
          category_id: { in: [1,2,3] }   
          
          # (amount > AND category_id=1) OR (amount < 500 AND category_id IN [5,6])
          - amount: {gt: 100}
            category_id: { eql: 1 }
          - amount: [lt: 500]
            category_id: { in: [5,6] }
            
          # (category_id IN [1,2] AND amount BETWEEN 100-200) OR (category_id = 3 AND amount >= 200
          - amount: { btw: [100, 200] }
            category_id: { in: [1, 2] }
          - amount: [lte: 200]
            category_id: { eql: 3 }
          
          # Matching by RegExp
          - { category_id: { in: [1,5] }, description: { regex: /awesome/i } }                 

See Also

Example Usage

Define the following configuration into config/packages/maria.yaml

maria:
    free_shipment:
        trigger: cart.updated
        handler: App\Handler\FreeShipmentHandler
        rules:
          amount: {gt: 100}
          category_id: {eql: 1}

Create a handler class in order to take an action to give free shipment

<?php
namespace App\Handler;

use SweetCode\MariaBundle\MariaEventArg;

class FreeShipmentHandler
{
    public function onAction(MariaEventArg $eventArg){
        //TODO: Write your magic code to give free shipment.
        // You can reach your original input data by:
        // $eventArg->getData();
    }
}

Trigger Maria where you updated cart in the project.

<?php
//...
/** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $eventDispatcher */
$eventDispatcher = $container->get('event_dispatcher');
// You can simply send an object or associative array to Maria through event context.
$context = [
    'amount'        => $order->getAmount(),
    'category_id'   => $order->getCategoryId(),
]
$eventDispatcher->dispatch(new \SweetCode\MariaBundle\MariaEventArg($context), 'cart.updated');
//...

Working In Asynchronous Way

Maria does not provide a way to invoke action handlers as asynchronous. However, you can make action handlers asynchronous easily by using other third-parties like RabbitMqBundle or built-in Symfony components like Messenger You can make your action handler asynchronous by following the steps below:

Important Notice: We strongly recommend to follow the installation steps from rabbitmq-bundle repository: https://github.com/php-amqplib/RabbitMqBundle

Install RabbitMqBundle

$ composer require php-amqplib/rabbitmq-bundle

Add the bundle class to bundle the bundle list in the project:

For Symfony 3.x users:

AppKernel.php

<?php
///...
public function registerBundles()
{
    $bundles = array(
        //...
        new OldSound\RabbitMqBundle\OldSoundRabbitMqBundle(),
        //...
    );
}
//...

For Symfony >= 4 users:

config/bundles.php

<?php
return [
  //...
   OldSound\RabbitMqBundle\OldSoundRabbitMqBundle::class => ['all' => true],
  //...  
];

Configure the RabbitMq bundle.

# config/packages/rabbitmq.yaml

old_sound_rabbit_mq:
    connections: 
        host: my.rabbitmq.host
        user: rabbitmq
        password: rabbitmq
        vhost: '/'
        lazy: false
        connection_timeout: 3
        read_write_timeout: 3
        keepalive: false
        heartbeat: 0
        use_socket: true
    producers:
        email_producer:
            connection: default
            exchange_options: {name: 'emails', type: direct}
            service_alias: email_producer # otherwise it gives very long service name
    consumers:
      email_consumer:
        exchange_options: {name: 'emails', type: direct}
        queue_options: {name: 'emails'} 
        # That's the service you need to implement as a consumer.
        # Check the documentation from the repository to see how to implement a consumer:
        # https://github.com/php-amqplib/RabbitMqBundle#consumers
        callback: email_sender_service 

And.. say maria, use email_producer as an action handler.

maria:
    gift-email:
        trigger: some.event
        handler: 
          reference: '@email_producer'
          method: 'publish'
          erialize: true
        rules:
          ##...

Enjoy!

TODO:

  • Moving the scenario rules into different type of storage providers such as in_memory or doctrine
  • Validation improvement for configuration.

License:

You can access the license documentation here.

Credits:

Bundles structure, extension tests and the documentation partially inspired RabbitMqBundle.

ibrahimgunduz34/maria-bundle 适用场景与选型建议

ibrahimgunduz34/maria-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.07k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2019 年 12 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「Symfony2」 「engine」 「rule」 「business」 「symfony3」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ibrahimgunduz34/maria-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ibrahimgunduz34/maria-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 ibrahimgunduz34/maria-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.07k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-02