Skip to content

Commit

Permalink
load the internal header automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Mar 14, 2024
1 parent 05c96b0 commit 8b7d8ee
Show file tree
Hide file tree
Showing 19 changed files with 51 additions and 84 deletions.
2 changes: 0 additions & 2 deletions docs/pages/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,10 @@ $projectionConnection = DriverManager::getConnection([
$mailer = /* your own mailer */;

$serializer = DefaultEventSerializer::createFromPaths(['src/Domain/Hotel/Event']);
$aggregateRegistry = (new AttributeAggregateRootRegistryFactory)->create(['src/Domain/Hotel']);

$eventStore = new DoctrineDbalStore(
$connection,
$serializer,
$aggregateRegistry,
);

$hotelProjector = new HotelProjector($projectionConnection);
Expand Down
2 changes: 0 additions & 2 deletions src/Aggregate/AggregateHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 0 additions & 3 deletions src/Debug/Trace/TraceHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace Patchlevel\EventSourcing\Debug\Trace;

use Patchlevel\EventSourcing\Attribute\Header;

/** @experimental */
#[Header('trace')]
final class TraceHeader
{
/** @param list<array{name: string, category: string}> $traces */
Expand Down
11 changes: 10 additions & 1 deletion src/Message/Serializer/DefaultHeadersSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,21 @@ public function deserialize(array $serializedHeaders): array
}

/** @param list<string> $paths */
public static function createFromPaths(array $paths): static
public static function createFromPaths(array $paths = []): static
{
return new self(
(new AttributeMessageHeaderRegistryFactory())->create($paths),
new MetadataHydrator(),
new JsonEncoder(),
);
}

public static function createDefault(): static
{
return new self(
MessageHeaderRegistry::createWithInternalHeaders(),
new MetadataHydrator(),
new JsonEncoder(),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public function create(array $paths): MessageHeaderRegistry
$result[$aggregateName] = $class;
}

return new MessageHeaderRegistry($result);
return MessageHeaderRegistry::createWithInternalHeaders($result);
}
}
18 changes: 18 additions & 0 deletions src/Metadata/Message/MessageHeaderRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -63,4 +68,17 @@ public function headerNames(): array
{
return $this->classToNameMap;
}

/** @param array<string, class-string> $headerNameToClassMap */
public static function createWithInternalHeaders(array $headerNameToClassMap = []): self
{
$internalHeaders = [
'aggregate' => AggregateHeader::class,

Check failure on line 76 in src/Metadata/Message/MessageHeaderRegistry.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Deptrac (locked, 8.3, ubuntu-latest)

Patchlevel\EventSourcing\Metadata\Message\MessageHeaderRegistry must not depend on Patchlevel\EventSourcing\Aggregate\AggregateHeader (MetadataMessage on Aggregate)
'trace' => TraceHeader::class,

Check failure on line 77 in src/Metadata/Message/MessageHeaderRegistry.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Deptrac (locked, 8.3, ubuntu-latest)

Patchlevel\EventSourcing\Metadata\Message\MessageHeaderRegistry must not depend on Patchlevel\EventSourcing\Debug\Trace\TraceHeader (MetadataMessage on Debug)
'archived' => ArchivedHeader::class,

Check failure on line 78 in src/Metadata/Message/MessageHeaderRegistry.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Deptrac (locked, 8.3, ubuntu-latest)

Patchlevel\EventSourcing\Metadata\Message\MessageHeaderRegistry must not depend on Patchlevel\EventSourcing\Store\ArchivedHeader (MetadataMessage on Store)
'newStreamStart' => NewStreamStartHeader::class,

Check failure on line 79 in src/Metadata/Message/MessageHeaderRegistry.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Deptrac (locked, 8.3, ubuntu-latest)

Patchlevel\EventSourcing\Metadata\Message\MessageHeaderRegistry must not depend on Patchlevel\EventSourcing\Store\NewStreamStartHeader (MetadataMessage on Store)
];

return new self($headerNameToClassMap + $internalHeaders);
}
}
3 changes: 0 additions & 3 deletions src/Store/ArchivedHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace Patchlevel\EventSourcing\Store;

use Patchlevel\EventSourcing\Attribute\Header;

/** @psalm-immutable */
#[Header('archived')]
final class ArchivedHeader
{
public function __construct(
Expand Down
6 changes: 5 additions & 1 deletion src/Store/DoctrineDbalStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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;
Expand All @@ -36,12 +37,15 @@ final class DoctrineDbalStore implements Store, ArchivableStore, DoctrineSchemaC
*/
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(
Expand Down
3 changes: 0 additions & 3 deletions src/Store/NewStreamStartHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace Patchlevel\EventSourcing\Store;

use Patchlevel\EventSourcing\Attribute\Header;

/** @psalm-immutable */
#[Header('newStreamStart')]
final class NewStreamStartHeader
{
public function __construct(
Expand Down
6 changes: 1 addition & 5 deletions tests/Benchmark/SimpleSetupBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Patchlevel\EventSourcing\Aggregate\AggregateRootId;
use Patchlevel\EventSourcing\EventBus\DefaultEventBus;
use Patchlevel\EventSourcing\EventBus\EventBus;
use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer;
use Patchlevel\EventSourcing\Repository\DefaultRepository;
use Patchlevel\EventSourcing\Repository\Repository;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector;
Expand Down Expand Up @@ -37,10 +36,7 @@ public function setUp(): void
$this->store = new DoctrineDbalStore(
$connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/BasicImplementation/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../src',
__DIR__ . '/BasicImplementation/Events',
]),
null,
'eventstore',
);

Expand Down
6 changes: 1 addition & 5 deletions tests/Benchmark/SnapshotsBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Patchlevel\EventSourcing\Aggregate\AggregateRootId;
use Patchlevel\EventSourcing\EventBus\DefaultEventBus;
use Patchlevel\EventSourcing\EventBus\EventBus;
use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer;
use Patchlevel\EventSourcing\Repository\DefaultRepository;
use Patchlevel\EventSourcing\Repository\Repository;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector;
Expand Down Expand Up @@ -43,10 +42,7 @@ public function setUp(): void
$this->store = new DoctrineDbalStore(
$connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/BasicImplementation/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../src',
__DIR__ . '/BasicImplementation/Events',
]),
null,
'eventstore',
);

Expand Down
6 changes: 1 addition & 5 deletions tests/Benchmark/SplitStreamBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Patchlevel\EventSourcing\Aggregate\AggregateRootId;
use Patchlevel\EventSourcing\EventBus\DefaultEventBus;
use Patchlevel\EventSourcing\EventBus\EventBus;
use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer;
use Patchlevel\EventSourcing\Metadata\Event\AttributeEventMetadataFactory;
use Patchlevel\EventSourcing\Repository\DefaultRepository;
use Patchlevel\EventSourcing\Repository\MessageDecorator\SplitStreamDecorator;
Expand Down Expand Up @@ -41,10 +40,7 @@ public function setUp(): void
$this->store = new DoctrineDbalStore(
$connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/BasicImplementation/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../src',
__DIR__ . '/BasicImplementation/Events',
]),
null,
'eventstore',
);

