prooph/laravel-package

Laravel 包,用于 prooph 组件,可快速开始使用消息总线、CQRS、事件存储和快照

v0.5 2018-08-01 12:30 UTC

This package is auto-updated.

Last update: 2024-08-29 03:18:34 UTC


README

概述

这是一个 Laravel 包,用于 prooph 组件,可快速开始使用消息总线、CQRS、事件存储和快照。它使用 prooph/pdo-event-store 事件存储,但也提供了更多适配器。

它提供了所有 服务定义和默认配置。这是一个快速启动包。如果您想在生产环境中使用 prooph 组件,我们建议根据您的需求配置 prooph 组件。请参阅 文档 了解 prooph 组件 的更多详细信息。

为了快速原型设计,我们建议使用我们的 prooph-cli 工具。

可用服务

  • Prooph\ServiceBus\CommandBus: 分发命令
  • Prooph\ServiceBus\EventBus: 分发事件
  • Prooph\ServiceBus\QueryBus: 允许通过消息总线进行查询
  • Prooph\EventStoreBusBridge\TransactionManager: 服务总线的事件存储事务管理器
  • Prooph\EventStoreBusBridge\EventPublisher: 在事件总线上发布事件

可用事件存储

  • Prooph\EventStore\Pdo\MariaDbEventStore: MariaDB 事件存储适配器
  • Prooph\EventStore\Pdo\MySqlEventStore: MySQL 事件存储适配器
  • Prooph\EventStore\Pdo\PostgresEventStore: PostgreSQL 事件存储适配器

可用外观

安装

您可以通过在您的 composer.json 中添加 "prooph/laravel-package": "^0.4" 作为依赖项,通过 Composer 安装 prooph/laravel-package

服务提供者

如果您使用的是 Laravel 5.5 或更高版本,则包将自动注册自身。否则,您需要将 Prooph\Package\ProophServiceProvider 添加到您的 提供者 数组中。然后您将能够访问上述服务。

此包具有配置文件,可以根据您的需要进行配置。

部署 prooph 配置文件以添加对 prooph 组件的配置。

$ php artisan vendor:publish

数据库

使用以下命令设置 Event Store 和快照的数据库迁移

$ php artisan make:migration create_event_stream_table

更新类 CreateEventStreamTable

class CreateEventStreamTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        \Prooph\Package\Migration\Schema\EventStoreSchema::createSingleStream('event_stream', true);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        \Prooph\Package\Migration\Schema\EventStoreSchema::dropStream('event_stream');
    }
}

现在更新快照表。

$ php artisan make:migration create_snapshot_table

更新类 CreateSnapshotTable

class CreateSnapshotTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        \Prooph\Package\Migration\Schema\SnapshotSchema::create('snapshot');
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        \Prooph\Package\Migration\Schema\SnapshotSchema::drop('snapshot');
    }
}

现在是执行迁移的时候了

$ php artisan migrate

示例

您只需定义您的模型(实体、仓库)以及命令/路由。以下是从 proophessor-do 示例应用程序 中的示例配置。

config/prooph.php 中定义 RegisterUser 的聚合仓库、命令路由和事件路由。

// add the following config in your config/prooph.php under the specific config key
return [
    'event_store' => [
        // list of aggregate repositories
        'user_collection' => [
            'repository_class' => \Prooph\ProophessorDo\Infrastructure\Repository\EventStoreUserCollection::class,
            'aggregate_type' => \Prooph\ProophessorDo\Model\User\User::class,
            'aggregate_translator' => \Prooph\EventSourcing\EventStoreIntegration\AggregateTranslator::class,
            'snapshot_store' => \Prooph\EventStore\Snapshot\SnapshotStore::class,
        ],
    ],
    'service_bus' => [
        'command_bus' => [
            'router' => [
                'routes' => [
                    // list of commands with corresponding command handler
                    \Prooph\ProophessorDo\Model\User\Command\RegisterUser::class => \Prooph\ProophessorDo\Model\User\Handler\RegisterUserHandler::class,
                ],
            ],
        ],
        'event_bus' => [
            'router' => [
                'routes' => [
                    // list of events with a list of projectors
                    \Prooph\ProophessorDo\Model\User\Event\UserWasRegistered::class => [
                        \Prooph\ProophessorDo\Projection\User\UserProjector::class
                    ],
                ],
            ],
        ],
    ],
];

将服务容器工厂添加到 config/dependencies.php

// add the following config in your config/dependencies.php after the other factories
return [
    // your factories
    // Model
    \Prooph\ProophessorDo\Model\User\Handler\RegisterUserHandler::class => \Prooph\ProophessorDo\Container\Model\User\RegisterUserHandlerFactory::class,
    \Prooph\ProophessorDo\Model\User\UserCollection::class => \Prooph\ProophessorDo\Container\Infrastructure\Repository\EventStoreUserCollectionFactory::class,
    // Projections
    \Prooph\ProophessorDo\Projection\User\UserProjector::class => \Prooph\ProophessorDo\Container\Projection\User\UserProjectorFactory::class,
    \Prooph\ProophessorDo\Projection\User\UserFinder::class => \Prooph\ProophessorDo\Container\Projection\User\UserFinderFactory::class,
];

以下是如何调用 RegisterUser 命令的示例

    /* @var $container \Illuminate\Container\Container */
    
    /* @var $commandBus \Prooph\ServiceBus\CommandBus */
    $commandBus = $container->make(Prooph\ServiceBus\CommandBus::class);

    $command = new \Prooph\ProophessorDo\Model\User\Command\RegisterUser(
        [
            'user_id' => \Rhumsaa\Uuid\Uuid::uuid4()->toString(),
            'name' => 'prooph',
            'email' => 'my@domain.com',
        ]
    );

    $commandBus->dispatch($command);

以下是如何从上面的示例中获取所有用户列表的示例

    /* @var $container \Illuminate\Container\Container */
    $userFinder = $container->make(Prooph\ProophessorDo\Projection\User\UserFinder::class);

    $users = $userFinder->findAll();

支持

贡献

请随意分支并扩展现有插件或添加新插件,并将更改后的拉取请求发送给我们!为了建立一致的代码质量,请为您的所有更改提供单元测试,并可能对文档进行适当调整。

许可证

New BSD 许可证 下发布。