承接 ignislabs/flare-cqrs 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

ignislabs/flare-cqrs

Composer 安装命令:

composer require ignislabs/flare-cqrs

包简介

Flare is a small and easy to use CQRS library.

README 文档

README

Flare is a small and easy to use CQRS library.
It drives CQRS by making use of the message bus pattern, separating Queries (interrogatory messages) from Commands (imperative messages).

This library was greatly inspired by Messaging Flavours article by Mathias Verraes.

What is CQRS?

Originated with Bertrand Meyer's Command and Query Separation principle (CQS):

It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, asking a question should not change the answer. More formally, methods should return a value only if they are referentially transparent and hence possess no side effects.

So, Command and Query Responsibility Segregation (CQRS):

... applies the CQS principle by using separate Query and Command objects to retrieve and modify data, respectively.

Source: Wikipedia

Installation

Through composer:

$ composer require ignislabs/flare-cqrs

Usage

FlareCQRS is framework-agnostic, but it's really easy to bootstrap and use.

Bootstrapping

<?php
// Instantiate the resolver:
// This will get you a new instance of the hander based on the _handler id_,
// i.e, the fully-qualified class name, or wharever identifier you might use in
// a framework container.
// By default FlareCQRS comes with two resolvers, a CallableResolver and a PSR11Resolver.
// You can always create your own resolver by implementing the `Resolver` contract.

// Generic callable resolver
$resolver = new \IgnisLabs\FlareCQRS\Handler\Resolver\CallableResolver(function($handlerId) {
    // Somehow resolve your handler handler:
    return new performMagicToGetHandlerInstance($handlerId);
});

// PSR11 resolver (assuming Laravel's `$app` container, since it's PSR-11 compliant)
$resolver = new \IgnisLabs\FlareCQRS\Handler\Resolver\PSR11Resolver($app);

// Now instantiate the buses passing them a Locator instance

$queryBus = new \IgnisLabs\FlareCQRS\QueryBus(
    // Tell the Locator which handler corresponds to which query
    // and how to instantiate the handlers (passing in the Resolver)
    new \IgnisLabs\FlareCQRS\Handler\Locator\MapLocator($resolver, [
        GetAllTasksQuery::class => GetAllTasksHandler::class
    ])
);

$commandBus = new \IgnisLabs\FlareCQRS\CommandBus(
    // Tell the Locator which handler corresponds to which command
    // and how to instantiate the handlers (passing in the Resolver)
    new \IgnisLabs\FlareCQRS\Handler\Locator\MapLocator($resolver, [
        AddTaskCommand::class => AddTaskHandler::class
    ])
);

Usage

Now you can use the buses to dispatch any message, like so:

<?php
// Queries can return whatever you need, it will be encapsulated in a Result object
$result = $queryBus->dispatch(new GetAllTasksQuery('some', 'params'));
// You can call `$result->call`:
$result->call(function(TaskCollection $tasks) {
    // Do what you want with your results
    // Using `call` let's you use type-hinting
    // It can be a any `callable`, not just a closure
});
// Or just get the result right away:
$result->getResult();

// Commands do not return anything
$commandBus->dispatch(new AddTaskCommand('Task Title', 'The task description'));

// You can dispatch multiple commands in sequence with a single call
$commandBus->dispatch(
    new AddTaskCommand('Task Title', 'The task description'),
    new UpdateTaskCommand('NEW Task Title', 'The NEW task description')
);
// Or if you like splat!
$commandBus->dispatch(...$commandsArray);

Message (Query & Command) classes

Your Message classes are simple DTO objects, so there are no rules or contracts to use, they can be whatever you like.

You can, however, take advantage of the DataAccessorTrait. With it you can have automatic accessor properties for your message classes. The trait defines a data private property, a get accessor method and the __get magic method so you can access the data as properties:

<?php

class MyMessage {
    use \IgnisLabs\FlareCQRS\Message\DataAccessorTrait;
 
    public function __construct(string $foo, int $bar) {
        $this->setData(compact('foo', 'bar'));
    }
}

$message = new MyMessage('baz', 'qux');
// Using generic `get` accessor:
$message->get('foo'); // returns 'baz'
// Using the magic accessor:
$message->bar; // returns 'qux'

Handlers

A handler can be anything, as long as it is callable. Although, you will probably want to make them classes. In order to make a class be callable, just implement the __invoke method in it.

The __invoke method will receive an instance of the corresponding command.

This way, the classes are 100% yours, no hard dependency on this library whatsoever and you can typehint freely.

Let's see a quick example:

<?php
class MyMessageHandler {
    public function __invoke(MyMessage $command) {
     // do something here
    }
}

Middlewares

You can create middlewares to interact with the messages before they reach their respective handlers.

Middlewares, same as with the Handlers, are callables, but you might prefer them to be classes, by using the same __invoke strategy.

You can pass your middlewares globally to your buses on instantiation as the last parameters like so:

<?php
$callableMiddleware = function() { /* ... */ };
$queryBus = new \IgnisLabs\FlareCQRS\QueryBus(
    $locator, new LoggingMiddleware($logger), new FooMiddleware, $callableMiddleware
);

Or you can add/replace the middlewares on a one time basis:

<?php
// Add a middleware to the chain
$commandBus->addMiddleware(new LoggingMiddleware($logger));
// Completely replace the middleware chain
$commandBus->middlewares(new LoggingMiddleware($logger), new FooMiddleware);

Buses are immutable, so adding or replacing middlewares on a bus will always return a new bus instance, so any subsequent calls to your buses will not be affected by these middleware changes.

ignislabs/flare-cqrs 适用场景与选型建议

ignislabs/flare-cqrs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 01 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 ignislabs/flare-cqrs 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 18
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-27