Skip to content

Commit

Permalink
Merge pull request #200 from patchlevel/add-auto-setup-listener
Browse files Browse the repository at this point in the history
add auto setup listener
  • Loading branch information
DavidBadura authored Apr 25, 2024
2 parents ec68bfd + c3ffa4b commit 23e5398
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
* groups: list<string>,
* limit: positive-int|null
* },
* auto_setup: array{
* enabled: bool,
* ids: list<string>,
* groups: list<string>,
* },
* rebuild_after_file_change: bool
* },
* connection: ?array{service: ?string, url: ?string},
Expand Down Expand Up @@ -145,6 +150,15 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()

->arrayNode('auto_setup')
->canBeEnabled()
->addDefaultsIfNotSet()
->children()
->arrayNode('ids')->scalarPrototype()->end()->end()
->arrayNode('groups')->scalarPrototype()->end()->end()
->end()
->end()

->booleanNode('rebuild_after_file_change')->defaultFalse()->end()
->end()
->end()
Expand Down
24 changes: 22 additions & 2 deletions src/DependencyInjection/PatchlevelEventSourcingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
use Patchlevel\EventSourcingBundle\DataCollector\MessageCollectorEventBus;
use Patchlevel\EventSourcingBundle\Doctrine\DbalConnectionFactory;
use Patchlevel\EventSourcingBundle\EventBus\SymfonyEventBus;
use Patchlevel\EventSourcingBundle\RequestListener\AutoSetupListener;
use Patchlevel\EventSourcingBundle\RequestListener\SubscriptionRebuildAfterFileChangeListener;
use Patchlevel\EventSourcingBundle\RequestListener\TraceListener;
use Patchlevel\EventSourcingBundle\ValueResolver\AggregateRootIdValueResolver;
Expand Down Expand Up @@ -358,6 +359,25 @@ static function (ChildDefinition $definition): void {
]);
}

if ($config['subscription']['auto_setup']['enabled']) {
$container->register(AutoSetupListener::class)
->setArguments([
new Reference(SubscriptionEngine::class),
$config['subscription']['auto_setup']['ids'] ?: null,
$config['subscription']['auto_setup']['groups'] ?: null,
])
->addTag('kernel.event_listener', [
'event' => 'kernel.request',
'priority' => 200,
'method' => 'onKernelRequest',
])
->addTag('kernel.event_listener', [
'priority' => 200,
'event' => 'console.command',
'method' => 'onConsoleCommand',
]);
}

if (!$config['subscription']['rebuild_after_file_change']) {
return;
}
Expand All @@ -371,11 +391,11 @@ static function (ChildDefinition $definition): void {
])
->addTag('kernel.event_listener', [
'event' => 'kernel.request',
'priority' => 200,
'priority' => 100,
'method' => 'onKernelRequest',
])
->addTag('kernel.event_listener', [
'priority' => 200,
'priority' => 100,
'event' => 'console.command',
'method' => 'onConsoleCommand',
]);
Expand Down
64 changes: 64 additions & 0 deletions src/RequestListener/AutoSetupListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcingBundle\RequestListener;

use Patchlevel\EventSourcing\Subscription\Engine\SubscriptionEngine;
use Patchlevel\EventSourcing\Subscription\Engine\SubscriptionEngineCriteria;
use Patchlevel\EventSourcing\Subscription\Status;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;

final class AutoSetupListener
{
/**
* @param list<string>|null $ids
* @param list<string>|null $groups
*/
public function __construct(
private readonly SubscriptionEngine $subscriptionEngine,
private readonly array|null $ids,
private readonly array|null $groups,
) {
}

public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}

$this->run();
}

public function onConsoleCommand(ConsoleCommandEvent $event): void
{
$this->run();
}

private function run(): void
{
$subscriptions = $this->subscriptionEngine->subscriptions(
new SubscriptionEngineCriteria(
$this->ids,
$this->groups,
),
);

$ids = [];

foreach ($subscriptions as $subscription) {
if ($subscription->status() !== Status::New) {
continue;
}

$ids[] = $subscription->id();
}

$this->subscriptionEngine->setup(
new SubscriptionEngineCriteria($ids),
true,
);
}
}

0 comments on commit 23e5398

Please sign in to comment.