diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index dd2ab725..3b81180c 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -21,8 +21,6 @@ jobs: - "lowest" - "highest" php-version: - - "8.2" - - "8.3" - "8.4" - "8.5" operating-system: diff --git a/composer.json b/composer.json index b1663b06..7cd1b135 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ } ], "require": { - "php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0", + "php": "~8.4.0 || ~8.5.0", "patchlevel/event-sourcing": "4.0.x-dev", "patchlevel/hydrator": "^2.0.3", "symfony/cache": "^7.3.0 || ^8.0.0", diff --git a/composer.lock b/composer.lock index 9fbafa0f..68e2b3d6 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "6a6604c75d21e691e0f8d598b6baef77", + "content-hash": "650fc67c6d696356c131beb6e5c4c6d2", "packages": [ { "name": "brick/math", @@ -420,12 +420,12 @@ "source": { "type": "git", "url": "https://github.com/patchlevel/event-sourcing.git", - "reference": "106c90ee5df01a3a06a8bef0823ac7fc16999297" + "reference": "88eaac6892931d167a27511d1c5cdb6fc4d1db25" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/patchlevel/event-sourcing/zipball/106c90ee5df01a3a06a8bef0823ac7fc16999297", - "reference": "106c90ee5df01a3a06a8bef0823ac7fc16999297", + "url": "https://api.github.com/repos/patchlevel/event-sourcing/zipball/88eaac6892931d167a27511d1c5cdb6fc4d1db25", + "reference": "88eaac6892931d167a27511d1c5cdb6fc4d1db25", "shasum": "" }, "require": { @@ -433,7 +433,7 @@ "doctrine/migrations": "^3.3.2", "patchlevel/hydrator": "^2.0.2", "patchlevel/worker": "^1.7.0", - "php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0", + "php": "~8.4.0 || ~8.5.0", "psr/cache": "^2.0.0 || ^3.0.0", "psr/clock": "^1.0", "psr/container": "^2.0", @@ -447,7 +447,7 @@ }, "require-dev": { "doctrine/orm": "^2.18.0 || ^3.0.0", - "ext-pdo_sqlite": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0", + "ext-pdo_sqlite": "~8.4.0 || ~8.5.0", "infection/infection": "^0.32.0", "league/commonmark": "^2.6.1", "patchlevel/coding-standard": "^1.3.0", @@ -507,7 +507,8 @@ "issues": "https://github.com/patchlevel/event-sourcing/issues", "source": "https://github.com/patchlevel/event-sourcing/tree/4.0.x" }, - "time": "2026-10-09T12:15:13+00:00" + "time": "2026-10-16T20:28:35+00:00", + "published-time": "2026-10-16T20:28:41+00:00" }, { "name": "patchlevel/hydrator", @@ -10156,10 +10157,10 @@ "prefer-stable": true, "prefer-lowest": false, "platform": { - "php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0" + "php": "~8.4.0 || ~8.5.0" }, "platform-dev": { "ext-pdo_sqlite": "*" }, - "plugin-api-version": "2.9.0" + "plugin-api-version": "2.11.0" } diff --git a/docs/configuration.md b/docs/configuration.md index 474efd1a..7c221b08 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -638,6 +638,7 @@ patchlevel_event_sourcing: default_max_retries: 3 default_exceptions: - Patchlevel\EventSourcing\Repository\AggregateOutdated + - Patchlevel\EventSourcing\Store\AppendConditionNotMet ``` :::note You can find out more about instant retry [here](/docs/event-sourcing/latest/command-bus/#instant-retry). @@ -914,7 +915,7 @@ patchlevel_event_sourcing: ## Dynamic Consistency Boundary You can enable the experimental dynamic consistency boundary (DCB). -This registers the `DecisionModelBuilder` and the `EventAppender` services. +This registers the `DecisionFactory` and the `EventAppender` services. ```yaml patchlevel_event_sourcing: @@ -930,4 +931,38 @@ DCB needs a store that supports appending, like `dbal_taggable` or `in_memory` f We recommend PostgreSQL for DCB, because only there the tag queries can use an index. ::: +Command handlers are messenger handlers, so they only get the command. +Inject the `DecisionFactory` and begin a new `Decision` for every command. + +```php +use Patchlevel\EventSourcing\DecisionModel\DecisionFactory; +use Symfony\Component\Messenger\Attribute\AsMessageHandler; + +#[AsMessageHandler(bus: 'command.bus')] +final class CreateHotelHandler +{ + public function __construct( + private readonly DecisionFactory $decisionFactory, + ) { + } + + public function __invoke(CreateHotel $command): void + { + $decision = $this->decisionFactory->begin(); + + if ($decision->read(new HotelExists($command->hotelId))) { + throw new RuntimeException('Hotel already exists'); + } + + $decision->append([ + new HotelCreated($command->hotelId, $command->hotelName), + ]); + } +} +``` +:::note +If something the decision has read changed in the meantime, an `AppendConditionNotMet` exception is thrown. +If you dispatch the command with the `CommandBus` service, its instant retry retries the command. +::: + If you want to learn more about DCB, read the [library documentation](/docs/event-sourcing/latest/dynamic-consistency-boundary). diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index c4c3e7d2..69c2fd8e 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -5,6 +5,7 @@ namespace Patchlevel\EventSourcingBundle\DependencyInjection; use Patchlevel\EventSourcing\Repository\AggregateOutdated; +use Patchlevel\EventSourcing\Store\AppendConditionNotMet; use Patchlevel\Hydrator\Extension\Cryptography\Cipher\OpensslCipherKeyFactory; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; @@ -285,7 +286,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->defaultValue(3) ->end() ->arrayNode('default_exceptions') - ->defaultValue([AggregateOutdated::class]) + ->defaultValue([AggregateOutdated::class, AppendConditionNotMet::class]) ->scalarPrototype()->end() ->end() ->end() diff --git a/src/DependencyInjection/PatchlevelEventSourcingExtension.php b/src/DependencyInjection/PatchlevelEventSourcingExtension.php index 08249d0b..c39cb2ac 100644 --- a/src/DependencyInjection/PatchlevelEventSourcingExtension.php +++ b/src/DependencyInjection/PatchlevelEventSourcingExtension.php @@ -46,9 +46,8 @@ use Patchlevel\EventSourcing\Console\Command\WatchCommand; use Patchlevel\EventSourcing\Console\DoctrineHelper; use Patchlevel\EventSourcing\Cryptography\DoctrineCipherKeyStore; -use Patchlevel\EventSourcing\DecisionModel\DecisionModelBuilder; +use Patchlevel\EventSourcing\DecisionModel\DecisionFactory; use Patchlevel\EventSourcing\DecisionModel\EventAppender; -use Patchlevel\EventSourcing\DecisionModel\StoreDecisionModelBuilder; use Patchlevel\EventSourcing\DecisionModel\StoreEventAppender; use Patchlevel\EventSourcing\EventBus\AttributeListenerProvider; use Patchlevel\EventSourcing\EventBus\Consumer; @@ -1337,16 +1336,18 @@ private function configureDCB(array $config, ContainerBuilder $container): void ); } - $container->register(StoreDecisionModelBuilder::class) - ->setArguments([new Reference(Store::class)]); - $container->setAlias(DecisionModelBuilder::class, StoreDecisionModelBuilder::class); - $container->register(StoreEventAppender::class) ->setArguments([ new Reference(Store::class), new Reference(EventTagExtractor::class), ]); $container->setAlias(EventAppender::class, StoreEventAppender::class); + + $container->register(DecisionFactory::class) + ->setArguments([ + new Reference(Store::class), + new Reference(EventAppender::class), + ]); } private function configureValueResolver(ContainerBuilder $container): void diff --git a/tests/Unit/PatchlevelEventSourcingBundleTest.php b/tests/Unit/PatchlevelEventSourcingBundleTest.php index 26922297..d68440f2 100644 --- a/tests/Unit/PatchlevelEventSourcingBundleTest.php +++ b/tests/Unit/PatchlevelEventSourcingBundleTest.php @@ -38,9 +38,8 @@ use Patchlevel\EventSourcing\Console\Command\SubscriptionStatusCommand; use Patchlevel\EventSourcing\Console\Command\SubscriptionTeardownCommand; use Patchlevel\EventSourcing\Console\Command\WatchCommand; -use Patchlevel\EventSourcing\DecisionModel\DecisionModelBuilder; +use Patchlevel\EventSourcing\DecisionModel\DecisionFactory; use Patchlevel\EventSourcing\DecisionModel\EventAppender; -use Patchlevel\EventSourcing\DecisionModel\StoreDecisionModelBuilder; use Patchlevel\EventSourcing\DecisionModel\StoreEventAppender; use Patchlevel\EventSourcing\EventBus\DefaultEventBus; use Patchlevel\EventSourcing\EventBus\EventBus; @@ -63,6 +62,7 @@ use Patchlevel\EventSourcing\Metadata\Subscriber\Psr6SubscriberMetadataFactory; use Patchlevel\EventSourcing\Metadata\Subscriber\SubscriberMetadataFactory; use Patchlevel\EventSourcing\QueryBus\QueryBus; +use Patchlevel\EventSourcing\Repository\AggregateOutdated; use Patchlevel\EventSourcing\Repository\DefaultRepository; use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager; use Patchlevel\EventSourcing\Repository\MessageDecorator\ChainMessageDecorator; @@ -79,6 +79,7 @@ use Patchlevel\EventSourcing\Snapshot\Adapter\Psr6SnapshotAdapter; use Patchlevel\EventSourcing\Snapshot\DefaultSnapshotStore; use Patchlevel\EventSourcing\Snapshot\SnapshotStore; +use Patchlevel\EventSourcing\Store\AppendConditionNotMet; use Patchlevel\EventSourcing\Store\ArchivedHeader; use Patchlevel\EventSourcing\Store\Dbal\PostgreSQLPlatform as EventSourcingPostgreSQLPlatform; use Patchlevel\EventSourcing\Store\Dbal\PostgreSQLPlatformMiddleware; @@ -592,6 +593,10 @@ public function testCommandBus(): void self::assertEquals(CreateProfile::class, $tag['handles']); self::assertEquals('command.bus', $tag['bus']); self::assertInstanceOf(InstantRetryCommandBus::class, $container->get(CommandBus::class)); + self::assertSame( + [AggregateOutdated::class, AppendConditionNotMet::class], + $container->getDefinition(InstantRetryCommandBus::class)->getArgument(2), + ); } public function testQueryBus(): void @@ -644,7 +649,7 @@ public function testDCB(): void ); self::assertInstanceOf(StoreEventAppender::class, $container->get(EventAppender::class)); - self::assertInstanceOf(StoreDecisionModelBuilder::class, $container->get(DecisionModelBuilder::class)); + self::assertInstanceOf(DecisionFactory::class, $container->get(DecisionFactory::class)); } public function testDCBWithInMemoryStore(): void @@ -663,7 +668,7 @@ public function testDCBWithInMemoryStore(): void ); self::assertInstanceOf(StoreEventAppender::class, $container->get(EventAppender::class)); - self::assertInstanceOf(StoreDecisionModelBuilder::class, $container->get(DecisionModelBuilder::class)); + self::assertInstanceOf(DecisionFactory::class, $container->get(DecisionFactory::class)); } public function testDCBWithStreamStore(): void