alessandrominoccheri / broadway-dynamodb
为broadway提供DynamoDb存储
v3.3.1
2021-10-21 21:08 UTC
Requires
- php: >=7.4
- aws/aws-sdk-php: <=3.140
- broadway/broadway: ^2.0
Requires (Dev)
- broadway/uuid-generator: ^1.0
- phpstan/phpstan: ^0.12.48
- phpunit/phpunit: ^9.4
- ramsey/uuid: ^3.3
- vimeo/psalm: ^3.16
This package is auto-updated.
Last update: 2024-09-22 04:12:48 UTC
README
这个库是(broadway)[https://github.com/broadway/broadway]使用Amazon DynamoDB的事件存储实现。
安装
通过composer安装包
$ composer require alessandrominoccheri/broadway-dynamodb
Symfony
要将此库安装到Symfony 5应用程序中,您可以查看此存储库
Laravel
要在Laravel项目中使用此库,您需要首先通过composer安装broadway for laravel,然后像这样安装broadway-dynamodb
composer require nwidart/laravel-broadway
composer require alessandrominoccheri/broadway-dynamodb
在config/broadway.php
中,您可以这样配置dynamo事件存储
'event-store' => [
'table' => 'event_store',
'driver' => 'dynamoDb',
'connection' => 'mysql_events',
'endpoint' => env("AWS_END_POINT"),
'access_key_id' => env("AWS_ACCESS_KEY_ID_DYNAMO"),
'secret_access_key' => env("AWS_SECRET_ACCESS_KEY_DYNAMO"),
'region' => env("AWS_DEFAULT_REGION")
],
您可以通过这种方式分发您的命令
$command = new YourCommand($params);
$this->commandBus->dispatch($command);
要注册您的服务,您可以创建一个提供者文件,例如
class BroadwayServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
* @return void
*/
public function register()
{
$this->bindEventSourcedRepositories();
$this->bindReadModelRepositories();
$this->registerCommandSubscribers();
$this->registerEventSubscribers();
$this->registerConsoleCommands();
}
public function boot()
{
}
/**
* Bind repositories
*/
private function bindEventSourcedRepositories()
{
$this->app->bind(YourRepository::class, function ($app) {
$eventStore = $app[\Broadway\EventStore\EventStore::class];
$eventBus = $app[\Broadway\EventHandling\EventBus::class];
return new YourRepository($eventStore, $eventBus);
});
}
/**
* Bind the read model repositories in the IoC container
*/
private function bindReadModelRepositories()
{
$this->app->bind(YourReadModelRepository::class, function ($app) {
$connection = $app[DynamoDbClient::class];
return new YourReadModelRepository($connection);
});
}
/**
* Register the command handlers on the command bus
*/
private function registerCommandSubscribers()
{
$yourCommandHandler = new YourCommandHandler($this->app[YourRepository::class]);
$this->app['laravelbroadway.command.registry']->subscribe([
$yourCommandHandler
]);
}
/**
* Register the event listeners on the event bus
*/
private function registerEventSubscribers()
{
$yourProjector = new YourProjector(
$this->app[YourRepository::class]
);
$this->app['laravelbroadway.event.registry']->subscribe([
$yourProjector
]);
}
调试
该库中已安装PHPStan,因此如果您想检查代码,可以在您的cli中启动
vendor/bin/phpstan analyse
测试
所有测试都为绿色非常重要。要运行测试,您需要docker运行,因此您需要
docker-compose up -d
./vendor/bin/phpunit
该库中已安装Psalm,如果您想检查代码,可以在您的cli中启动
vendor/bin/psalm