Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,31 @@ patchlevel_event_sourcing:
class: App\Domain\Hotel\Hotel
```

or by adding the corresponding aggregate attribute.
The snapshotStore is optional and `null` by default.

```php
namespace App\Domain\Hotel;

use Patchlevel\EventSourcing\Aggregate\AggregateChanged;
use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use Patchlevel\EventSourcingBundle\Attribute\Aggregate;

#[Aggregate(name: 'hotel', snapshotStore: 'default')]
final class Hotel extends AggregateRoot
{
protected function apply(AggregateChanged $event): void
{

}

public function aggregateRootId(): string
{
return '1';
}
}
```

### Define projections

So that we can see all the hotels on our website
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
"require": {
"php": "~8.0.0|~8.1.0",
"patchlevel/event-sourcing": "^1.1.0",
"symfony/dependency-injection": "^4.4.34|^5.4.0|^6.0.0",
"symfony/http-kernel": "^4.4.34|^5.4.0|^6.0.0",
"symfony/config": "^4.4.34|^5.4.0|^6.0.0",
"symfony/console": "^4.4.34|^5.4.0|^6.0.0"
"symfony/console": "^4.4.34|^5.4.0|^6.0.0",
"symfony/dependency-injection": "^4.4.34|^5.4.0|^6.0.0",
"symfony/finder": "^4.4.34|^5.4.0|^6.0.0",
"symfony/http-kernel": "^4.4.34|^5.4.0|^6.0.0"
},
"require-dev": {
"ext-pdo_sqlite": "~8.0.0|~8.1.0",
Expand Down
136 changes: 68 additions & 68 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions src/Attribute/Aggregate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcingBundle\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class Aggregate
{
private string $name;
private ?string $snapshotStore;

public function __construct(string $name, ?string $snapshotStore = null)
{
$this->name = $name;
$this->snapshotStore = $snapshotStore;
}

public function getName(): string
{
return $this->name;
}

public function getSnapshotStore(): ?string
{
return $this->snapshotStore;
}
}
6 changes: 6 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()

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

->arrayNode('aggregates')
->useAttributeAsKey('name')
->arrayPrototype()
Expand Down
33 changes: 28 additions & 5 deletions src/DependencyInjection/PatchlevelEventSourcingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\Migrations\Tools\Console\Command\ExecuteCommand;
use Doctrine\Migrations\Tools\Console\Command\MigrateCommand;
use Doctrine\Migrations\Tools\Console\Command\StatusCommand;
use InvalidArgumentException;
use Patchlevel\EventSourcing\Console\Command\DatabaseCreateCommand;
use Patchlevel\EventSourcing\Console\Command\DatabaseDropCommand;
use Patchlevel\EventSourcing\Console\Command\ProjectionCreateCommand;
Expand Down Expand Up @@ -52,12 +53,19 @@
use Patchlevel\EventSourcing\WatchServer\WatchServerClient;
use Patchlevel\EventSourcingBundle\DataCollector\EventCollector;
use Patchlevel\EventSourcingBundle\DataCollector\EventListener;
use Patchlevel\EventSourcingBundle\Loader\AggregateAttributesLoader;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

use function array_intersect_key;
use function array_keys;
use function array_merge;
use function class_exists;
use function count;
use function implode;
use function is_string;
use function sprintf;

final class PatchlevelEventSourcingExtension extends Extension
Expand All @@ -70,7 +78,7 @@ public function load(array $configs, ContainerBuilder $container): void
$configuration = new Configuration();

/**
* @var array{event_bus: ?array{type: string, service: string}, watch_server: array{enabled: bool, host: string}, connection: ?array{service: ?string, url: ?string}, store: array{schema_manager: string, type: string, options: array<string, mixed>}, aggregates: array<string, array{class: string, snapshot_store: ?string}>, snapshot_stores: array<string, array{type: string, service: string}>, migration: array{path: string, namespace: string}} $config
* @var array{event_bus: ?array{type: string, service: string}, watch_server: array{enabled: bool, host: string}, connection: ?array{service: ?string, url: ?string}, store: array{schema_manager: string, type: string, options: array<string, mixed>}, aggregates_paths: list<string>, aggregates: array<string, array{class: string, snapshot_store: ?string}>, snapshot_stores: array<string, array{type: string, service: string}>, migration: array{path: string, namespace: string}} $config
*/
$config = $this->processConfiguration($configuration, $configs);

Expand Down Expand Up @@ -232,16 +240,31 @@ private function configureSnapshots(array $config, ContainerBuilder $container):
}

/**
* @param array{aggregates: array<string, array{class: string, snapshot_store: ?string}>} $config
* @param array{aggregates_paths: list<string>, aggregates: array<string, array{class: string, snapshot_store: ?string}>} $config
*/
private function configureAggregates(array $config, ContainerBuilder $container): void
{
$container->setParameter('event_sourcing.aggregates', $this->aggregateHashMap($config['aggregates']));
$aggregates = $config['aggregates'];

foreach ($config['aggregates'] as $aggregateName => $definition) {
if (count($config['aggregates_paths']) > 0) {
$attributedAggregateClasses = (new AggregateAttributesLoader())->load($config['aggregates_paths']);
$duplicates = array_intersect_key($aggregates, $attributedAggregateClasses);

if (count($duplicates) > 0) {
$duplicateNames = implode(',', array_keys($duplicates));

throw new InvalidArgumentException('found following duplicate aggregate names: ' . $duplicateNames);
}

$aggregates = array_merge($aggregates, $attributedAggregateClasses);
}

$container->setParameter('event_sourcing.aggregates', $this->aggregateHashMap($aggregates));

foreach ($aggregates as $aggregateName => $definition) {
$id = sprintf('event_sourcing.repository.%s', $aggregateName);

if ($definition['snapshot_store']) {
if (is_string($definition['snapshot_store'])) {
$container->register($id, SnapshotRepository::class)
->setArguments([
new Reference(Store::class),
Expand Down
Loading