Skip to content

Commit

Permalink
Merge pull request #213 from gzumba/custom-headers-configuration
Browse files Browse the repository at this point in the history
Custom headers configuration
  • Loading branch information
DavidBadura authored Jul 16, 2024
2 parents 8e88031 + 2591cb8 commit 53f5f9b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
24 changes: 23 additions & 1 deletion docs/pages/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ patchlevel_event_sourcing:
!!! tip

If you want to learn more about events, read the [library documentation](https://patchlevel.github.io/event-sourcing-docs/latest/events/).

## Custom Headers

If you want to implement custom headers for your application, you must specify the
paths to look for those headers.
If you want you can use glob patterns to specify multiple paths.

```yaml
patchlevel_event_sourcing:
headers: '%kernel.project_dir%/src/*/Domain/Header'
```
Or use an array to specify multiple paths.

```yaml
patchlevel_event_sourcing:
headers:
- '%kernel.project_dir%/src/Hotel/Domain/Header'
- '%kernel.project_dir%/src/Room/Domain/Header'
```
!!! tip

If you want to learn more about custom headers, read the [library documentation](https://event-sourcing.patchlevel.io/latest/message/#custom-headers).

## Connection

Expand Down Expand Up @@ -433,4 +455,4 @@ You can then specify this service here:
patchlevel_event_sourcing:
clock:
service: 'my_own_clock_service'
```
```
7 changes: 7 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* store: array{merge_orm_schema: bool, options: array<string, mixed>},
* aggregates: list<string>,
* events: list<string>,
* headers: list<string>,
* snapshot_stores: array<string, array{type: string, service: string}>,
* migration: array{path: string, namespace: string},
* cryptography: array{enabled: bool, algorithm: string},
Expand Down Expand Up @@ -90,6 +91,12 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarPrototype()->end()
->end()

->arrayNode('headers')
->beforeNormalization()->castToArray()->end()
->defaultValue([])
->scalarPrototype()->end()
->end()

->arrayNode('clock')
->addDefaultsIfNotSet()
->children()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private function configureSerializer(array $config, ContainerBuilder $container)

$container->register(MessageHeaderRegistry::class)
->setFactory([new Reference(MessageHeaderRegistryFactory::class), 'create'])
->setArguments([[]]);
->setArguments([$config['headers']]);

$container->register(DefaultHeadersSerializer::class)
->setArguments([
Expand Down
13 changes: 13 additions & 0 deletions tests/Fixtures/CustomHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Patchlevel\EventSourcingBundle\Tests\Fixtures;

use Patchlevel\EventSourcing\Attribute\Header;

#[Header('custom')]
class CustomHeader
{
public function __construct(
readonly string $value
) {}
}
30 changes: 28 additions & 2 deletions tests/Unit/PatchlevelEventSourcingBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Patchlevel\EventSourcing\EventBus\Psr14EventBus;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
use Patchlevel\EventSourcing\Metadata\Event\EventRegistry;
use Patchlevel\EventSourcing\Metadata\Message\MessageHeaderRegistry;
use Patchlevel\EventSourcing\Repository\DefaultRepository;
use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager;
use Patchlevel\EventSourcing\Repository\MessageDecorator\ChainMessageDecorator;
Expand All @@ -57,6 +58,7 @@
use Patchlevel\EventSourcingBundle\DependencyInjection\PatchlevelEventSourcingExtension;
use Patchlevel\EventSourcingBundle\EventBus\SymfonyEventBus;
use Patchlevel\EventSourcingBundle\PatchlevelEventSourcingBundle;
use Patchlevel\EventSourcingBundle\Tests\Fixtures\CustomHeader;
use Patchlevel\EventSourcingBundle\Tests\Fixtures\DummyArgumentResolver;
use Patchlevel\EventSourcingBundle\Tests\Fixtures\Listener1;
use Patchlevel\EventSourcingBundle\Tests\Fixtures\Listener2;
Expand Down Expand Up @@ -436,6 +438,29 @@ public function testAggregateRegistry(): void
self::assertTrue($aggregateRegistry->hasAggregateClass(Profile::class));
}

public function testMessageHeaderRegistry(): void
{
$container = new ContainerBuilder();

$this->compileContainer(
$container,
[
'patchlevel_event_sourcing' => [
'connection' => [
'service' => 'doctrine.dbal.eventstore_connection',
],
'headers' => [__DIR__ . '/../Fixtures'],
],
]
);

/** @var MessageHeaderRegistry $messageHeaderRegistry */
$messageHeaderRegistry = $container->get(MessageHeaderRegistry::class);

self::assertInstanceOf(MessageHeaderRegistry::class, $messageHeaderRegistry);
self::assertTrue($messageHeaderRegistry->hasHeaderClass(CustomHeader::class));
}

public function testRepositoryManager(): void
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -653,7 +678,6 @@ public function testCatchUpSubscriptionEngine(): void
$container->get(SubscriptionEngine::class));
}


public function testAutoconfigureSubscriber(): void
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -689,7 +713,7 @@ public function testAutoconfigureArgumentResolver(): void

$container->setDefinition(DummyArgumentResolver::class, new Definition(DummyArgumentResolver::class))
->setAutoconfigured(true)
;
;

$this->compileContainer(
$container,
Expand Down Expand Up @@ -836,6 +860,7 @@ public function testFullBuild(): void
self::assertInstanceOf(RepositoryManager::class, $container->get(RepositoryManager::class));
self::assertInstanceOf(EventRegistry::class, $container->get(EventRegistry::class));
}

public function testNamedRepository(): void
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -883,4 +908,5 @@ private function compileContainer(ContainerBuilder $container, array $config): v

$container->compile();
}

}

0 comments on commit 53f5f9b

Please sign in to comment.