├── .gitignore ├── phpstan.neon.dist ├── .editorconfig ├── src ├── Database │ └── IlluminateConnectionTransaction.php ├── Eloquent │ ├── CastIdentityToString.php │ └── UuidAsPrimaryKey.php ├── AggregateRoot │ ├── EloquentAggregateRoot.php │ ├── EloquentAggregateRootBehaviour.php │ └── EloquentAggregateRootRepository.php ├── Configuration │ ├── ApplicationBoundTestConfiguration.php │ └── ApplicationBoundMessageConsumerConfiguration.php ├── Testing │ ├── IlluminateMessageConsumerTesting.php │ └── EloquentAggregateRootTestingBehavior.php ├── Container │ ├── EventSourcedAggregateRootRepositoryRegistrationBuilder.php │ └── EloquentAggregateRootRepositoryRegistrationBuilder.php └── EventSauceConfiguration.php ├── composer.json ├── .github └── workflows │ └── phpstan.yml ├── LICENSE ├── phpstan-baseline.neon └── .php-cs-fixer.dist.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | .php-cs-fixer.cache 3 | composer.lock 4 | -------------------------------------------------------------------------------- /phpstan.neon.dist: -------------------------------------------------------------------------------- 1 | includes: 2 | - phpstan-baseline.neon 3 | - ./vendor/nunomaduro/larastan/extension.neon 4 | 5 | parameters: 6 | 7 | paths: 8 | - src/ 9 | 10 | # Level 9 is the highest level 11 | level: max 12 | 13 | checkMissingIterableValueType: false 14 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | indent_size = 4 7 | indent_style = space 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | 14 | [*.{yml,yaml}] 15 | indent_size = 2 16 | 17 | [*.{neon,neon.dist}] 18 | indent_style = tab 19 | 20 | [docker-compose.yml] 21 | indent_size = 4 22 | 23 | [*.{js,ts,cjs,vue,tsx}] 24 | indent_size = 2 25 | -------------------------------------------------------------------------------- /src/Database/IlluminateConnectionTransaction.php: -------------------------------------------------------------------------------- 1 | connection->beginTransaction(); 19 | } 20 | 21 | public function commit(): void 22 | { 23 | $this->connection->commit(); 24 | } 25 | 26 | public function rollBack(): void 27 | { 28 | $this->connection->rollBack(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Eloquent/CastIdentityToString.php: -------------------------------------------------------------------------------- 1 | toString(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/AggregateRoot/EloquentAggregateRoot.php: -------------------------------------------------------------------------------- 1 | 16 | */ 17 | interface EloquentAggregateRoot extends EventedAggregateRoot 18 | { 19 | public static function aggregateRootIdColumnName(): string; 20 | 21 | /** 22 | * @phpstan-param T2 $aggregateRootId 23 | * 24 | * @return EloquentAggregateRoot 25 | */ 26 | public static function findByAggregateRootId(AggregateRootId $aggregateRootId): EloquentAggregateRoot; 27 | 28 | public function persistAggregateRoot(): void; 29 | } 30 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dflydev/eventsauce-support-for-laravel", 3 | "type": "library", 4 | "license": "MIT", 5 | "require": { 6 | "php": "^8.1", 7 | "eventsauce/eventsauce": "^3", 8 | "eventsauce/message-outbox-for-illuminate": "^0.4", 9 | "eventsauce/message-repository-for-illuminate": "^0.4", 10 | "dflydev/eventsauce-support": "^0@dev" 11 | }, 12 | "autoload": { 13 | "psr-4": { 14 | "Dflydev\\EventSauce\\SupportForLaravel\\": "src/" 15 | } 16 | }, 17 | "extra": { 18 | "branch-alias": { 19 | "dev-main": "0.x-dev" 20 | } 21 | }, 22 | "require-dev": { 23 | "friendsofphp/php-cs-fixer": "^3.15.1", 24 | "phpstan/phpstan": "^1.10", 25 | "nunomaduro/larastan": "^2.5", 26 | "orchestra/testbench": "^8.0" 27 | }, 28 | "config": { 29 | "allow-plugins": { 30 | "ergebnis/composer-normalize": true 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /.github/workflows/phpstan.yml: -------------------------------------------------------------------------------- 1 | name: PHPStan 2 | 3 | on: 4 | push: 5 | pull_request: 6 | branches: 7 | - main 8 | 9 | concurrency: 10 | group: phpstan-${{ github.ref_name || github.run_id }} 11 | cancel-in-progress: true 12 | 13 | jobs: 14 | 15 | phpstan: 16 | name: PHPStan PHP ${{ matrix.php }} 17 | runs-on: ubuntu-latest 18 | strategy: 19 | fail-fast: false 20 | matrix: 21 | php: ['8.2'] 22 | 23 | steps: 24 | - name: Checkout 25 | uses: actions/checkout@v3 26 | with: 27 | fetch-depth: 1 28 | 29 | - name: Set up PHP 30 | uses: shivammathur/setup-php@v2 31 | with: 32 | php-version: ${{ matrix.php }} 33 | 34 | - name: Install Composer dependencies 35 | uses: ramsey/composer-install@v2 36 | with: 37 | composer-options: "--no-progress --prefer-dist --optimize-autoloader" 38 | 39 | - name: Larastan 40 | run: php ./vendor/bin/phpstan analyse --memory-limit=2G --error-format=github 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 dflydev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Eloquent/UuidAsPrimaryKey.php: -------------------------------------------------------------------------------- 1 | incrementing = false; 17 | $this->keyType = 'string'; 18 | 19 | $this->primaryKey = static::aggregateRootIdColumnName(); 20 | 21 | if (!array_key_exists(static::aggregateRootIdColumnName(), $this->casts)) { 22 | $this->casts[static::aggregateRootIdColumnName()] = static::aggregateRootIdClassName(); 23 | } 24 | } 25 | 26 | public static function bootUuidAsPrimaryKey(): void 27 | { 28 | static::creating(function (self $model) { 29 | $primaryKey = $model->getKeyName(); 30 | 31 | if (!$model->$primaryKey) { 32 | /** @phpstan-var class-string $class */ 33 | $class = $model::aggregateRootIdClassName(); 34 | 35 | $model->{$primaryKey} = $class::generate(); 36 | } 37 | }); 38 | } 39 | 40 | public function getKeyType(): string 41 | { 42 | return $this->keyType; 43 | } 44 | 45 | public function getIncrementing(): bool 46 | { 47 | return $this->incrementing; 48 | } 49 | 50 | public function getForeignKey() 51 | { 52 | return $this->primaryKey; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /phpstan-baseline.neon: -------------------------------------------------------------------------------- 1 | parameters: 2 | ignoreErrors: 3 | - 4 | message: "#^Method Dflydev\\\\EventSauce\\\\SupportForLaravel\\\\Container\\\\EloquentAggregateRootRepositoryRegistrationBuilder\\:\\:build\\(\\) has parameter \\$aggregateRootClassName with generic interface Dflydev\\\\EventSauce\\\\SupportForLaravel\\\\AggregateRoot\\\\EloquentAggregateRoot but does not specify its types\\: T1, T2$#" 5 | count: 1 6 | path: src/Container/EloquentAggregateRootRepositoryRegistrationBuilder.php 7 | 8 | - 9 | message: "#^Method Dflydev\\\\EventSauce\\\\SupportForLaravel\\\\Container\\\\EventSourcedAggregateRootRepositoryRegistrationBuilder\\:\\:build\\(\\) has parameter \\$aggregateRootClassName with generic interface Dflydev\\\\EventSauce\\\\Support\\\\AggregateRoot\\\\EventSourcedAggregateRoot but does not specify its types\\: T1, T2$#" 10 | count: 1 11 | path: src/Container/EventSourcedAggregateRootRepositoryRegistrationBuilder.php 12 | 13 | - 14 | message: "#^Method Dflydev\\\\EventSauce\\\\SupportForLaravel\\\\EventSauceConfiguration\\:\\:eventSauceRepositoryFor\\(\\) has parameter \\$aggregateRootClassName with generic interface EventSauce\\\\EventSourcing\\\\AggregateRoot but does not specify its types\\: AggregateRootIdType$#" 15 | count: 1 16 | path: src/EventSauceConfiguration.php 17 | 18 | - 19 | message: "#^Method Dflydev\\\\EventSauce\\\\SupportForLaravel\\\\EventSauceConfiguration\\:\\:eventSauceRepositoryFor\\(\\) return type with generic interface EventSauce\\\\EventSourcing\\\\AggregateRootRepository does not specify its types\\: T$#" 20 | count: 1 21 | path: src/EventSauceConfiguration.php 22 | 23 | - 24 | message: "#^Method Dflydev\\\\EventSauce\\\\SupportForLaravel\\\\EventSauceConfiguration\\:\\:eventSauceRepositoryServiceName\\(\\) has parameter \\$aggregateRootClassName with generic interface EventSauce\\\\EventSourcing\\\\AggregateRoot but does not specify its types\\: AggregateRootIdType$#" 25 | count: 1 26 | path: src/EventSauceConfiguration.php 27 | -------------------------------------------------------------------------------- /src/Configuration/ApplicationBoundTestConfiguration.php: -------------------------------------------------------------------------------- 1 | application = $application ?? app(); 19 | } 20 | 21 | public function fakeSynchronousMessageDispatcher(?MessageDispatcher $messageDispatcher = null): self 22 | { 23 | EventSauceConfiguration::fakeSynchronousMessageDispatcher($messageDispatcher, $this->application); 24 | 25 | return $this; 26 | } 27 | 28 | public function fakeTransactionalMessageDispatcher(?MessageDispatcher $messageDispatcher = null): self 29 | { 30 | EventSauceConfiguration::fakeTransactionalMessageDispatcher($messageDispatcher, $this->application); 31 | 32 | return $this; 33 | } 34 | 35 | public function fakeAsynchronousMessageDispatcher(?MessageDispatcher $messageDispatcher = null): self 36 | { 37 | EventSauceConfiguration::fakeAsynchronousMessageDispatcher($messageDispatcher, $this->application); 38 | 39 | return $this; 40 | } 41 | 42 | public function fakeMessageRepository(?MessageRepository $messageRepository = null): self 43 | { 44 | EventSauceConfiguration::fakeMessageRepository($messageRepository, $this->application); 45 | 46 | return $this; 47 | } 48 | 49 | public function fakeMessageDispatching(): self 50 | { 51 | return $this 52 | ->fakeSynchronousMessageDispatcher() 53 | ->fakeAsynchronousMessageDispatcher() 54 | ->fakeTransactionalMessageDispatcher(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /.php-cs-fixer.dist.php: -------------------------------------------------------------------------------- 1 | true, 10 | '@Symfony' => true, 11 | 12 | 'declare_strict_types' => true, 13 | 14 | // Override @Symfony 15 | 16 | 'phpdoc_align' => [ 17 | 'align' => 'left', 18 | 'tags' => [ 19 | 'method', 20 | 'param', 21 | 'property', 22 | 'property-read', 23 | 'property-write', 24 | 'return', 25 | 'throws', 26 | 'type', 27 | 'var', 28 | ], 29 | ], 30 | 31 | 'phpdoc_separation' => [ 32 | 'groups' => [ 33 | // Defaults 34 | ['deprecated', 'link', 'see', 'since'], 35 | ['author', 'copyright', 'license'], 36 | ['category', 'package', 'subpackage'], 37 | ['property', 'property-read', 'property-write'], 38 | 39 | // Overrides 40 | ['template', 'template-covariant', 'template-uses'], 41 | 42 | ['uses'], 43 | 44 | ['phpstan-*'], // This is not available... yet. 45 | 46 | ['phpstan-param', 'phpstan-return'], 47 | ], 48 | ], 49 | 50 | 'global_namespace_import' => [ 51 | 'import_classes' => null, 52 | 'import_constants' => true, 53 | 'import_functions' => true, 54 | ], 55 | 'no_empty_comment' => false, 56 | 'php_unit_method_casing' => ['case' => 'snake_case'], 57 | 'single_line_throw' => false, 58 | 'yoda_style' => false, 59 | 60 | 'phpdoc_to_comment' => [ 61 | 'ignored_tags' => [ 62 | 'array', 63 | 'var', 64 | 'phpstan-var', 65 | ], 66 | ], 67 | ]; 68 | 69 | $finder = Finder::create() 70 | ->in([ 71 | __DIR__.'/src', 72 | ]) 73 | ->name('*.php') 74 | ->ignoreDotFiles(true) 75 | ->ignoreVCS(true); 76 | 77 | return (new Config()) 78 | ->setFinder($finder) 79 | ->setRules($rules) 80 | ->setRiskyAllowed(true) 81 | ->setUsingCache(true); 82 | -------------------------------------------------------------------------------- /src/Configuration/ApplicationBoundMessageConsumerConfiguration.php: -------------------------------------------------------------------------------- 1 | application = $application ?? app(); 17 | } 18 | 19 | public function registerNonLazySynchronousMessageConsumer(string ...$messageConsumerClassNames): void 20 | { 21 | EventSauceConfiguration::registerNonLazySynchronousMessageConsumer($messageConsumerClassNames, $this->application); 22 | } 23 | 24 | public function registerSynchronousMessageConsumer(string ...$messageConsumerClassNames): void 25 | { 26 | EventSauceConfiguration::registerSynchronousMessageConsumer($messageConsumerClassNames, $this->application); 27 | } 28 | 29 | public function registerNonLazyAsynchronousMessageConsumer(string ...$messageConsumerClassNames): void 30 | { 31 | EventSauceConfiguration::registerNonLazyAsynchronousMessageConsumer($messageConsumerClassNames, $this->application); 32 | } 33 | 34 | public function registerAsynchronousMessageConsumer(string ...$messageConsumerClassNames): void 35 | { 36 | EventSauceConfiguration::registerAsynchronousMessageConsumer($messageConsumerClassNames, $this->application); 37 | } 38 | 39 | public function registerNonLazyTransactionalMessageConsumer(string ...$messageConsumerClassNames): void 40 | { 41 | EventSauceConfiguration::registerNonLazyTransactionalMessageConsumer($messageConsumerClassNames, $this->application); 42 | } 43 | 44 | public function registerTransactionalMessageConsumer(string ...$messageConsumerClassNames): void 45 | { 46 | EventSauceConfiguration::registerTransactionalMessageConsumer($messageConsumerClassNames, $this->application); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Testing/IlluminateMessageConsumerTesting.php: -------------------------------------------------------------------------------- 1 | messageRepositoryForMessageConsumerTesting)) { 33 | throw new \LogicException('Message repository for message consumer testing already set.'); 34 | } 35 | 36 | $this->messageRepositoryForMessageConsumerTesting = $messageRepositoryForMessageConsumerTesting; 37 | } 38 | 39 | protected function getInMemoryMessageRepositoryForMessageConsumerTesting(): InMemoryMessageRepository 40 | { 41 | if (!isset($this->messageRepositoryForMessageConsumerTesting)) { 42 | $this->messageRepositoryForMessageConsumerTesting = new InMemoryMessageRepository(); 43 | } 44 | 45 | return $this->messageRepositoryForMessageConsumerTesting; 46 | } 47 | 48 | /** 49 | * @before 50 | */ 51 | public function configureContainerForMessageConsumerTesting(): void 52 | { 53 | $this->afterApplicationCreated(function () { 54 | $transactionalMessageDispatcher = new LazyMessageDispatcher($this->app, ...$this->getMessageConsumerClassesToTest()); 55 | 56 | EventSauceConfiguration::configureForTesting($this->app) 57 | ->fakeMessageRepository($this->getInMemoryMessageRepositoryForMessageConsumerTesting()) 58 | ->fakeMessageDispatching() 59 | ->fakeTransactionalMessageDispatcher($transactionalMessageDispatcher); 60 | 61 | $this->configureMessageConsumerTesting(); 62 | }); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Testing/EloquentAggregateRootTestingBehavior.php: -------------------------------------------------------------------------------- 1 | 23 | */ 24 | use AggregateRootTestingBehavior; 25 | 26 | /** 27 | * @return AggregateRootRepository 28 | */ 29 | protected function eloquentAggregateRootRepository(): AggregateRootRepository 30 | { 31 | return new EloquentAggregateRootRepository( 32 | $this->aggregateRootType(), 33 | $this->transaction(), 34 | $this->messageRepository(), 35 | $this->messagePreparation() 36 | ); 37 | } 38 | 39 | public function configureForEloquentAggregateRootType(string $aggregateRootType): void 40 | { 41 | self::setAggregateRootType($aggregateRootType); 42 | 43 | $this->scenarioConfiguration = $this->scenarioConfigurationForEloquent(...); 44 | } 45 | 46 | private function scenarioConfigurationForEloquent(Scenario $scenario): Scenario 47 | { 48 | /** @var EloquentAggregateRoot $aggregateRootType */ 49 | $aggregateRootType = self::aggregateRootType(); 50 | 51 | assert(in_array(EloquentAggregateRoot::class, class_implements($aggregateRootType))); 52 | 53 | $generator = function (array $events) { 54 | yield from $events; 55 | 56 | return count($events); 57 | }; 58 | 59 | return $scenario->visitEvents(function (...$events) use ($aggregateRootType, $generator) { 60 | if (count($events) === 0) { 61 | return; 62 | } 63 | 64 | // We do not use the aggregate root repository here, we just need to persist 65 | // it based on the events that were recorded. 66 | $aggregateRootType::reconstituteAndPersistFromEvents($this->aggregateRootId(), $generator($events)); 67 | }); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/AggregateRoot/EloquentAggregateRootBehaviour.php: -------------------------------------------------------------------------------- 1 | 26 | */ 27 | use EventedAggregateRootBehaviour; 28 | 29 | public static function bootEloquentAggregateRootBehaviour(): void 30 | { 31 | static::creating(self::syncAggregateRootVersion(...)); 32 | static::updating(self::syncAggregateRootVersion(...)); 33 | } 34 | 35 | public static function findByAggregateRootId(AggregateRootId $aggregateRootId): EloquentAggregateRoot 36 | { 37 | /** @var EloquentAggregateRoot|null $aggregateRoot */ 38 | $aggregateRoot = static::query() 39 | ->where(self::aggregateRootIdColumnName(), '=', $aggregateRootId->toString()) 40 | ->first(); 41 | 42 | assert(!is_null($aggregateRoot), 'Aggregate Root not found'); 43 | 44 | return $aggregateRoot; 45 | } 46 | 47 | /** 48 | * @phpstan-return T2 49 | */ 50 | public function aggregateRootId(): AggregateRootId 51 | { 52 | return $this->{static::aggregateRootIdColumnName()}; 53 | } 54 | 55 | public function persistAggregateRoot(): void 56 | { 57 | $this->save(); 58 | } 59 | 60 | /** 61 | * @param EloquentAggregateRoot&Model $model 62 | */ 63 | public static function syncAggregateRootVersion(EloquentAggregateRoot $model): void 64 | { 65 | $model->setAttribute('aggregate_root_version', $model->aggregateRootVersion()); 66 | } 67 | 68 | public static function reconstituteAndPersistFromEvents(AggregateRootId $aggregateRootId, \Generator $events): static 69 | { 70 | $aggregateRoot = new self(); 71 | 72 | $aggregateRoot->fill([ 73 | static::aggregateRootIdColumnName() => $aggregateRootId, 74 | ]); 75 | 76 | /** @var object $event */ 77 | foreach ($events as $event) { 78 | $aggregateRoot->apply($event); 79 | } 80 | 81 | $aggregateRootVersion = $events->getReturn(); 82 | 83 | $aggregateRoot->aggregateRootVersion = (is_int($aggregateRootVersion) && $aggregateRootVersion >= 0) 84 | ? $aggregateRootVersion 85 | : 0; 86 | 87 | $aggregateRoot->persistAggregateRoot(); 88 | 89 | return $aggregateRoot; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/AggregateRoot/EloquentAggregateRootRepository.php: -------------------------------------------------------------------------------- 1 | 27 | */ 28 | final readonly class EloquentAggregateRootRepository implements AggregateRootRepository 29 | { 30 | private MessageRepository|null $messageRepository; 31 | private MessagePreparation $messagePreparation; 32 | 33 | /** 34 | * @param class-string> $aggregateRootClassName 35 | */ 36 | public function __construct( 37 | private string $aggregateRootClassName, 38 | private Transaction $transaction, 39 | ?MessageRepository $messageRepository = null, 40 | ?MessagePreparation $messagePreparation = null, 41 | private ?MessageDispatcher $transactionalMessageDispatcher = null, 42 | private ?MessageDispatcher $synchronousMessageDispatcher = null, 43 | private ?OutboxRepository $outboxRepository = null, 44 | ) { 45 | $this->messageRepository = $messageRepository; 46 | $this->messagePreparation = $messagePreparation ?? new DefaultMessagePreparation(); 47 | } 48 | 49 | /** 50 | * @phpstan-param T2 $aggregateRootId 51 | * 52 | * @return EloquentAggregateRoot 53 | */ 54 | public function retrieve(AggregateRootId $aggregateRootId): EloquentAggregateRoot 55 | { 56 | $aggregateRootClassName = $this->aggregateRootClassName; 57 | 58 | return $aggregateRootClassName::findByAggregateRootId($aggregateRootId); 59 | } 60 | 61 | public function persist(object $aggregateRoot): void 62 | { 63 | assert($aggregateRoot instanceof EloquentAggregateRoot, 'Expected $aggregateRoot to be an instance of '.AggregateRoot::class); 64 | 65 | $messages = $this->messagePreparation->prepareMessages( 66 | $this->aggregateRootClassName, 67 | $aggregateRoot->aggregateRootId(), 68 | $aggregateRoot->aggregateRootVersion(), 69 | ...$aggregateRoot->releaseEvents() 70 | ); 71 | 72 | if (count($messages) === 0) { 73 | return; 74 | } 75 | 76 | try { 77 | $this->transaction->begin(); 78 | 79 | try { 80 | $aggregateRoot->persistAggregateRoot(); 81 | 82 | $this->messageRepository?->persist(...$messages); 83 | $this->outboxRepository?->persist(...$messages); 84 | $this->transactionalMessageDispatcher?->dispatch(...$messages); 85 | 86 | $this->transaction->commit(); 87 | } catch (Throwable $exception) { 88 | $this->transaction->rollBack(); 89 | throw $exception; 90 | } 91 | } catch (Throwable $exception) { 92 | throw UnableToPersistMessages::dueTo('', $exception); 93 | } 94 | 95 | $this->synchronousMessageDispatcher?->dispatch(...$messages); 96 | } 97 | 98 | public function persistEvents(AggregateRootId $aggregateRootId, int $aggregateRootVersion, object ...$events): void 99 | { 100 | // TODO: Implement persistEvents() method. 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/Container/EventSourcedAggregateRootRepositoryRegistrationBuilder.php: -------------------------------------------------------------------------------- 1 | |null 24 | */ 25 | private MessageRepository|string|null $messageRepository; 26 | 27 | /** 28 | * @var OutboxRepository|class-string|null 29 | */ 30 | private OutboxRepository|string|null $outboxRepository; 31 | 32 | /** 33 | * @var MessageDecorator|class-string|null 34 | */ 35 | private MessageDecorator|string|null $messageDecorator; 36 | 37 | /** 38 | * @var MessageDispatcher|class-string|null 39 | */ 40 | private MessageDispatcher|string|null $transactionalMessageDispatcher; 41 | 42 | /** 43 | * @var MessageDispatcher|class-string|null 44 | */ 45 | private MessageDispatcher|string|null $synchronousMessageDispatcher = null; 46 | private bool $withoutOutboxRepository = false; 47 | private bool $withoutTransactionalMessageDispatcher = false; 48 | private bool $withoutSynchronousMessageDispatcher = false; 49 | private bool $withoutMessageDecorator = false; 50 | 51 | private function __construct() 52 | { 53 | } 54 | 55 | public static function new(): self 56 | { 57 | return new self(); 58 | } 59 | 60 | /** 61 | * @param class-string|MessageRepository $messageRepository 62 | */ 63 | public function withMessageRepository(string|MessageRepository $messageRepository): self 64 | { 65 | $instance = clone $this; 66 | $instance->messageRepository = $messageRepository; 67 | 68 | return $instance; 69 | } 70 | 71 | /** 72 | * @param class-string|OutboxRepository|null $outboxRepository 73 | */ 74 | public function withOutboxRepository(null|string|OutboxRepository $outboxRepository = null): self 75 | { 76 | $instance = clone $this; 77 | $instance->outboxRepository = $outboxRepository ?? OutboxRepository::class; 78 | 79 | return $instance; 80 | } 81 | 82 | /** 83 | * @param class-string|MessageDecorator|null $messageDecorator 84 | */ 85 | public function withMessageDecorator(null|string|MessageDecorator $messageDecorator = null): self 86 | { 87 | $instance = clone $this; 88 | $instance->messageDecorator = $messageDecorator ?? MessageDecorator::class; 89 | 90 | return $instance; 91 | } 92 | 93 | /** 94 | * @param class-string|MessageDispatcher|null $transactionalMessageDispatcher 95 | */ 96 | public function withTransactionalMessageDispatcher(null|string|MessageDispatcher $transactionalMessageDispatcher = null): self 97 | { 98 | $instance = clone $this; 99 | $instance->transactionalMessageDispatcher = $transactionalMessageDispatcher ?? SynchronousMessageDispatcher::class; 100 | 101 | return $instance; 102 | } 103 | 104 | /** 105 | * @param class-string|MessageDispatcher|null $synchronousMessageDispatcher 106 | */ 107 | public function withSynchronousMessageDispatcher(null|string|MessageDispatcher $synchronousMessageDispatcher = null): self 108 | { 109 | $instance = clone $this; 110 | $instance->synchronousMessageDispatcher = $synchronousMessageDispatcher ?? SynchronousMessageDispatcher::class; 111 | 112 | return $instance; 113 | } 114 | 115 | public function withoutOutboxRepository(): self 116 | { 117 | $instance = clone $this; 118 | $instance->withoutOutboxRepository = true; 119 | 120 | return $instance; 121 | } 122 | 123 | public function withoutMessageDecorator(): self 124 | { 125 | $instance = clone $this; 126 | $instance->withoutMessageDecorator = true; 127 | 128 | return $instance; 129 | } 130 | 131 | public function withoutTransactionalMessageDispatcher(): self 132 | { 133 | $instance = clone $this; 134 | $instance->withoutTransactionalMessageDispatcher = true; 135 | 136 | return $instance; 137 | } 138 | 139 | public function withoutSynchronousMessageDispatcher(): self 140 | { 141 | $instance = clone $this; 142 | $instance->withoutSynchronousMessageDispatcher = true; 143 | 144 | return $instance; 145 | } 146 | 147 | /** 148 | * @param class-string $aggregateRootClassName 149 | */ 150 | public function build( 151 | string $aggregateRootClassName, 152 | string $aggregateRootRepositoryInterfaceName, 153 | string $aggregateRootRepositoryImplementationClassName, 154 | ?Application $application = null 155 | ): void { 156 | $application = $application ?? app(); 157 | 158 | $innerInstanceName = EventSauceConfiguration::eventSauceRepositoryServiceName($aggregateRootClassName); 159 | 160 | $application->singleton($aggregateRootRepositoryInterfaceName, $aggregateRootRepositoryImplementationClassName); 161 | 162 | $application->singleton($innerInstanceName, EventSourcedAggregateRootRepository::class); 163 | 164 | $application->when($aggregateRootRepositoryImplementationClassName) 165 | ->needs(AggregateRootRepository::class) 166 | ->give($innerInstanceName); 167 | 168 | $application->singleton($innerInstanceName, function (Application $app) use ($aggregateRootClassName) { 169 | /** @var Transaction $transaction */ 170 | $transaction = $app->get(Transaction::class); 171 | 172 | /** @var MessageRepository|class-string|null $messageRepository */ 173 | $messageRepository = $this->messageRepository ?? $app->get(MessageRepository::class); 174 | 175 | if (is_string($messageRepository)) { 176 | /** @var MessageRepository|null $messageRepository */ 177 | $messageRepository = $app->get($messageRepository); 178 | } 179 | 180 | assert(!is_null($messageRepository), 'Message repository is null.'); 181 | 182 | $args = [ 183 | $aggregateRootClassName, 184 | $transaction, 185 | $messageRepository, 186 | ]; 187 | 188 | if (!$this->withoutOutboxRepository) { 189 | if (isset($this->outboxRepository)) { 190 | $args['outboxRepository'] = is_object($this->outboxRepository) ? $this->outboxRepository : $app->get($this->outboxRepository); 191 | } elseif ($app->bound(OutboxRepository::class)) { 192 | $args['outboxRepository'] = $app->get(OutboxRepository::class); 193 | } 194 | } 195 | 196 | if (!$this->withoutTransactionalMessageDispatcher) { 197 | if (isset($this->transactionalMessageDispatcher)) { 198 | $args['transactionalMessageDispatcher'] = is_object($this->transactionalMessageDispatcher) ? $this->transactionalMessageDispatcher : $app->get($this->transactionalMessageDispatcher); 199 | } elseif ($app->bound(EventSauceConfiguration::transactionalMessageDispatcherServiceName())) { 200 | $args['transactionalMessageDispatcher'] = $app->get(EventSauceConfiguration::transactionalMessageDispatcherServiceName()); 201 | } 202 | } 203 | 204 | if (!$this->withoutSynchronousMessageDispatcher) { 205 | if (isset($this->synchronousMessageDispatcher)) { 206 | $args['synchronousMessageDispatcher'] = is_object($this->synchronousMessageDispatcher) ? $this->synchronousMessageDispatcher : $app->get($this->synchronousMessageDispatcher); 207 | } elseif ($app->bound(EventSauceConfiguration::synchronousMessageDispatcherServiceName())) { 208 | $args['synchronousMessageDispatcher'] = $app->get(EventSauceConfiguration::synchronousMessageDispatcherServiceName()); 209 | } 210 | } 211 | 212 | /** @var array{'messageDecorator'?: MessageDecorator|null} $messagePreparationArgs */ 213 | $messagePreparationArgs = []; 214 | 215 | if (!$this->withoutMessageDecorator) { 216 | if (isset($this->messageDecorator)) { 217 | $messagePreparationArgs['messageDecorator'] = is_object($this->messageDecorator) ? $this->messageDecorator : $app->get($this->messageDecorator); 218 | } elseif ($app->bound(MessageDecorator::class)) { 219 | $messagePreparationArgs['messageDecorator'] = $app->get(MessageDecorator::class); 220 | } 221 | } 222 | 223 | if (count($messagePreparationArgs)) { 224 | $args['messagePreparation'] = new DefaultMessagePreparation(...$messagePreparationArgs); 225 | } 226 | 227 | return new EventSourcedAggregateRootRepository(...$args); 228 | }); 229 | } 230 | } 231 | -------------------------------------------------------------------------------- /src/Container/EloquentAggregateRootRepositoryRegistrationBuilder.php: -------------------------------------------------------------------------------- 1 | |null 24 | */ 25 | private MessageRepository|string|null $messageRepository; 26 | 27 | /** 28 | * @var OutboxRepository|class-string|null 29 | */ 30 | private OutboxRepository|string|null $outboxRepository; 31 | 32 | /** 33 | * @var MessageDecorator|class-string|null 34 | */ 35 | private MessageDecorator|string|null $messageDecorator; 36 | 37 | /** 38 | * @var MessageDispatcher|class-string|null 39 | */ 40 | private MessageDispatcher|string|null $transactionalMessageDispatcher; 41 | 42 | /** 43 | * @var MessageDispatcher|class-string|null 44 | */ 45 | private MessageDispatcher|string|null $synchronousMessageDispatcher = null; 46 | private bool $withoutMessageRepository = false; 47 | private bool $withoutOutboxRepository = false; 48 | private bool $withoutTransactionalMessageDispatcher = false; 49 | private bool $withoutSynchronousMessageDispatcher = false; 50 | private bool $withoutMessageDecorator = false; 51 | 52 | private function __construct() 53 | { 54 | } 55 | 56 | public static function new(): self 57 | { 58 | return new self(); 59 | } 60 | 61 | /** 62 | * @param class-string|MessageRepository $messageRepository 63 | */ 64 | public function withMessageRepository(string|MessageRepository $messageRepository): self 65 | { 66 | $instance = clone $this; 67 | $instance->messageRepository = $messageRepository; 68 | 69 | return $instance; 70 | } 71 | 72 | /** 73 | * @param class-string|OutboxRepository|null $outboxRepository 74 | */ 75 | public function withOutboxRepository(null|string|OutboxRepository $outboxRepository = null): self 76 | { 77 | $instance = clone $this; 78 | $instance->outboxRepository = $outboxRepository ?? OutboxRepository::class; 79 | 80 | return $instance; 81 | } 82 | 83 | /** 84 | * @param class-string|MessageDecorator|null $messageDecorator 85 | */ 86 | public function withMessageDecorator(null|string|MessageDecorator $messageDecorator = null): self 87 | { 88 | $instance = clone $this; 89 | $instance->messageDecorator = $messageDecorator ?? MessageDecorator::class; 90 | 91 | return $instance; 92 | } 93 | 94 | /** 95 | * @param class-string|MessageDispatcher|null $transactionalMessageDispatcher 96 | */ 97 | public function withTransactionalMessageDispatcher(null|string|MessageDispatcher $transactionalMessageDispatcher = null): self 98 | { 99 | $instance = clone $this; 100 | $instance->transactionalMessageDispatcher = $transactionalMessageDispatcher ?? SynchronousMessageDispatcher::class; 101 | 102 | return $instance; 103 | } 104 | 105 | /** 106 | * @param class-string|MessageDispatcher|null $synchronousMessageDispatcher 107 | */ 108 | public function withSynchronousMessageDispatcher(null|string|MessageDispatcher $synchronousMessageDispatcher = null): self 109 | { 110 | $instance = clone $this; 111 | $instance->synchronousMessageDispatcher = $synchronousMessageDispatcher ?? SynchronousMessageDispatcher::class; 112 | 113 | return $instance; 114 | } 115 | 116 | public function withoutMessageRepository(): self 117 | { 118 | $instance = clone $this; 119 | $instance->withoutMessageRepository = true; 120 | 121 | return $instance; 122 | } 123 | 124 | public function withoutOutboxRepository(): self 125 | { 126 | $instance = clone $this; 127 | $instance->withoutOutboxRepository = true; 128 | 129 | return $instance; 130 | } 131 | 132 | public function withoutMessageDecorator(): self 133 | { 134 | $instance = clone $this; 135 | $instance->withoutMessageDecorator = true; 136 | 137 | return $instance; 138 | } 139 | 140 | public function withoutTransactionalMessageDispatcher(): self 141 | { 142 | $instance = clone $this; 143 | $instance->withoutTransactionalMessageDispatcher = true; 144 | 145 | return $instance; 146 | } 147 | 148 | public function withoutSynchronousMessageDispatcher(): self 149 | { 150 | $instance = clone $this; 151 | $instance->withoutSynchronousMessageDispatcher = true; 152 | 153 | return $instance; 154 | } 155 | 156 | /** 157 | * @param class-string $aggregateRootClassName 158 | */ 159 | public function build( 160 | string $aggregateRootClassName, 161 | string $aggregateRootRepositoryInterfaceName, 162 | string $aggregateRootRepositoryImplementationClassName, 163 | ?Application $application = null 164 | ): void { 165 | $application = $application ?? app(); 166 | 167 | $innerInstanceName = EventSauceConfiguration::eventSauceRepositoryServiceName($aggregateRootClassName); 168 | 169 | $application->singleton($aggregateRootRepositoryInterfaceName, $aggregateRootRepositoryImplementationClassName); 170 | 171 | $application->singleton($innerInstanceName, EloquentAggregateRootRepository::class); 172 | 173 | $application->when($aggregateRootRepositoryImplementationClassName) 174 | ->needs(AggregateRootRepository::class) 175 | ->give($innerInstanceName); 176 | 177 | $application->singleton($innerInstanceName, function (Application $app) use ($aggregateRootClassName) { 178 | /** @var Transaction $transaction */ 179 | $transaction = $app->get(Transaction::class); 180 | 181 | $args = [ 182 | $aggregateRootClassName, 183 | $transaction, 184 | ]; 185 | 186 | if (!$this->withoutMessageRepository) { 187 | if (isset($this->messageRepository)) { 188 | $args['messageRepository'] = is_object($this->messageRepository) ? $this->messageRepository : $app->get($this->messageRepository); 189 | } elseif ($app->bound(MessageRepository::class)) { 190 | $args['messageRepository'] = $app->get(MessageRepository::class); 191 | } 192 | } 193 | 194 | if (!$this->withoutOutboxRepository) { 195 | if (isset($this->outboxRepository)) { 196 | $args['outboxRepository'] = is_object($this->outboxRepository) ? $this->outboxRepository : $app->get($this->outboxRepository); 197 | } elseif ($app->bound(OutboxRepository::class)) { 198 | $args['outboxRepository'] = $app->get(OutboxRepository::class); 199 | } 200 | } 201 | 202 | if (!$this->withoutTransactionalMessageDispatcher) { 203 | if (isset($this->transactionalMessageDispatcher)) { 204 | $args['transactionalMessageDispatcher'] = is_object($this->transactionalMessageDispatcher) ? $this->transactionalMessageDispatcher : $app->get($this->transactionalMessageDispatcher); 205 | } elseif ($app->bound(EventSauceConfiguration::transactionalMessageDispatcherServiceName())) { 206 | $args['transactionalMessageDispatcher'] = $app->get(EventSauceConfiguration::transactionalMessageDispatcherServiceName()); 207 | } 208 | } 209 | 210 | if (!$this->withoutSynchronousMessageDispatcher) { 211 | if (isset($this->synchronousMessageDispatcher)) { 212 | $args['synchronousMessageDispatcher'] = is_object($this->synchronousMessageDispatcher) ? $this->synchronousMessageDispatcher : $app->get($this->synchronousMessageDispatcher); 213 | } elseif ($app->bound(EventSauceConfiguration::synchronousMessageDispatcherServiceName())) { 214 | $args['synchronousMessageDispatcher'] = $app->get(EventSauceConfiguration::synchronousMessageDispatcherServiceName()); 215 | } 216 | } 217 | 218 | /** @var array{'messageDecorator'?: MessageDecorator|null} $messagePreparationArgs */ 219 | $messagePreparationArgs = []; 220 | 221 | if (!$this->withoutMessageDecorator) { 222 | if (isset($this->messageDecorator)) { 223 | $messagePreparationArgs['messageDecorator'] = is_object($this->messageDecorator) ? $this->messageDecorator : $app->get($this->messageDecorator); 224 | } elseif ($app->bound(MessageDecorator::class)) { 225 | $messagePreparationArgs['messageDecorator'] = $app->get(MessageDecorator::class); 226 | } 227 | } 228 | 229 | if (count($messagePreparationArgs)) { 230 | $args['messagePreparation'] = new DefaultMessagePreparation(...$messagePreparationArgs); 231 | } 232 | 233 | return new EloquentAggregateRootRepository(...$args); 234 | }); 235 | } 236 | } 237 | -------------------------------------------------------------------------------- /src/EventSauceConfiguration.php: -------------------------------------------------------------------------------- 1 | singleton($messageConsumerClassName); 47 | $application->tag($messageConsumerClassName, MessageDecorator::class); 48 | } 49 | } 50 | 51 | /** 52 | * @param class-string $aggregateRootClassName 53 | */ 54 | public static function eventSauceRepositoryServiceName(string $aggregateRootClassName): string 55 | { 56 | return $aggregateRootClassName.'.EventSauceAggregateRootRepository'; 57 | } 58 | 59 | public static function synchronousMessageDispatcherServiceName(): string 60 | { 61 | return MessageDispatcher::class.'.synchronous'; 62 | } 63 | 64 | public static function asynchronousMessageDispatcherServiceName(): string 65 | { 66 | return MessageDispatcher::class.'.asynchronous'; 67 | } 68 | 69 | public static function transactionalMessageDispatcherServiceName(): string 70 | { 71 | return MessageDispatcher::class.'.transactional'; 72 | } 73 | 74 | public static function consumesMessagesSynchronouslyTagName(): string 75 | { 76 | return MessageConsumer::class.'.synchronous'; 77 | } 78 | 79 | public static function consumesMessagesAsynchronouslyTagName(): string 80 | { 81 | return MessageConsumer::class.'.asynchronous'; 82 | } 83 | 84 | public static function consumesMessagesTransactionallyTagName(): string 85 | { 86 | return MessageConsumer::class.'.transactional'; 87 | } 88 | 89 | public static function registerMessageConsumer(string $tag, string|array $messageConsumerClassName, ?bool $lazy = true, ?Application $application = null): void 90 | { 91 | $application = $application ?? app(); 92 | 93 | $messageConsumerClassNames = is_array($messageConsumerClassName) ? $messageConsumerClassName : [$messageConsumerClassName]; 94 | 95 | foreach ($messageConsumerClassNames as $messageConsumerClassName) { 96 | $application->singleton($messageConsumerClassName); 97 | 98 | if ($lazy) { 99 | $lazyServiceName = $messageConsumerClassName.'.madeLazy'; 100 | $application->singleton($lazyServiceName, fn (Container $container) => new LazyMessageConsumer($container, $messageConsumerClassName)); 101 | $application->tag($lazyServiceName, $tag); 102 | } else { 103 | $application->tag($messageConsumerClassName, $tag); 104 | } 105 | } 106 | } 107 | 108 | public static function configureMessageConsumers(?Application $application = null): ApplicationBoundMessageConsumerConfiguration 109 | { 110 | return new ApplicationBoundMessageConsumerConfiguration($application ?? app()); 111 | } 112 | 113 | public static function configureForTesting(?Application $application = null): ApplicationBoundTestConfiguration 114 | { 115 | return new ApplicationBoundTestConfiguration($application ?? app()); 116 | } 117 | 118 | public static function registerNonLazySynchronousMessageConsumer(string|array $messageConsumerClassName, ?Application $application = null): void 119 | { 120 | self::registerMessageConsumer( 121 | self::consumesMessagesSynchronouslyTagName(), 122 | $messageConsumerClassName, 123 | lazy: false, 124 | application: $application 125 | ); 126 | } 127 | 128 | public static function registerSynchronousMessageConsumer(string|array $messageConsumerClassName, ?Application $application = null): void 129 | { 130 | self::registerMessageConsumer( 131 | self::consumesMessagesSynchronouslyTagName(), 132 | $messageConsumerClassName, 133 | lazy: true, 134 | application: $application 135 | ); 136 | } 137 | 138 | public static function registerNonLazyAsynchronousMessageConsumer(string|array $messageConsumerClassName, ?Application $application = null): void 139 | { 140 | self::registerMessageConsumer( 141 | self::consumesMessagesAsynchronouslyTagName(), 142 | $messageConsumerClassName, 143 | lazy: false, 144 | application: $application 145 | ); 146 | } 147 | 148 | public static function registerAsynchronousMessageConsumer(string|array $messageConsumerClassName, ?Application $application = null): void 149 | { 150 | self::registerMessageConsumer( 151 | self::consumesMessagesAsynchronouslyTagName(), 152 | $messageConsumerClassName, 153 | lazy: true, 154 | application: $application 155 | ); 156 | } 157 | 158 | public static function registerNonLazyTransactionalMessageConsumer(string|array $messageConsumerClassName, ?Application $application = null): void 159 | { 160 | self::registerMessageConsumer( 161 | self::consumesMessagesTransactionallyTagName(), 162 | $messageConsumerClassName, 163 | lazy: false, 164 | application: $application 165 | ); 166 | } 167 | 168 | public static function registerTransactionalMessageConsumer(string|array $messageConsumerClassName, ?Application $application = null): void 169 | { 170 | self::registerMessageConsumer( 171 | self::consumesMessagesTransactionallyTagName(), 172 | $messageConsumerClassName, 173 | application: $application 174 | ); 175 | } 176 | 177 | public static function fakeSynchronousMessageDispatcher(?MessageDispatcher $messageDispatcher = null, ?Application $application = null): void 178 | { 179 | $application = $application ?? app(); 180 | $messageDispatcher = $messageDispatcher ?? new SynchronousMessageDispatcher(); 181 | 182 | $application->singleton(self::synchronousMessageDispatcherServiceName(), fn () => $messageDispatcher); 183 | } 184 | 185 | public static function registerSynchronousMessageDispatcher(?Application $application = null): void 186 | { 187 | $application = $application ?? app(); 188 | 189 | $application->singleton(self::synchronousMessageDispatcherServiceName(), function () use ($application) { 190 | /** @var MessageConsumer[] $messageConsumers */ 191 | $messageConsumers = $application->tagged(self::consumesMessagesSynchronouslyTagName()); 192 | 193 | return new SynchronousMessageDispatcher(...$messageConsumers); 194 | }); 195 | } 196 | 197 | public static function fakeTransactionalMessageDispatcher(?MessageDispatcher $messageDispatcher = null, ?Application $application = null): void 198 | { 199 | $application = $application ?? app(); 200 | $messageDispatcher = $messageDispatcher ?? new SynchronousMessageDispatcher(); 201 | 202 | $application->singleton(self::transactionalMessageDispatcherServiceName(), fn () => $messageDispatcher); 203 | } 204 | 205 | public static function registerTransactionalMessageDispatcher(?Application $application = null): void 206 | { 207 | $application = $application ?? app(); 208 | 209 | $application->singleton(self::transactionalMessageDispatcherServiceName(), function () use ($application) { 210 | /** @var MessageConsumer[] $messageConsumers */ 211 | $messageConsumers = $application->tagged(self::consumesMessagesTransactionallyTagName()); 212 | 213 | return new SynchronousMessageDispatcher(...$messageConsumers); 214 | }); 215 | } 216 | 217 | public static function fakeAsynchronousMessageDispatcher(?MessageDispatcher $messageDispatcher = null, ?Application $application = null): void 218 | { 219 | $application = $application ?? app(); 220 | $messageDispatcher = $messageDispatcher ?? new SynchronousMessageDispatcher(); 221 | 222 | $application->singleton(self::asynchronousMessageDispatcherServiceName(), fn () => $messageDispatcher); 223 | } 224 | 225 | public static function registerAsynchronousMessageDispatcher(?Application $application = null): void 226 | { 227 | $application = $application ?? app(); 228 | 229 | $application->singleton(OutboxRelay::class); 230 | 231 | $application 232 | ->when(OutboxRelay::class) 233 | ->needs(MessageConsumer::class) 234 | ->give(function (Container $application) { 235 | /** @var MessageDispatcher $asynchronousMessageDispatcher */ 236 | $asynchronousMessageDispatcher = $application->get(self::asynchronousMessageDispatcherServiceName()); 237 | 238 | return new class($asynchronousMessageDispatcher) implements MessageConsumer { 239 | public function __construct(private readonly MessageDispatcher $messageDispatcher) 240 | { 241 | } 242 | 243 | public function handle(Message $message): void 244 | { 245 | $this->messageDispatcher->dispatch($message); 246 | } 247 | }; 248 | }); 249 | 250 | $application->singleton(self::asynchronousMessageDispatcherServiceName(), function () use ($application) { 251 | /** @var MessageConsumer[] $messageConsumers */ 252 | $messageConsumers = $application->tagged(self::consumesMessagesAsynchronouslyTagName()); 253 | 254 | return new SynchronousMessageDispatcher(...$messageConsumers); 255 | }); 256 | } 257 | 258 | public static function fakeMessageRepository(?MessageRepository $messageRepository = null, ?Application $application = null): void 259 | { 260 | $application = $application ?? app(); 261 | $messageRepository = $messageRepository ?? new InMemoryMessageRepository(); 262 | 263 | $application->singleton(MessageRepository::class, fn () => $messageRepository); 264 | } 265 | 266 | public static function registerMessageRepository(string $class, ?Application $application = null): void 267 | { 268 | $application = $application ?? app(); 269 | 270 | switch ($class) { 271 | case InMemoryMessageRepository::class: 272 | $application->singleton(MessageRepository::class, InMemoryMessageRepository::class); 273 | 274 | // no break 275 | case IlluminateUuidV4MessageRepository::class: 276 | $application->singleton(MessageRepository::class, IlluminateUuidV4MessageRepository::class); 277 | $application->singleton(IlluminateUuidV4MessageRepository::class); 278 | $application->when(IlluminateUuidV4MessageRepository::class) 279 | ->needs('$tableName') 280 | ->giveConfig('eventsauce.message_repository.table_name'); 281 | 282 | break; 283 | 284 | default: 285 | throw new LogicException(sprintf('Unknown Message Repository class "%s" specified', $class)); 286 | } 287 | } 288 | 289 | public static function registerMessageOutboxRepository(string $class, ?Application $application = null): void 290 | { 291 | $application = $application ?? app(); 292 | 293 | switch ($class) { 294 | case IlluminateOutboxRepository::class: 295 | $application->singleton(OutboxRepository::class, IlluminateOutboxRepository::class); 296 | $application->when(IlluminateOutboxRepository::class) 297 | ->needs('$tableName') 298 | ->giveConfig('eventsauce.message_outbox.table_name'); 299 | 300 | $application->singleton(IlluminateTransactionalMessageRepository::class); 301 | $application->when(IlluminateTransactionalMessageRepository::class) 302 | ->needs(MessageRepository::class) 303 | ->give(IlluminateUuidV4MessageRepository::class); 304 | 305 | break; 306 | 307 | default: 308 | throw new LogicException(sprintf('Unknown Message Outbox class "%s" specified', $class)); 309 | } 310 | } 311 | 312 | public static function registerTransaction(string $class, ?Application $application = null): void 313 | { 314 | $application = $application ?? app(); 315 | 316 | $application->singleton(Transaction::class, $class); 317 | } 318 | 319 | public static function registerMessageSerializer(string $class, ?Application $application = null): void 320 | { 321 | $application = $application ?? app(); 322 | 323 | $application->singleton(MessageSerializer::class, $class); 324 | } 325 | 326 | public static function registerClassNameInflector(?string $class = null, ?Application $application = null): void 327 | { 328 | if (!$class) { 329 | return; 330 | } 331 | 332 | $application = $application ?? app(); 333 | 334 | $application->singleton(ClassNameInflector::class, $class); 335 | } 336 | 337 | public static function registerPayloadSerializer(?string $class = null, ?Application $application = null): void 338 | { 339 | if (!$class) { 340 | return; 341 | } 342 | 343 | $application = $application ?? app(); 344 | 345 | $application->singleton(PayloadSerializer::class, $class); 346 | } 347 | 348 | public static function registerDefaultMessageDecorator(?Application $application = null): void 349 | { 350 | $application = $application ?? app(); 351 | 352 | $application->singleton(DefaultHeadersDecorator::class); 353 | $application->tag(DefaultHeadersDecorator::class, MessageDecorator::class); 354 | 355 | $application->singleton(MessageDecorator::class, function () use ($application) { 356 | /** @var MessageDecorator[] $messageDecorators */ 357 | $messageDecorators = $application->tagged(MessageDecorator::class); 358 | 359 | return new MessageDecoratorChain(...$messageDecorators); 360 | }); 361 | } 362 | 363 | public static function registerMessagePreparation(?string $class = null, ?Application $application = null): void 364 | { 365 | if (!$class) { 366 | return; 367 | } 368 | 369 | $application = $application ?? app(); 370 | 371 | $application->singleton(MessagePreparation::class, $class); 372 | } 373 | 374 | public static function eloquentRepositoryRegistration(): EloquentAggregateRootRepositoryRegistrationBuilder 375 | { 376 | return EloquentAggregateRootRepositoryRegistrationBuilder::new(); 377 | } 378 | 379 | public static function eventSourcedRepositoryRegistration(): EventSourcedAggregateRootRepositoryRegistrationBuilder 380 | { 381 | return EventSourcedAggregateRootRepositoryRegistrationBuilder::new(); 382 | } 383 | 384 | /** 385 | * @param class-string $aggregateRootClassName 386 | */ 387 | public static function eventSauceRepositoryFor(string $aggregateRootClassName, ?Application $application = null): AggregateRootRepository 388 | { 389 | $application = $application ?? app(); 390 | 391 | return $application->make(self::eventSauceRepositoryServiceName($aggregateRootClassName)); 392 | } 393 | } 394 | --------------------------------------------------------------------------------