Skip to content

Commit 40d2656

Browse files
committed
use resource tags for aggregates & events
1 parent 073ca58 commit 40d2656

10 files changed

+207
-236
lines changed

composer.lock

Lines changed: 104 additions & 101 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcingBundle\DependencyInjection;
6+
7+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
10+
/** @internal */
11+
final class AggregateCompilerPass implements CompilerPassInterface
12+
{
13+
public function process(ContainerBuilder $container): void
14+
{
15+
$aggregates = [];
16+
foreach ($container->findTaggedResourceIds('event_sourcing.aggregate') as $id => $tags) {
17+
foreach ($tags as $tag) {
18+
$aggregates[$tag['name']] = $id;
19+
}
20+
}
21+
22+
$container->setParameter('event_sourcing.aggregates', $aggregates);
23+
}
24+
}

src/DependencyInjection/CommandHandlerCompilerPass.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Patchlevel\EventSourcing\CommandBus\Handler\CreateAggregateHandler;
88
use Patchlevel\EventSourcing\CommandBus\Handler\UpdateAggregateHandler;
99
use Patchlevel\EventSourcing\CommandBus\HandlerFinder;
10-
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
1110
use Patchlevel\EventSourcing\Repository\RepositoryManager;
1211
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1312
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -27,10 +26,7 @@ public function process(ContainerBuilder $container): void
2726

2827
$bus = $container->getParameter('patchlevel_event_sourcing.aggregate_handlers.bus');
2928

30-
/** @var AggregateRootRegistry $aggregateRootRegistry */
31-
$aggregateRootRegistry = $container->get(AggregateRootRegistry::class);
32-
33-
foreach ($aggregateRootRegistry->aggregateClasses() as $aggregateName => $aggregateClass) {
29+
foreach ($container->getParameter('event_sourcing.aggregates') as $aggregateName => $aggregateClass) {
3430
$parameterResolverId = sprintf('.event_sourcing.handler_parameter_resolver.%s', $aggregateName);
3531

3632
foreach (HandlerFinder::findInClass($aggregateClass) as $aggregateHandler) {

src/DependencyInjection/Configuration.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@
6565
* translators: list<string>
6666
* }
6767
* },
68-
* aggregates: list<string>,
69-
* events: list<string>,
7068
* headers: list<string>,
7169
* snapshot_stores: array<string, array{type: string, service: string}>,
7270
* migration: array{path: string, namespace: string},
@@ -143,18 +141,6 @@ public function getConfigTreeBuilder(): TreeBuilder
143141
->end()
144142
->end()
145143

146-
->arrayNode('events')
147-
->beforeNormalization()->castToArray()->end()
148-
->defaultValue([])
149-
->scalarPrototype()->end()
150-
->end()
151-
152-
->arrayNode('aggregates')
153-
->beforeNormalization()->castToArray()->end()
154-
->defaultValue([])
155-
->scalarPrototype()->end()
156-
->end()
157-
158144
->arrayNode('headers')
159145
->beforeNormalization()->castToArray()->end()
160146
->defaultValue([])
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcingBundle\DependencyInjection;
6+
7+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
10+
/** @internal */
11+
final class EventCompilerPass implements CompilerPassInterface
12+
{
13+
public function process(ContainerBuilder $container): void
14+
{
15+
$events = [];
16+
foreach ($container->findTaggedResourceIds('event_sourcing.event') as $id => $tags) {
17+
foreach ($tags as $tag) {
18+
$events[$tag['name']] = $id;
19+
}
20+
}
21+
22+
$container->setParameter('event_sourcing.events', $events);
23+
}
24+
}

src/DependencyInjection/HandlerServiceLocatorCompilerPass.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Patchlevel\EventSourcing\Attribute\Inject;
88
use Patchlevel\EventSourcing\CommandBus\Handler\ServiceNotResolvable;
99
use Patchlevel\EventSourcing\CommandBus\HandlerFinder;
10-
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
1110
use Patchlevel\EventSourcingBundle\CommandBus\SymfonyParameterResolver;
1211
use Psr\Container\ContainerInterface;
1312
use ReflectionAttribute;
@@ -32,10 +31,7 @@ public function process(ContainerBuilder $container): void
3231
return;
3332
}
3433

