定制 eventsauce/laravel-eventsauce 二次开发

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

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

eventsauce/laravel-eventsauce

Composer 安装命令:

composer require eventsauce/laravel-eventsauce

包简介

Integration support for EventSauce with the Laravel framework.

README 文档

README

Build Status Code Style Latest Stable Version Total Downloads

Laravel EventSauce

👋 This project is currently looking for a new maintainer.

This library allows you to easily integrate EventSauce with your Laravel application. It takes out the tedious work of having to set up your own message dispatcher and provides an easy API to set up aggregate roots, aggregate root repositories, consumers, and more. It also comes with a range of scaffolding console commands to easily generate the boilerplate needed to get started with an Event Sourced application.

⚠️ While already usable, this library is currently still a work in progress. More documentation and features will be added over time. We appreciate pull requests that help extend and improve this project.

Requirements

  • PHP 7.4 or higher
  • Laravel 8.0 or higher

Installation

Before installing a new package it's always a good idea to clear your config cache:

php artisan config:clear

You can install the library through Composer. This will also install the main EventSauce library.

composer require eventsauce/laravel-eventsauce

Configuration

You can publish the config file with the following command:

php artisan vendor:publish --tag="eventsauce-config"

Migrations

The default domain_messages table will be loaded in through the library's service provider and migrated with:

php artisan migrate

You can also publish it and modify it as you see fit with the following command:

php artisan vendor:publish --tag="eventsauce-migrations"

Default Connection

The default database connection can be modified by setting the EVENTSAUCE_CONNECTION env variable:

EVENTSAUCE_CONNECTION=mysql

Default Table

The default table name for your domain messages can be set with the EVENTSAUCE_TABLE env variable:

EVENTSAUCE_TABLE=event_store

Scaffolding

Laravel EventSauce comes with some commands that you can use to scaffold objects and files which you'll need to build your Event Sourced app. These commands take out the tedious work of writing these yourself and instead let you focus on actually writing your domain logic.

Generating Aggregate Roots

Laravel EventSauce can generate aggregate roots and its related files for you. By using the make:aggregate-root command, you can generate the following objects and files:

  • The AggregateRoot
  • The AggregateRootId
  • The AggregateRootRepository
  • The migration file

To generate these files for a "Registration" process, run the following command:

php artisan make:aggregate-root Domain/Registration

This will scaffold the following files:

  • App\Domain\Registration
  • App\Domain\RegistrationId
  • App\Domain\RegistrationRepository
  • database/migrations/xxxx_xx_xx_create_registration_domain_messages_table.php

These are all the files you need to get started with an https://github.com/EventSaucePHP/LaravelEventSauce.

Generating Consumers

Laravel EventSauce can also generate consumers for you. For example, run the make:consumer command to generate a SendEmailConfirmation process manager:

php artisan make:consumer Domain/SendEmailConfirmation

This will create a class at App\Domain\SendEmailConfirmation where you can now define handle{EventName} methods to handle events.

Generating Commands & Events

EventSauce can generate commands and events for you so you don't need to write these yourself. First, define a commands_and_events.yml file which contains your definitions:

namespace: App\Domain\Registration
commands:
  ConfirmUser:
    fields:
      identifier: RegistrationAggregateRootId
      user_id: int
events:
  UserWasConfirmed:
    fields:
      identifier: RegistrationAggregateRootId
      user_id: int

Then define the input and output output file in the AggregateRootRepository:

final class RegistrationAggregateRootRepository extends AggregateRootRepository
{
    ...

    /** @var string */
    protected static $inputFile = __DIR__.'/commands_and_events.yml';

    /** @var string */
    protected static $outputFile = __DIR__.'/commands_and_events.php';
}

And register the AggregateRootRepository in your eventsauce.php config file:

'repositories' => [
    App\Domain\Registration\RegistrationAggregateRootRepository::class,
],

You can now generate commands and events for all repositories that you've added by running the following command:

php artisan eventsauce:generate

For more info on creating events and commands with EventSauce, as well as how to define different types, see the EventSauce documentation.

Usage

Aggregate Roots

More docs coming soon...

Aggregate Root Repositories

More docs coming soon...

Queue Property

You can instruct Laravel to queue all consumers onto a specific queue by setting the $queue property:

use App\Domain\SendConfirmationNotification;
use EventSauce\LaravelEventSauce\AggregateRootRepository;

final class RegistrationAggregateRootRepository extends AggregateRootRepository
{
    protected array $consumers = [
        SendConfirmationNotification::class,
    ];
    
    protected string $queue = 'registrations';
}

This will force all consumers who have the ShouldQueue contract implemented to make use of the registrations queue instead of the default queue defined in your queue.php config file.

Consumers

Consumers are classes that react to events fired from your aggregate roots. There's two types of consumers: projections and process managers. Projections update read models (think updating data in databases, updating reports,...) while process managers handle one-time tasks (think sending emails, triggering builds, ...). For more information on how to use them, check out EventSauce's Reacting to Events documentation.

A SendEmailConfirmation process manager, for example, can look like this:

use App\Events\UserWasRegistered;
use App\Models\User;
use App\Notifications\NewUserNotification;
use EventSauce\LaravelEventSauce\Consumer;

final class SendConfirmationNotification extends Consumer
{
    protected function handleUserWasRegistered(UserWasRegistered $event): void
    {
        User::where('email', $event->email())
            ->first()
            ->notify(new NewUserNotification());
    }
}

Within this consumer you always define methods following the handle{EventName} specification.

Registering Consumers

After writing your consumer, you can register them with the $consumers property on the related AggregateRootRepository:

use App\Domain\SendConfirmationNotification;
use EventSauce\LaravelEventSauce\AggregateRootRepository;

final class RegistrationAggregateRootRepository extends AggregateRootRepository
{
    protected array $consumers = [
        SendConfirmationNotification::class,
    ];
}

The sequence of adding consumers shouldn't matter as the data handling within these consumers should always be treated as independent from each other.

Queueing Consumers

By default, consumers are handled synchronous. To queue a consumer you should implement the ShouldQueue contract on your consumer class.

use EventSauce\LaravelEventSauce\Consumer;
use Illuminate\Contracts\Queue\ShouldQueue;

final class SendConfirmationNotification extends Consumer implements ShouldQueue
{
    ...
}

By doing so, we'll instruct Laravel to queue the consumer and let the data handling be done at a later point in time. This is useful to delay long-running data processing.

Changelog

Check out the CHANGELOG in this repository for all the recent changes.

Maintainers

This project is currently looking for a new maintainer.

Acknowledgments

Thanks to Frank De Jonge for building EventSauce. Thanks to Freek Van der Herten and Spatie's Laravel EventSauce library for inspiration to some of the features in this package.

License

Laravel EventSauce is open-sourced software licensed under the MIT license.

eventsauce/laravel-eventsauce 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 98
  • Watchers: 4
  • Forks: 18
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-15