事件驅動架構(EDA)專注於解耦系統,並透過回應事件使系統更加靈活、可擴展和可維護。在 PHP 中,EDA 中經常使用的兩個重要模式是事件溯源和命令查詢職責分離(CQRS)。這是使用 PHP 實作它們的逐步指南,以及實作範例。
指令:
活動:
讀取模型:
建立目錄結構:
event-driven-php/ ├── src/ │ ├── Commands/ │ ├── Events/ │ ├── Handlers/ │ ├── Models/ │ └── ReadModels/ ├── tests/ └── vendor/
安裝依賴項(例如 symfony/event-dispatcher):
composer require symfony/event-dispatcher
指令代表改變狀態的動作。範例:PlaceOrderCommand.php。
// src/Commands/PlaceOrderCommand.php class PlaceOrderCommand { public string $orderId; public string $customerId; public function __construct(string $orderId, string $customerId) { $this->orderId = $orderId; $this->customerId = $customerId; } }
事件描述了系統中發生的事情。範例:OrderPlacedEvent.php。
// src/Events/OrderPlacedEvent.php class OrderPlacedEvent { public string $orderId; public string $customerId; public function __construct(string $orderId, string $customerId) { $this->orderId = $orderId; $this->customerId = $customerId; } }
命令處理程序執行實際的業務邏輯並引發事件。範例:PlaceOrderHandler.php。
// src/Handlers/PlaceOrderHandler.php use Symfony\Component\EventDispatcher\EventDispatcher; class PlaceOrderHandler { private EventDispatcher $eventDispatcher; public function __construct(EventDispatcher $eventDispatcher) { $this->eventDispatcher = $eventDispatcher; } public function handle(PlaceOrderCommand $command) { // Business logic (e.g., check stock, validate order) // Emit the event $event = new OrderPlacedEvent($command->orderId, $command->customerId); $this->eventDispatcher->dispatch($event, 'order.placed'); } }
事件處理程序偵聽特定事件並更新讀取模型。範例:OrderProjection.php。
// src/ReadModels/OrderProjection.php class OrderProjection { private array $orders = []; public function onOrderPlaced(OrderPlacedEvent $event) { // Save or update read model with necessary data $this->orders[$event->orderId] = [ 'orderId' => $event->orderId, 'customerId' => $event->customerId, 'status' => 'placed' ]; } public function getOrder(string $orderId) { return $this->orders[$orderId] ?? null; } }
use Symfony\Component\EventDispatcher\EventDispatcher; // Bootstrapping the system $dispatcher = new EventDispatcher(); $orderProjection = new OrderProjection(); // Register event listeners $dispatcher->addListener('order.placed', [$orderProjection, 'onOrderPlaced']); // Create the command and command handler $command = new PlaceOrderCommand('123', 'cust_001'); $handler = new PlaceOrderHandler($dispatcher); // Handle the command (Place the order) $handler->handle($command); // Query the read model for the order $order = $orderProjection->getOrder('123'); print_r($order);
輸出:
Array ( [orderId] => 123 [customerId] => cust_001 [status] => placed )
對於完整的事件來源,您還需要實作事件儲存將事件儲存到資料庫。
class EventStore { private array $storedEvents = []; public function append(Event $event) { $this->storedEvents[] = $event; } public function getEventsForAggregate(string $aggregateId): array { return array_filter($this->storedEvents, function($event) use ($aggregateId) { return $event->aggregateId === $aggregateId; }); } }
此範例示範了 CQRS 和 Event Sourcing 在 PHP 中的簡單應用。透過這些模式,您可以建立可擴展且可維護的系統,同時提供強大的可審核性和靈活的讀取/寫入處理。該架構可以隨著額外的投影、更複雜的事件處理以及訊息佇列或第三方通知等外部整合而成長。
以上是在 PHP 中實作事件驅動架構:深入研究事件溯源與 CQRS的詳細內容。更多資訊請關注PHP中文網其他相關文章!