35-
/** @var AggregateRootRegistry $aggregateRootRegistry */
36-
$aggregateRootRegistry = $container->get(AggregateRootRegistry::class);
37-
38-
foreach ($aggregateRootRegistry->aggregateClasses() as $aggregateName => $aggregateClass) {
34+
foreach ($container->getParameter('event_sourcing.aggregates') as $aggregateName => $aggregateClass) {
3935
$parameterResolverId = sprintf('.event_sourcing.handler_parameter_resolver.%s', $aggregateName);
4036
$services = [];
4137

src/DependencyInjection/PatchlevelEventSourcingExtension.php

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@
6161
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootMetadataAwareMetadataFactory;
6262
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootMetadataFactory;
6363
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
64-
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AttributeAggregateRootRegistryFactory;
6564
use Patchlevel\EventSourcing\Metadata\Event\AttributeEventMetadataFactory;
66-
use Patchlevel\EventSourcing\Metadata\Event\AttributeEventRegistryFactory;
6765
use Patchlevel\EventSourcing\Metadata\Event\EventMetadataFactory;
6866
use Patchlevel\EventSourcing\Metadata\Event\EventRegistry;
6967
use Patchlevel\EventSourcing\Metadata\Message\AttributeMessageHeaderRegistryFactory;
@@ -153,6 +151,7 @@
153151
use Symfony\Component\DependencyInjection\ContainerInterface;
154152
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
155153
use Symfony\Component\DependencyInjection\Extension\Extension;
154+
use Symfony\Component\DependencyInjection\Parameter;
156155
use Symfony\Component\DependencyInjection\Reference;
157156

158157
use function class_exists;
@@ -164,8 +163,6 @@ final class PatchlevelEventSourcingExtension extends Extension
164163
/** @param array<array-key, mixed> $configs */
165164
public function load(array $configs, ContainerBuilder $container): void
166165
{
167-
$this->removeNonServices($container);
168-
169166
$configuration = new Configuration();
170167

171168
/** @var Config $config */
@@ -185,7 +182,7 @@ public function load(array $configs, ContainerBuilder $container): void
185182
$this->configureConnection($config, $container);
186183
$this->configureStore($config, $container);
187184
$this->configureSnapshots($config, $container);
188-
$this->configureAggregates($config, $container);
185+
$this->configureAggregates($container);
189186
$this->configureCommands($container);
190187
$this->configureProfiler($container);
191188
$this->configureClock($config, $container);
@@ -202,11 +199,17 @@ public function load(array $configs, ContainerBuilder $container): void
202199
/** @param Config $config */
203200
private function configureSerializer(array $config, ContainerBuilder $container): void
204201
{
205-
$container->register(AttributeEventRegistryFactory::class);
202+
$container->registerAttributeForAutoconfiguration(
203+
Event::class,
204+
static function (ChildDefinition $definition, Event $attribute): void {
205+
$definition->addResourceTag('event_sourcing.event', ['name' => $attribute->name]);
206+
},
207+
);
208+
209+
$container->setParameter('event_sourcing.events', []);
206210

207211
$container->register(EventRegistry::class)
208-
->setFactory([new Reference(AttributeEventRegistryFactory::class), 'create'])
209-
->setArguments([$config['events']]);
212+
->setArguments([new Parameter('event_sourcing.events')]);
210213

211214
$container->register(AttributeEventMetadataFactory::class);
212215
$container->setAlias(EventMetadataFactory::class, AttributeEventMetadataFactory::class);
@@ -879,17 +882,22 @@ private function configureSnapshots(array $config, ContainerBuilder $container):
879882
$container->setAlias(SnapshotStore::class, DefaultSnapshotStore::class);
880883
}
881884

882-
/** @param Config $config */
883-
private function configureAggregates(array $config, ContainerBuilder $container): void
885+
private function configureAggregates(ContainerBuilder $container): void
884886
{
887+
$container->registerAttributeForAutoconfiguration(
888+
Aggregate::class,
889+
static function (ChildDefinition $definition, Aggregate $attribute): void {
890+
$definition->addResourceTag('event_sourcing.aggregate', ['name' => $attribute->name]);
891+
},
892+
);
893+
885894
$container->register(AggregateRootMetadataAwareMetadataFactory::class);
886895
$container->setAlias(AggregateRootMetadataFactory::class, AggregateRootMetadataAwareMetadataFactory::class);
887896

888-
$container->register(AttributeAggregateRootRegistryFactory::class);
897+
$container->setParameter('event_sourcing.aggregates', []);
889898

890899
$container->register(AggregateRootRegistry::class)
891-
->setFactory([new Reference(AttributeAggregateRootRegistryFactory::class), 'create'])
892-
->setArguments([$config['aggregates']]);
900+
->setArguments([new Parameter('event_sourcing.aggregates')]);
893901

894902
$container->register(DefaultRepositoryManager::class)
895903
->setArguments([
@@ -1221,26 +1229,4 @@ private function configureValueResolver(ContainerBuilder $container): void
12211229
$container->register(IdentifierValueResolver::class)
12221230
->addTag('controller.argument_value_resolver', ['priority' => 200]);
12231231
}
1224-
1225-
private function removeNonServices(ContainerBuilder $container): void
1226-
{
1227-
$container->registerAttributeForAutoconfiguration(
1228-
Aggregate::class,
1229-
static function (ChildDefinition $definition): void {
1230-
$definition->setAbstract(true)->addTag(
1231-
'container.excluded',
1232-
['source' => sprintf('with #[%s] attribute', Aggregate::class)],
1233-
);
1234-
},
1235-
);
1236-
$container->registerAttributeForAutoconfiguration(
1237-
Event::class,
1238-
static function (ChildDefinition $definition): void {
1239-
$definition->setAbstract(true)->addTag(
1240-
'container.excluded',
1241-
['source' => sprintf('with #[%s] attribute', Event::class)],
1242-
);
1243-
},
1244-
);
1245-
}
12461232
}

src/DependencyInjection/RepositoryCompilerPass.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
namespace Patchlevel\EventSourcingBundle\DependencyInjection;
66

7-
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
87
use Patchlevel\EventSourcing\Repository\Repository;
98
use Patchlevel\EventSourcing\Repository\RepositoryManager;
109
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1110
use Symfony\Component\DependencyInjection\ContainerBuilder;
12-
use Symfony\Component\DependencyInjection\ContainerInterface;
1311
use Symfony\Component\DependencyInjection\Definition;
1412
use Symfony\Component\DependencyInjection\Reference;
1513

@@ -18,20 +16,14 @@ final class RepositoryCompilerPass implements CompilerPassInterface
1816
{
1917
public function process(ContainerBuilder $container): void
2018
{
21-
$aggregateRootRegistry = $container->get(AggregateRootRegistry::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
22-
23-
if (!$aggregateRootRegistry instanceof AggregateRootRegistry) {
24-
return;
25-
}
26-
27-
foreach ($aggregateRootRegistry->aggregateNames() as $aggregateName) {
19+
foreach ($container->getParameter('event_sourcing.aggregates') as $aggregateName => $aggregateClass) {
2820
$aggregateRepositoryName = $aggregateName . 'Repository';
2921
$aggregateRepositoryId = 'event_sourcing.' . $aggregateName . '.repository';
3022

3123
$definition = new Definition(Repository::class);
3224
$definition->setPublic(false);
3325
$definition->setFactory([new Reference(RepositoryManager::class), 'get']);
34-
$definition->setArgument(0, $aggregateRootRegistry->aggregateClass($aggregateName));
26+
$definition->setArgument(0, $aggregateClass);
3527

3628
$container->setDefinition($aggregateRepositoryId, $definition);
3729
$container->registerAliasForArgument($aggregateRepositoryId, Repository::class, $aggregateRepositoryName)->setPublic(false);

src/PatchlevelEventSourcingBundle.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Patchlevel\EventSourcingBundle;
66

7+
use Patchlevel\EventSourcingBundle\DependencyInjection\AggregateCompilerPass;
78
use Patchlevel\EventSourcingBundle\DependencyInjection\CommandHandlerCompilerPass;
9+
use Patchlevel\EventSourcingBundle\DependencyInjection\EventCompilerPass;
810
use Patchlevel\EventSourcingBundle\DependencyInjection\HandlerServiceLocatorCompilerPass;
911
use Patchlevel\EventSourcingBundle\DependencyInjection\QueryHandlerCompilerPass;
1012
use Patchlevel\EventSourcingBundle\DependencyInjection\RepositoryCompilerPass;
@@ -17,6 +19,8 @@ final class PatchlevelEventSourcingBundle extends Bundle
1719
{
1820
public function build(ContainerBuilder $container): void
1921
{
22+
$container->addCompilerPass(new AggregateCompilerPass(), priority: 100);
23+
$container->addCompilerPass(new EventCompilerPass(), priority: 100);
2024
$container->addCompilerPass(new RepositoryCompilerPass());
2125
$container->addCompilerPass(new SubscriberGuardCompilePass());
2226
$container->addCompilerPass(new CommandHandlerCompilerPass(), priority: 100);

0 commit comments

Comments
 (0)