Skip to content
Open
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"symfony/cache": "^6.4.0 || ^7.0.0 || ^8.0.0",
"symfony/config": "^6.4.0 || ^7.0.0 || ^8.0.0",
"symfony/console": "^6.4.1 || ^7.0.1 || ^8.0.0",
"symfony/dependency-injection": "^6.4.1 || ^7.0.1 || ^8.0.0",
"symfony/dependency-injection": "^7.3.0 || ^8.0.0",
"symfony/event-dispatcher": "^6.4.1 || ^7.0.1 || ^8.0.0",
"symfony/finder": "^6.4.0 || ^7.0.0 || ^8.0.0",
"symfony/http-kernel": "^6.4.1 || ^7.0.1 || ^8.0.0",
Expand Down
207 changes: 105 additions & 102 deletions composer.lock

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions src/DependencyInjection/CommandHandlerCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Patchlevel\EventSourcing\CommandBus\Handler\CreateAggregateHandler;
use Patchlevel\EventSourcing\CommandBus\Handler\UpdateAggregateHandler;
use Patchlevel\EventSourcing\CommandBus\HandlerFinder;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
use Patchlevel\EventSourcing\Repository\RepositoryManager;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -27,13 +26,10 @@

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

/** @var AggregateRootRegistry $aggregateRootRegistry */
$aggregateRootRegistry = $container->get(AggregateRootRegistry::class);

foreach ($aggregateRootRegistry->aggregateClasses() as $aggregateName => $aggregateClass) {
foreach ($container->getParameter('event_sourcing.aggregates') as $aggregateName => $aggregateClass) {

Check failure on line 29 in src/DependencyInjection/CommandHandlerCompilerPass.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.5, ubuntu-latest)

Argument of an invalid type array|bool|float|int|string|null supplied for foreach, only iterables are supported.
$parameterResolverId = sprintf('.event_sourcing.handler_parameter_resolver.%s', $aggregateName);

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

Check failure on line 32 in src/DependencyInjection/CommandHandlerCompilerPass.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.5, ubuntu-latest)

Parameter #1 $classString of static method Patchlevel\EventSourcing\CommandBus\HandlerFinder::findInClass() expects class-string, mixed given.
$handlerId = strtolower(sprintf('event_sourcing.handler.%s.%s', $aggregateName, $aggregateHandler->method));
$handlerClass = $aggregateHandler->static ? CreateAggregateHandler::class : UpdateAggregateHandler::class;

Expand Down
21 changes: 0 additions & 21 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@
* translators: list<string>
* }
* },
* 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{
Expand Down Expand Up @@ -143,24 +140,6 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()

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

->arrayNode('aggregates')
->beforeNormalization()->castToArray()->end()
->defaultValue([])
->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 @@ -7,7 +7,6 @@
use Patchlevel\EventSourcing\Attribute\Inject;
use Patchlevel\EventSourcing\CommandBus\Handler\ServiceNotResolvable;
use Patchlevel\EventSourcing\CommandBus\HandlerFinder;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
use Patchlevel\EventSourcingBundle\CommandBus\SymfonyParameterResolver;
use Psr\Container\ContainerInterface;
use ReflectionAttribute;
Expand All @@ -32,15 +31,12 @@
return;
}

/** @var AggregateRootRegistry $aggregateRootRegistry */
$aggregateRootRegistry = $container->get(AggregateRootRegistry::class);

foreach ($aggregateRootRegistry->aggregateClasses() as $aggregateName => $aggregateClass) {
foreach ($container->getParameter('event_sourcing.aggregates') as $aggregateName => $aggregateClass) {

Check failure on line 34 in src/DependencyInjection/HandlerServiceLocatorCompilerPass.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.5, ubuntu-latest)

Argument of an invalid type array|bool|float|int|string|null supplied for foreach, only iterables are supported.
$parameterResolverId = sprintf('.event_sourcing.handler_parameter_resolver.%s', $aggregateName);
$services = [];

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

Check failure on line 38 in src/DependencyInjection/HandlerServiceLocatorCompilerPass.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.5, ubuntu-latest)

Parameter #1 $classString of static method Patchlevel\EventSourcing\CommandBus\HandlerFinder::findInClass() expects class-string, mixed given.
$services += $this->services(new ReflectionMethod($aggregateClass, $aggregateHandler->method), $container);

