From 2026f2499dd21f174a7249fd7bf8e788860727c5 Mon Sep 17 00:00:00 2001 From: Beau Simensen Date: Sat, 8 Jul 2023 15:57:26 -0500 Subject: [PATCH] Add support for defining message decorators --- ...egateRootRepositoryRegistrationBuilder.php | 44 ++++++++++++++++++- ...egateRootRepositoryRegistrationBuilder.php | 44 ++++++++++++++++++- src/EventSauceConfiguration.php | 14 +++++- 3 files changed, 99 insertions(+), 3 deletions(-) diff --git a/src/Container/EloquentAggregateRootRepositoryRegistrationBuilder.php b/src/Container/EloquentAggregateRootRepositoryRegistrationBuilder.php index 7531bf1..6db0c37 100644 --- a/src/Container/EloquentAggregateRootRepositoryRegistrationBuilder.php +++ b/src/Container/EloquentAggregateRootRepositoryRegistrationBuilder.php @@ -4,11 +4,13 @@ namespace Dflydev\EventSauce\SupportForLaravel\Container; +use Dflydev\EventSauce\Support\MessagePreparation\DefaultMessagePreparation; use Dflydev\EventSauce\Support\Transaction\Transaction; use Dflydev\EventSauce\SupportForLaravel\AggregateRoot\EloquentAggregateRoot; use Dflydev\EventSauce\SupportForLaravel\AggregateRoot\EloquentAggregateRootRepository; use Dflydev\EventSauce\SupportForLaravel\EventSauceConfiguration; use EventSauce\EventSourcing\AggregateRootRepository; +use EventSauce\EventSourcing\MessageDecorator; use EventSauce\EventSourcing\MessageDispatcher; use EventSauce\EventSourcing\MessageRepository; use EventSauce\EventSourcing\SynchronousMessageDispatcher; @@ -27,6 +29,11 @@ final class EloquentAggregateRootRepositoryRegistrationBuilder */ private OutboxRepository|string|null $outboxRepository; + /** + * @var MessageDecorator|class-string|null + */ + private MessageDecorator|string|null $messageDecorator; + /** * @var MessageDispatcher|class-string|null */ @@ -40,6 +47,7 @@ final class EloquentAggregateRootRepositoryRegistrationBuilder private bool $withoutOutboxRepository = false; private bool $withoutTransactionalMessageDispatcher = false; private bool $withoutSynchronousMessageDispatcher = false; + private bool $withoutMessageDecorator = false; private function __construct() { @@ -72,6 +80,17 @@ public function withOutboxRepository(null|string|OutboxRepository $outboxReposit return $instance; } + /** + * @param class-string|MessageDecorator|null $messageDecorator + */ + public function withMessageDecorator(null|string|MessageDecorator $messageDecorator = null): self + { + $instance = clone $this; + $instance->messageDecorator = $messageDecorator ?? MessageDecorator::class; + + return $instance; + } + /** * @param class-string|MessageDispatcher|null $transactionalMessageDispatcher */ @@ -110,6 +129,14 @@ public function withoutOutboxRepository(): self return $instance; } + public function withoutMessageDecorator(): self + { + $instance = clone $this; + $instance->withoutMessageDecorator = true; + + return $instance; + } + public function withoutTransactionalMessageDispatcher(): self { $instance = clone $this; @@ -168,7 +195,7 @@ public function build( if (isset($this->outboxRepository)) { $args['outboxRepository'] = is_object($this->outboxRepository) ? $this->outboxRepository : $app->get($this->outboxRepository); } elseif ($app->bound(OutboxRepository::class)) { - $args['transactionalMessageDispatcher'] = $app->get(OutboxRepository::class); + $args['outboxRepository'] = $app->get(OutboxRepository::class); } } @@ -188,6 +215,21 @@ public function build( } } + /** @var array{'messageDecorator'?: MessageDecorator|null} $messagePreparationArgs */ + $messagePreparationArgs = []; + + if (!$this->withoutMessageDecorator) { + if (isset($this->messageDecorator)) { + $messagePreparationArgs['messageDecorator'] = is_object($this->messageDecorator) ? $this->messageDecorator : $app->get($this->messageDecorator); + } elseif ($app->bound(MessageDecorator::class)) { + $messagePreparationArgs['messageDecorator'] = $app->get(MessageDecorator::class); + } + } + + if (count($messagePreparationArgs)) { + $args['messagePreparation'] = new DefaultMessagePreparation(...$messagePreparationArgs); + } + return new EloquentAggregateRootRepository(...$args); }); } diff --git a/src/Container/EventSourcedAggregateRootRepositoryRegistrationBuilder.php b/src/Container/EventSourcedAggregateRootRepositoryRegistrationBuilder.php index 021c057..558bb72 100644 --- a/src/Container/EventSourcedAggregateRootRepositoryRegistrationBuilder.php +++ b/src/Container/EventSourcedAggregateRootRepositoryRegistrationBuilder.php @@ -6,9 +6,11 @@ use Dflydev\EventSauce\Support\AggregateRoot\EventSourcedAggregateRoot; use Dflydev\EventSauce\Support\AggregateRoot\EventSourcedAggregateRootRepository; +use Dflydev\EventSauce\Support\MessagePreparation\DefaultMessagePreparation; use Dflydev\EventSauce\Support\Transaction\Transaction; use Dflydev\EventSauce\SupportForLaravel\EventSauceConfiguration; use EventSauce\EventSourcing\AggregateRootRepository; +use EventSauce\EventSourcing\MessageDecorator; use EventSauce\EventSourcing\MessageDispatcher; use EventSauce\EventSourcing\MessageRepository; use EventSauce\EventSourcing\SynchronousMessageDispatcher; @@ -27,6 +29,11 @@ final class EventSourcedAggregateRootRepositoryRegistrationBuilder */ private OutboxRepository|string|null $outboxRepository; + /** + * @var MessageDecorator|class-string|null + */ + private MessageDecorator|string|null $messageDecorator; + /** * @var MessageDispatcher|class-string|null */ @@ -39,6 +46,7 @@ final class EventSourcedAggregateRootRepositoryRegistrationBuilder private bool $withoutOutboxRepository = false; private bool $withoutTransactionalMessageDispatcher = false; private bool $withoutSynchronousMessageDispatcher = false; + private bool $withoutMessageDecorator = false; private function __construct() { @@ -71,6 +79,17 @@ public function withOutboxRepository(null|string|OutboxRepository $outboxReposit return $instance; } + /** + * @param class-string|MessageDecorator|null $messageDecorator + */ + public function withMessageDecorator(null|string|MessageDecorator $messageDecorator = null): self + { + $instance = clone $this; + $instance->messageDecorator = $messageDecorator ?? MessageDecorator::class; + + return $instance; + } + /** * @param class-string|MessageDispatcher|null $transactionalMessageDispatcher */ @@ -101,6 +120,14 @@ public function withoutOutboxRepository(): self return $instance; } + public function withoutMessageDecorator(): self + { + $instance = clone $this; + $instance->withoutMessageDecorator = true; + + return $instance; + } + public function withoutTransactionalMessageDispatcher(): self { $instance = clone $this; @@ -162,7 +189,7 @@ public function build( if (isset($this->outboxRepository)) { $args['outboxRepository'] = is_object($this->outboxRepository) ? $this->outboxRepository : $app->get($this->outboxRepository); } elseif ($app->bound(OutboxRepository::class)) { - $args['transactionalMessageDispatcher'] = $app->get(OutboxRepository::class); + $args['outboxRepository'] = $app->get(OutboxRepository::class); } } @@ -182,6 +209,21 @@ public function build( } } + /** @var array{'messageDecorator'?: MessageDecorator|null} $messagePreparationArgs */ + $messagePreparationArgs = []; + + if (!$this->withoutMessageDecorator) { + if (isset($this->messageDecorator)) { + $messagePreparationArgs['messageDecorator'] = is_object($this->messageDecorator) ? $this->messageDecorator : $app->get($this->messageDecorator); + } elseif ($app->bound(MessageDecorator::class)) { + $messagePreparationArgs['messageDecorator'] = $app->get(MessageDecorator::class); + } + } + + if (count($messagePreparationArgs)) { + $args['messagePreparation'] = new DefaultMessagePreparation(...$messagePreparationArgs); + } + return new EventSourcedAggregateRootRepository(...$args); }); } diff --git a/src/EventSauceConfiguration.php b/src/EventSauceConfiguration.php index 7178f39..619a282 100644 --- a/src/EventSauceConfiguration.php +++ b/src/EventSauceConfiguration.php @@ -36,6 +36,18 @@ final class EventSauceConfiguration { + public static function registerMessageDecorator(string|array $messageConsumerClassName, ?Application $application = null): void + { + $application = $application ?? app(); + + $messageConsumerClassNames = is_array($messageConsumerClassName) ? $messageConsumerClassName : [$messageConsumerClassName]; + + foreach ($messageConsumerClassNames as $messageConsumerClassName) { + $application->singleton($messageConsumerClassName); + $application->tag($messageConsumerClassName, MessageDecorator::class); + } + } + /** * @param class-string $aggregateRootClassName */ @@ -333,7 +345,7 @@ public static function registerPayloadSerializer(?string $class = null, ?Applica $application->singleton(PayloadSerializer::class, $class); } - public static function registerMessageDecorator(?Application $application = null): void + public static function registerDefaultMessageDecorator(?Application $application = null): void { $application = $application ?? app();