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 25, 2024
1 parent 7671350 commit 5bf6165
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 88 deletions.
3 changes: 3 additions & 0 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,11 @@ deptrac:
- Attribute
- Metadata
MetadataMessage:
- Aggregate
- Attribute
- Debug
- Metadata
- Store
MetadataSubscriber:
- Attribute
- Metadata
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
9 changes: 9 additions & 0 deletions src/Message/Serializer/DefaultHeadersSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}
}
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,
'trace' => TraceHeader::class,
'archived' => ArchivedHeader::class,
'newStreamStart' => NewStreamStartHeader::class,
];

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 @@ -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;
Expand All @@ -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(
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: 0 additions & 6 deletions tests/Benchmark/SimpleSetupBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down
6 changes: 0 additions & 6 deletions tests/Benchmark/SnapshotsBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 0 additions & 6 deletions tests/Benchmark/SplitStreamBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 0 additions & 8 deletions tests/Benchmark/SubscriptionEngineBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
6 changes: 0 additions & 6 deletions tests/Integration/BankAccountSplitStream/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
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: 0 additions & 9 deletions tests/Integration/Pipeline/PipelineChangeStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -66,8 +59,6 @@ public function testSuccessful(): void
$newStore = new DoctrineDbalStore(
$this->connectionNew,
$eventSerializer,
$headersSerializer,
'eventstore',
);

$newSchemaDirector = new DoctrineSchemaDirector(
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 @@ -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;
Expand Down Expand Up @@ -63,10 +62,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 @@ -183,10 +179,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 @@ -315,10 +308,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 @@ -430,10 +420,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 @@ -591,10 +578,7 @@ public function testBlueGreenDeploymentRollback(): void
$store = new DoctrineDbalStore(
$this->connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']),
DefaultHeadersSerializer::createFromPaths([
__DIR__ . '/../../../src',
__DIR__,
]),
null,
'eventstore',
);

Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/Message/Serializer/HeadersSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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(),
Expand Down

0 comments on commit 5bf6165

Please sign in to comment.