Check failure on line 39 in src/DependencyInjection/HandlerServiceLocatorCompilerPass.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.5, ubuntu-latest)

Parameter #1 $objectOrMethod of class ReflectionMethod constructor expects object|string, mixed given.
}

$container->register($parameterResolverId, SymfonyParameterResolver::class)
Expand Down
68 changes: 29 additions & 39 deletions src/DependencyInjection/PatchlevelEventSourcingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Doctrine\ORM\Tools\ToolEvents;
use Patchlevel\EventSourcing\Attribute\Aggregate;
use Patchlevel\EventSourcing\Attribute\Event;
use Patchlevel\EventSourcing\Attribute\Header;
use Patchlevel\EventSourcing\Attribute\Processor;
use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\Attribute\Subscriber;
Expand Down Expand Up @@ -61,9 +62,7 @@
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootMetadataAwareMetadataFactory;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootMetadataFactory;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AttributeAggregateRootRegistryFactory;
use Patchlevel\EventSourcing\Metadata\Event\AttributeEventMetadataFactory;
use Patchlevel\EventSourcing\Metadata\Event\AttributeEventRegistryFactory;
use Patchlevel\EventSourcing\Metadata\Event\EventMetadataFactory;
use Patchlevel\EventSourcing\Metadata\Event\EventRegistry;
use Patchlevel\EventSourcing\Metadata\Message\AttributeMessageHeaderRegistryFactory;
Expand Down Expand Up @@ -153,6 +152,7 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;

