inmanturbo/signal
最新稳定版本:v1.0.3
Composer 安装命令:
composer create-project inmanturbo/signal
包简介
The skeleton application for the Laravel framework.
关键字:
README 文档
README
Usage
composer require inmanturbo/signal
php artisan signal:configure
php artisan signal:migrate
app(Signal::class)($command);
<?php use Inmanturbo\Signal\EventWithData; use Inmanturbo\Signal\Signal; use Spatie\EventSourcing\AggregateRoots\AggregateRoot; use Spatie\EventSourcing\Commands\AggregateUuid; use Spatie\EventSourcing\Commands\HandledBy; use Spatie\EventSourcing\Snapshots\EloquentSnapshot; use Spatie\LaravelData\Data; it('can add item to cart', function (): void { $product = new Product( '123', 2, 100, ); app(Signal::class)(new AddCartItem( 'fake-uuid', 'fake-uuid2', $product, )); app(Signal::class)(new AddCartItem( 'fake-uuid', 'fake-uuid3', $product, )); $cart = CartAggregateRoot::retrieve('fake-uuid'); $cart->snapshot(); app(Signal::class)(AddCartItem::from([ 'cartUuid' => 'fake-uuid', 'cartItemUuid' => 'fake-uuid4', 'product' => $product, ])); $cart = CartAggregateRoot::retrieve('fake-uuid'); expect($cart->total)->toBe(600); expect(count($cart->cartItems))->toBe(3); expect(EloquentSnapshot::count())->toBe(1); }); class Product extends Data { public function __construct( public string $sku, public int $quantity, public int $price, ) {} } class CartItemAdded extends EventWithData { public function __construct( #[AggregateUuid] public string $cartUuid, public string $cartItemUuid, public Product $product, ) {} } #[HandledBy(CartAggregateRoot::class)] class AddCartItem extends CartItemAdded { // } class CartAggregateRoot extends AggregateRoot { public $total; public array $cartItems; public function addItem( AddCartItem $addCartItem ): self { $this->recordThat( CartItemAdded::from($addCartItem->toArray()), ); return $this; } public function applyAddItem(CartItemAdded $event) { $this->total += $amount = ($event->product->quantity * $event->product->price); $this->cartItems[] = (object) [ 'uuid' => $event->cartItemUuid, 'product' => $event->product, 'amount' => $amount, ]; } }
统计信息
- 总下载量: 104
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-01