diff --git a/deptrac.yaml b/deptrac.yaml index 5798c3dc6..e76d22ee2 100644 --- a/deptrac.yaml +++ b/deptrac.yaml @@ -128,8 +128,11 @@ deptrac: - Attribute - Metadata MetadataMessage: + - Aggregate - Attribute + - Debug - Metadata + - Store MetadataSubscriber: - Attribute - Metadata diff --git a/src/Aggregate/AggregateHeader.php b/src/Aggregate/AggregateHeader.php index 506a25ffe..d4e9b515d 100644 --- a/src/Aggregate/AggregateHeader.php +++ b/src/Aggregate/AggregateHeader.php @@ -5,11 +5,9 @@ namespace Patchlevel\EventSourcing\Aggregate; use DateTimeImmutable; -use Patchlevel\EventSourcing\Attribute\Header; use Patchlevel\Hydrator\Normalizer\DateTimeImmutableNormalizer; /** @psalm-immutable */ -#[Header('aggregate')] final class AggregateHeader { /** @param positive-int $playhead */ diff --git a/src/Debug/Trace/TraceHeader.php b/src/Debug/Trace/TraceHeader.php index 2151b420a..c0406f1de 100644 --- a/src/Debug/Trace/TraceHeader.php +++ b/src/Debug/Trace/TraceHeader.php @@ -4,10 +4,7 @@ namespace Patchlevel\EventSourcing\Debug\Trace; -use Patchlevel\EventSourcing\Attribute\Header; - /** @experimental */ -#[Header('trace')] final class TraceHeader { /** @param list $traces */ diff --git a/src/Message/Serializer/DefaultHeadersSerializer.php b/src/Message/Serializer/DefaultHeadersSerializer.php index a6331ac9e..cc96f52d6 100644 --- a/src/Message/Serializer/DefaultHeadersSerializer.php +++ b/src/Message/Serializer/DefaultHeadersSerializer.php @@ -60,4 +60,13 @@ public static function createFromPaths(array $paths): static new JsonEncoder(), ); } + + public static function createDefault(): static + { + return new self( + MessageHeaderRegistry::createWithInternalHeaders(), + new MetadataHydrator(), + new JsonEncoder(), + ); + } } diff --git a/src/Metadata/Message/AttributeMessageHeaderRegistryFactory.php b/src/Metadata/Message/AttributeMessageHeaderRegistryFactory.php index 819b0cf34..103e5d432 100644 --- a/src/Metadata/Message/AttributeMessageHeaderRegistryFactory.php +++ b/src/Metadata/Message/AttributeMessageHeaderRegistryFactory.php @@ -31,6 +31,6 @@ public function create(array $paths): MessageHeaderRegistry $result[$aggregateName] = $class; } - return new MessageHeaderRegistry($result); + return MessageHeaderRegistry::createWithInternalHeaders($result); } } diff --git a/src/Metadata/Message/MessageHeaderRegistry.php b/src/Metadata/Message/MessageHeaderRegistry.php index e6821bc11..d4bdb56d3 100644 --- a/src/Metadata/Message/MessageHeaderRegistry.php +++ b/src/Metadata/Message/MessageHeaderRegistry.php @@ -4,6 +4,11 @@ namespace Patchlevel\EventSourcing\Metadata\Message; +use Patchlevel\EventSourcing\Aggregate\AggregateHeader; +use Patchlevel\EventSourcing\Debug\Trace\TraceHeader; +use Patchlevel\EventSourcing\Store\ArchivedHeader; +use Patchlevel\EventSourcing\Store\NewStreamStartHeader; + use function array_flip; use function array_key_exists; @@ -63,4 +68,17 @@ public function headerNames(): array { return $this->classToNameMap; } + + /** @param array $headerNameToClassMap */ + public static function createWithInternalHeaders(array $headerNameToClassMap = []): self + { + $internalHeaders = [ + 'aggregate' => AggregateHeader::class, + 'trace' => TraceHeader::class, + 'archived' => ArchivedHeader::class, + 'newStreamStart' => NewStreamStartHeader::class, + ]; + + return new self($headerNameToClassMap + $internalHeaders); + } } diff --git a/src/Store/ArchivedHeader.php b/src/Store/ArchivedHeader.php index 941de7178..d65906031 100644 --- a/src/Store/ArchivedHeader.php +++ b/src/Store/ArchivedHeader.php @@ -4,10 +4,7 @@ namespace Patchlevel\EventSourcing\Store; -use Patchlevel\EventSourcing\Attribute\Header; - /** @psalm-immutable */ -#[Header('archived')] final class ArchivedHeader { public function __construct( diff --git a/src/Store/DoctrineDbalStore.php b/src/Store/DoctrineDbalStore.php index edb70f64b..cf4b818a2 100644 --- a/src/Store/DoctrineDbalStore.php +++ b/src/Store/DoctrineDbalStore.php @@ -15,6 +15,7 @@ use Patchlevel\EventSourcing\Aggregate\AggregateHeader; use Patchlevel\EventSourcing\Message\HeaderNotFound; use Patchlevel\EventSourcing\Message\Message; +use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer; use Patchlevel\EventSourcing\Message\Serializer\HeadersSerializer; use Patchlevel\EventSourcing\Schema\DoctrineSchemaConfigurator; use Patchlevel\EventSourcing\Serializer\EventSerializer; @@ -40,12 +41,15 @@ final class DoctrineDbalStore implements Store, ArchivableStore, SubscriptionSto */ private const MAX_UNSIGNED_SMALL_INT = 65_535; + private readonly HeadersSerializer $headersSerializer; + public function __construct( private readonly Connection $connection, private readonly EventSerializer $eventSerializer, - private readonly HeadersSerializer $headersSerializer, + HeadersSerializer|null $headersSerializer = null, private readonly string $storeTableName = 'eventstore', ) { + $this->headersSerializer = $headersSerializer ?? DefaultHeadersSerializer::createDefault(); } public function load( diff --git a/src/Store/NewStreamStartHeader.php b/src/Store/NewStreamStartHeader.php index c7b8da202..0e84f7226 100644 --- a/src/Store/NewStreamStartHeader.php +++ b/src/Store/NewStreamStartHeader.php @@ -4,10 +4,7 @@ namespace Patchlevel\EventSourcing\Store; -use Patchlevel\EventSourcing\Attribute\Header; - /** @psalm-immutable */ -#[Header('newStreamStart')] final class NewStreamStartHeader { public function __construct( diff --git a/tests/Benchmark/SimpleSetupBench.php b/tests/Benchmark/SimpleSetupBench.php index 52a4f20a5..1b8931f34 100644 --- a/tests/Benchmark/SimpleSetupBench.php +++ b/tests/Benchmark/SimpleSetupBench.php @@ -5,7 +5,6 @@ namespace Patchlevel\EventSourcing\Tests\Benchmark; use Patchlevel\EventSourcing\Aggregate\AggregateRootId; -use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer; use Patchlevel\EventSourcing\Repository\DefaultRepository; use Patchlevel\EventSourcing\Repository\Repository; use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector; @@ -32,11 +31,6 @@ public function setUp(): void $this->store = new DoctrineDbalStore( $connection, DefaultEventSerializer::createFromPaths([__DIR__ . '/BasicImplementation/Events']), - DefaultHeadersSerializer::createFromPaths([ - __DIR__ . '/../../src', - __DIR__ . '/BasicImplementation/Events', - ]), - 'eventstore', ); $this->repository = new DefaultRepository($this->store, Profile::metadata()); diff --git a/tests/Benchmark/SnapshotsBench.php b/tests/Benchmark/SnapshotsBench.php index 755876037..f11d22261 100644 --- a/tests/Benchmark/SnapshotsBench.php +++ b/tests/Benchmark/SnapshotsBench.php @@ -5,7 +5,6 @@ namespace Patchlevel\EventSourcing\Tests\Benchmark; use Patchlevel\EventSourcing\Aggregate\AggregateRootId; -use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer; use Patchlevel\EventSourcing\Repository\DefaultRepository; use Patchlevel\EventSourcing\Repository\Repository; use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector; @@ -38,11 +37,6 @@ public function setUp(): void $this->store = new DoctrineDbalStore( $connection, DefaultEventSerializer::createFromPaths([__DIR__ . '/BasicImplementation/Events']), - DefaultHeadersSerializer::createFromPaths([ - __DIR__ . '/../../src', - __DIR__ . '/BasicImplementation/Events', - ]), - 'eventstore', ); $this->adapter = new InMemorySnapshotAdapter(); diff --git a/tests/Benchmark/SplitStreamBench.php b/tests/Benchmark/SplitStreamBench.php index 7bb3737b5..659c6db78 100644 --- a/tests/Benchmark/SplitStreamBench.php +++ b/tests/Benchmark/SplitStreamBench.php @@ -5,7 +5,6 @@ namespace Patchlevel\EventSourcing\Tests\Benchmark; use Patchlevel\EventSourcing\Aggregate\AggregateRootId; -use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer; use Patchlevel\EventSourcing\Metadata\Event\AttributeEventMetadataFactory; use Patchlevel\EventSourcing\Repository\DefaultRepository; use Patchlevel\EventSourcing\Repository\MessageDecorator\SplitStreamDecorator; @@ -36,11 +35,6 @@ public function setUp(): void $this->store = new DoctrineDbalStore( $connection, DefaultEventSerializer::createFromPaths([__DIR__ . '/BasicImplementation/Events']), - DefaultHeadersSerializer::createFromPaths([ - __DIR__ . '/../../src', - __DIR__ . '/BasicImplementation/Events', - ]), - 'eventstore', ); $this->repository = new DefaultRepository( diff --git a/tests/Benchmark/SubscriptionEngineBench.php b/tests/Benchmark/SubscriptionEngineBench.php index c915b2581..54bbe1047 100644 --- a/tests/Benchmark/SubscriptionEngineBench.php +++ b/tests/Benchmark/SubscriptionEngineBench.php @@ -5,7 +5,6 @@ namespace Patchlevel\EventSourcing\Tests\Benchmark; use Patchlevel\EventSourcing\Aggregate\AggregateRootId; -use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer; use Patchlevel\EventSourcing\Repository\DefaultRepository; use Patchlevel\EventSourcing\Repository\Repository; use Patchlevel\EventSourcing\Schema\ChainDoctrineSchemaConfigurator; @@ -41,13 +40,6 @@ public function setUp(): void $this->store = new DoctrineDbalStore( $connection, DefaultEventSerializer::createFromPaths([__DIR__ . '/BasicImplementation/Events']), - DefaultHeadersSerializer::createFromPaths( - [ - __DIR__ . '/BasicImplementation/Events', - __DIR__ . '/../../src', - ], - ), - 'eventstore', ); $this->repository = new DefaultRepository($this->store, Profile::metadata()); diff --git a/tests/Integration/BankAccountSplitStream/IntegrationTest.php b/tests/Integration/BankAccountSplitStream/IntegrationTest.php index f4194b908..9540725a6 100644 --- a/tests/Integration/BankAccountSplitStream/IntegrationTest.php +++ b/tests/Integration/BankAccountSplitStream/IntegrationTest.php @@ -5,7 +5,6 @@ namespace Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream; use Doctrine\DBAL\Connection; -use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer; use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry; use Patchlevel\EventSourcing\Metadata\Event\AttributeEventMetadataFactory; use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager; @@ -47,11 +46,6 @@ public function testSuccessful(): void $store = new DoctrineDbalStore( $this->connection, DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), - DefaultHeadersSerializer::createFromPaths([ - __DIR__ . '/../../../src', - __DIR__, - ]), - 'eventstore', ); $bankAccountProjector = new BankAccountProjector($this->connection); diff --git a/tests/Integration/BasicImplementation/BasicIntegrationTest.php b/tests/Integration/BasicImplementation/BasicIntegrationTest.php index 6d2bce8f2..d782339e8 100644 --- a/tests/Integration/BasicImplementation/BasicIntegrationTest.php +++ b/tests/Integration/BasicImplementation/BasicIntegrationTest.php @@ -46,10 +46,8 @@ public function testSuccessful(): void $this->connection, DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), DefaultHeadersSerializer::createFromPaths([ - __DIR__ . '/../../../src', - __DIR__, + __DIR__ . '/Header', ]), - 'eventstore', ); $profileProjector = new ProfileProjector($this->connection); @@ -115,10 +113,8 @@ public function testSnapshot(): void $this->connection, DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), DefaultHeadersSerializer::createFromPaths([ - __DIR__ . '/../../../src', - __DIR__, + __DIR__ . '/Header', ]), - 'eventstore', ); $profileProjection = new ProfileProjector($this->connection); diff --git a/tests/Integration/Pipeline/PipelineChangeStoreTest.php b/tests/Integration/Pipeline/PipelineChangeStoreTest.php index cb9ceb23d..203f59d41 100644 --- a/tests/Integration/Pipeline/PipelineChangeStoreTest.php +++ b/tests/Integration/Pipeline/PipelineChangeStoreTest.php @@ -5,7 +5,6 @@ namespace Patchlevel\EventSourcing\Tests\Integration\Pipeline; use Doctrine\DBAL\Connection; -use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer; use Patchlevel\EventSourcing\Pipeline\Middleware\ExcludeEventMiddleware; use Patchlevel\EventSourcing\Pipeline\Middleware\RecalculatePlayheadMiddleware; use Patchlevel\EventSourcing\Pipeline\Middleware\ReplaceEventMiddleware; @@ -44,16 +43,10 @@ public function tearDown(): void public function testSuccessful(): void { $eventSerializer = DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']); - $headersSerializer = DefaultHeadersSerializer::createFromPaths([ - __DIR__ . '/../../../src', - __DIR__, - ]); $oldStore = new DoctrineDbalStore( $this->connectionOld, $eventSerializer, - $headersSerializer, - 'eventstore', ); $oldSchemaDirector = new DoctrineSchemaDirector( @@ -66,8 +59,6 @@ public function testSuccessful(): void $newStore = new DoctrineDbalStore( $this->connectionNew, $eventSerializer, - $headersSerializer, - 'eventstore', ); $newSchemaDirector = new DoctrineSchemaDirector( diff --git a/tests/Integration/Store/StoreTest.php b/tests/Integration/Store/StoreTest.php index 5ef674048..f6d6056fe 100644 --- a/tests/Integration/Store/StoreTest.php +++ b/tests/Integration/Store/StoreTest.php @@ -8,7 +8,6 @@ use Doctrine\DBAL\Connection; use Patchlevel\EventSourcing\Aggregate\AggregateHeader; use Patchlevel\EventSourcing\Message\Message; -use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer; use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector; use Patchlevel\EventSourcing\Serializer\DefaultEventSerializer; use Patchlevel\EventSourcing\Store\DoctrineDbalStore; @@ -32,11 +31,6 @@ public function setUp(): void $this->store = new DoctrineDbalStore( $this->connection, DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), - DefaultHeadersSerializer::createFromPaths([ - __DIR__ . '/../../../src', - __DIR__, - ]), - 'eventstore', ); $schemaDirector = new DoctrineSchemaDirector( diff --git a/tests/Integration/Subscription/SubscriptionTest.php b/tests/Integration/Subscription/SubscriptionTest.php index 4023f9a82..603dd9bce 100644 --- a/tests/Integration/Subscription/SubscriptionTest.php +++ b/tests/Integration/Subscription/SubscriptionTest.php @@ -12,7 +12,6 @@ use Patchlevel\EventSourcing\Debug\Trace\TraceHeader; use Patchlevel\EventSourcing\Debug\Trace\TraceStack; use Patchlevel\EventSourcing\Message\Message; -use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer; use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry; use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager; use Patchlevel\EventSourcing\Schema\ChainDoctrineSchemaConfigurator; @@ -63,11 +62,6 @@ public function testHappyPath(): void $store = new DoctrineDbalStore( $this->connection, DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), - DefaultHeadersSerializer::createFromPaths([ - __DIR__ . '/../../../src', - __DIR__, - ]), - 'eventstore', ); $clock = new FrozenClock(new DateTimeImmutable('2021-01-01T00:00:00')); @@ -183,11 +177,6 @@ public function testErrorHandling(): void $store = new DoctrineDbalStore( $this->connection, DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), - DefaultHeadersSerializer::createFromPaths([ - __DIR__ . '/../../../src', - __DIR__, - ]), - 'eventstore', ); $subscriptionStore = new DoctrineSubscriptionStore( @@ -315,11 +304,6 @@ public function testProcessor(): void $store = new DoctrineDbalStore( $this->connection, DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), - DefaultHeadersSerializer::createFromPaths([ - __DIR__ . '/../../../src', - __DIR__, - ]), - 'eventstore', ); $clock = new FrozenClock(new DateTimeImmutable('2021-01-01T00:00:00')); @@ -430,11 +414,6 @@ public function testBlueGreenDeployment(): void $store = new DoctrineDbalStore( $this->connection, DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), - DefaultHeadersSerializer::createFromPaths([ - __DIR__ . '/../../../src', - __DIR__, - ]), - 'eventstore', ); $clock = new FrozenClock(new DateTimeImmutable('2021-01-01T00:00:00')); @@ -591,11 +570,6 @@ public function testBlueGreenDeploymentRollback(): void $store = new DoctrineDbalStore( $this->connection, DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), - DefaultHeadersSerializer::createFromPaths([ - __DIR__ . '/../../../src', - __DIR__, - ]), - 'eventstore', ); $clock = new FrozenClock(new DateTimeImmutable('2021-01-01T00:00:00')); diff --git a/tests/Unit/Message/Serializer/HeadersSerializerTest.php b/tests/Unit/Message/Serializer/HeadersSerializerTest.php index a1d8b587f..11951382e 100644 --- a/tests/Unit/Message/Serializer/HeadersSerializerTest.php +++ b/tests/Unit/Message/Serializer/HeadersSerializerTest.php @@ -22,8 +22,7 @@ final class HeadersSerializerTest extends TestCase public function testSerialize(): void { $serializer = DefaultHeadersSerializer::createFromPaths([ - __DIR__ . '/../../../../src', // add our headers - __DIR__ . '/../../Fixture', // add user headers + __DIR__ . '/../../Fixture', ]); $content = $serializer->serialize([ @@ -44,8 +43,7 @@ public function testDeserialize(): void { $serializer = new DefaultHeadersSerializer( (new AttributeMessageHeaderRegistryFactory())->create([ - __DIR__ . '/../../../../src', // add our headers - __DIR__ . '/../../Fixture', // add user headers + __DIR__ . '/../../Fixture', ]), new MetadataHydrator(), new JsonEncoder(),