use function class_exists;
Expand All @@ -164,8 +164,6 @@ final class PatchlevelEventSourcingExtension extends Extension
/** @param array<array-key, mixed> $configs */
public function load(array $configs, ContainerBuilder $container): void
{
$this->removeNonServices($container);

$configuration = new Configuration();

/** @var Config $config */
Expand All @@ -185,7 +183,7 @@ public function load(array $configs, ContainerBuilder $container): void
$this->configureConnection($config, $container);
$this->configureStore($config, $container);
$this->configureSnapshots($config, $container);
$this->configureAggregates($config, $container);
$this->configureAggregates($container);
$this->configureCommands($container);
$this->configureProfiler($container);
$this->configureClock($config, $container);
Expand All @@ -202,11 +200,17 @@ public function load(array $configs, ContainerBuilder $container): void
/** @param Config $config */
private function configureSerializer(array $config, ContainerBuilder $container): void
{
$container->register(AttributeEventRegistryFactory::class);
$container->registerAttributeForAutoconfiguration(
Event::class,
static function (ChildDefinition $definition, Event $attribute): void {
$definition->addResourceTag('event_sourcing.event', ['name' => $attribute->name]);
},
);

$container->setParameter('event_sourcing.events', []);

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

$container->register(AttributeEventMetadataFactory::class);
$container->setAlias(EventMetadataFactory::class, AttributeEventMetadataFactory::class);
Expand All @@ -224,12 +228,15 @@ private function configureSerializer(array $config, ContainerBuilder $container)

$container->setAlias(EventSerializer::class, DefaultEventSerializer::class);

$container->register(AttributeMessageHeaderRegistryFactory::class);
$container->setAlias(MessageHeaderRegistryFactory::class, AttributeMessageHeaderRegistryFactory::class);
$container->registerAttributeForAutoconfiguration(
Header::class,
static function (ChildDefinition $definition, Header $attribute): void {
$definition->addResourceTag('event_sourcing.header', ['name' => $attribute->name]);
},
);

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

$container->register(DefaultHeadersSerializer::class)
->setArguments([
Expand Down Expand Up @@ -879,17 +886,22 @@ private function configureSnapshots(array $config, ContainerBuilder $container):
$container->setAlias(SnapshotStore::class, DefaultSnapshotStore::class);
}

/** @param Config $config */
private function configureAggregates(array $config, ContainerBuilder $container): void
private function configureAggregates(ContainerBuilder $container): void
{
$container->registerAttributeForAutoconfiguration(
Aggregate::class,
static function (ChildDefinition $definition, Aggregate $attribute): void {
$definition->addResourceTag('event_sourcing.aggregate', ['name' => $attribute->name]);
},
);

$container->register(AggregateRootMetadataAwareMetadataFactory::class);
$container->setAlias(AggregateRootMetadataFactory::class, AggregateRootMetadataAwareMetadataFactory::class);

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

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

$container->register(DefaultRepositoryManager::class)
->setArguments([
Expand Down Expand Up @@ -1221,26 +1233,4 @@ private function configureValueResolver(ContainerBuilder $container): void
$container->register(IdentifierValueResolver::class)
->addTag('controller.argument_value_resolver', ['priority' => 200]);
}

private function removeNonServices(ContainerBuilder $container): void
{
$container->registerAttributeForAutoconfiguration(
Aggregate::class,
static function (ChildDefinition $definition): void {
$definition->setAbstract(true)->addTag(
'container.excluded',
['source' => sprintf('with #[%s] attribute', Aggregate::class)],
);
},
);
$container->registerAttributeForAutoconfiguration(
Event::class,
static function (ChildDefinition $definition): void {
$definition->setAbstract(true)->addTag(
'container.excluded',
['source' => sprintf('with #[%s] attribute', Event::class)],
);
},
);
}
}
12 changes: 2 additions & 10 deletions src/DependencyInjection/RepositoryCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

namespace Patchlevel\EventSourcingBundle\DependencyInjection;

use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
use Patchlevel\EventSourcing\Repository\Repository;
use Patchlevel\EventSourcing\Repository\RepositoryManager;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

Expand All @@ -18,20 +16,14 @@
{
public function process(ContainerBuilder $container): void
{
$aggregateRootRegistry = $container->get(AggregateRootRegistry::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);

if (!$aggregateRootRegistry instanceof AggregateRootRegistry) {
return;
}

foreach ($aggregateRootRegistry->aggregateNames() as $aggregateName) {
foreach ($container->getParameter('event_sourcing.aggregates') as $aggregateName => $aggregateClass) {

Check failure on line 19 in src/DependencyInjection/RepositoryCompilerPass.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.5, ubuntu-latest)

Argument of an invalid type array|bool|float|int|string|null supplied for foreach, only iterables are supported.
$aggregateRepositoryName = $aggregateName . 'Repository';
$aggregateRepositoryId = 'event_sourcing.' . $aggregateName . '.repository';

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

$container->setDefinition($aggregateRepositoryId, $definition);
$container->registerAliasForArgument($aggregateRepositoryId, Repository::class, $aggregateRepositoryName)->setPublic(false);
Expand Down
32 changes: 32 additions & 0 deletions src/DependencyInjection/ResourceCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcingBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/** @internal */
final class ResourceCompilerPass implements CompilerPassInterface
{
private const RESSOURCES = [
'event_sourcing.aggregate' => 'event_sourcing.aggregates',
'event_sourcing.event' => 'event_sourcing.events',
'event_sourcing.header' => 'event_sourcing.headers',
];

public function process(ContainerBuilder $container): void
{
foreach (self::RESSOURCES as $tag => $parameter) {
$map = [];
foreach ($container->findTaggedResourceIds($tag) as $id => $tags) {
foreach ($tags as $tag) {
$map[$tag['name']] = $id;

Check failure on line 25 in src/DependencyInjection/ResourceCompilerPass.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.5, ubuntu-latest)

Possibly invalid array key type mixed.

Check failure on line 25 in src/DependencyInjection/ResourceCompilerPass.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.5, ubuntu-latest)

Cannot access offset 'name' on mixed.
}
}

$container->setParameter($parameter, $map);
}
}
}
2 changes: 2 additions & 0 deletions src/PatchlevelEventSourcingBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Patchlevel\EventSourcingBundle;

use Patchlevel\EventSourcingBundle\DependencyInjection\ResourceCompilerPass;
use Patchlevel\EventSourcingBundle\DependencyInjection\CommandHandlerCompilerPass;
use Patchlevel\EventSourcingBundle\DependencyInjection\HandlerServiceLocatorCompilerPass;
use Patchlevel\EventSourcingBundle\DependencyInjection\QueryHandlerCompilerPass;
Expand All @@ -17,6 +18,7 @@ final class PatchlevelEventSourcingBundle extends Bundle
{
public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new ResourceCompilerPass(), priority: 100);
$container->addCompilerPass(new RepositoryCompilerPass());
$container->addCompilerPass(new SubscriberGuardCompilePass());
$container->addCompilerPass(new CommandHandlerCompilerPass(), priority: 100);
Expand Down
Loading
Loading