Expand Down
8 changes: 1 addition & 7 deletions tests/Benchmark/SubscriptionEngineBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Patchlevel\EventSourcing\Aggregate\AggregateRootId;
use Patchlevel\EventSourcing\EventBus\DefaultEventBus;
use Patchlevel\EventSourcing\EventBus\EventBus;
use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer;
use Patchlevel\EventSourcing\Repository\DefaultRepository;
use Patchlevel\EventSourcing\Repository\Repository;
use Patchlevel\EventSourcing\Schema\ChainDoctrineSchemaConfigurator;
Expand Down Expand Up @@ -46,12 +45,7 @@ public function setUp(): void
$this->store = new DoctrineDbalStore(
$connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/BasicImplementation/Events']),
DefaultHeadersSerializer::createFromPaths(
[
__DIR__ . '/BasicImplementation/Events',
__DIR__ . '/../../src',
],
),
null,
'eventstore',
);

Expand Down
6 changes: 1 addition & 5 deletions tests/Integration/BankAccountSplitStream/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Doctrine\DBAL\Connection;
use Patchlevel\EventSourcing\EventBus\DefaultEventBus;
use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
use Patchlevel\EventSourcing\Metadata\Event\AttributeEventMetadataFactory;
use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager;
Expand Down Expand Up @@ -48,10 +47,7 @@ public function testSuccessful(): void
$store = new DoctrineDbalStore(
$this->connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__,
]),
null,
'eventstore',
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public function testSuccessful(): void
$this->connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__,
__DIR__ . '/Header',
]),
'eventstore',
);
Expand Down Expand Up @@ -115,8 +114,7 @@ public function testSnapshot(): void
$this->connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__,
__DIR__ . '/Header',
]),
'eventstore',
);
Expand Down
9 changes: 2 additions & 7 deletions tests/Integration/Pipeline/PipelineChangeStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Doctrine\DBAL\Connection;
use Patchlevel\EventSourcing\EventBus\DefaultEventBus;
use Patchlevel\EventSourcing\Message\Serializer\DefaultHeadersSerializer;
use Patchlevel\EventSourcing\Pipeline\Middleware\ExcludeEventMiddleware;
use Patchlevel\EventSourcing\Pipeline\Middleware\RecalculatePlayheadMiddleware;
use Patchlevel\EventSourcing\Pipeline\Middleware\ReplaceEventMiddleware;
Expand Down Expand Up @@ -45,15 +44,11 @@ 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,
null,
'eventstore',
);

Expand All @@ -67,7 +62,7 @@ public function testSuccessful(): void
$newStore = new DoctrineDbalStore(
$this->connectionNew,
$eventSerializer,
$headersSerializer,
null,
'eventstore',
);

Expand Down
6 changes: 1 addition & 5 deletions tests/Integration/Store/StoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,10 +31,7 @@ public function setUp(): void
$this->store = new DoctrineDbalStore(
$this->connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__,
]),
null,
'eventstore',
);

Expand Down
26 changes: 5 additions & 21 deletions tests/Integration/Subscription/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Patchlevel\EventSourcing\Debug\Trace\TraceStack;
use Patchlevel\EventSourcing\EventBus\DefaultEventBus;
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;
Expand Down Expand Up @@ -64,10 +63,7 @@ public function testHappyPath(): void
$store = new DoctrineDbalStore(
$this->connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__,
]),
null,
'eventstore',
);

Expand Down Expand Up @@ -185,10 +181,7 @@ public function testErrorHandling(): void
$store = new DoctrineDbalStore(
$this->connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__,
]),
null,
'eventstore',
);

Expand Down Expand Up @@ -318,10 +311,7 @@ public function testProcessor(): void
$store = new DoctrineDbalStore(
$this->connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__,
]),
null,
'eventstore',
);

Expand Down Expand Up @@ -433,10 +423,7 @@ public function testBlueGreenDeployment(): void
$store = new DoctrineDbalStore(
$this->connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__,
]),
null,
'eventstore',
);

Expand Down Expand Up @@ -595,10 +582,7 @@ public function testBlueGreenDeploymentRollback(): void
$store = new DoctrineDbalStore(
$this->connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__,
]),
null,
'eventstore',
);

Expand Down
Loading

0 comments on commit 8b7d8ee

Please sign in to comment.