Skip to content

Commit

Permalink
add projectionist auto boot feature
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Aug 6, 2023
1 parent fd63faa commit 1df169a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->addDefaultsIfNotSet()
->children()
->booleanNode('sync')->defaultValue(true)->end()
->booleanNode('auto_boot')->defaultValue(false)->end()
->end()
->end()

Expand Down
12 changes: 11 additions & 1 deletion src/DependencyInjection/PatchlevelEventSourcingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
use Patchlevel\EventSourcing\WatchServer\WatchServerClient;
use Patchlevel\EventSourcingBundle\DataCollector\EventSourcingCollector;
use Patchlevel\EventSourcingBundle\DataCollector\MessageListener;
use Patchlevel\EventSourcingBundle\Listener\ProjectionistAutoBootListener;
use Patchlevel\Hydrator\Hydrator;
use Patchlevel\Hydrator\MetadataHydrator;
use Psr\Log\LoggerInterface;
Expand All @@ -101,7 +102,7 @@
/**
* @psalm-type Config = array{
* event_bus: ?array{type: string, service: string},
* projection: array{sync: bool},
* projection: array{sync: bool, auto_boot: bool},
* watch_server: array{enabled: bool, host: string},
* connection: ?array{service: ?string, url: ?string},
* store: array{merge_orm_schema: bool, options: array<string, mixed>},
Expand Down Expand Up @@ -272,6 +273,15 @@ private function configureProjection(array $config, ContainerBuilder $container)
])
->addTag('console.command');

if ($config['projection']['auto_boot']) {
$container->register(ProjectionistAutoBootListener::class)
->setArguments([
new Reference(Projectionist::class),
new Reference('lock.default.factory'),
])
->addTag('kernel.event_listener');
}

if (!$config['projection']['sync']) {
return;
}
Expand Down
33 changes: 33 additions & 0 deletions src/Listener/ProjectionistAutoBootListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcingBundle\Listener;

use Patchlevel\EventSourcing\Projection\Projectionist\Projectionist;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Lock\LockFactory;

final class ProjectionistAutoBootListener
{
public function __construct(
private readonly Projectionist $projectionist,
private readonly LockFactory $lockFactory,
) {
}

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

$lock = $this->lockFactory->createLock('projectionist-boot');

if (!$lock->acquire()) {
return;
}

$this->projectionist->boot();
}
}
22 changes: 22 additions & 0 deletions tests/Unit/PatchlevelEventSourcingBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use Patchlevel\EventSourcing\WatchServer\WatchServer;
use Patchlevel\EventSourcing\WatchServer\WatchServerClient;
use Patchlevel\EventSourcingBundle\DependencyInjection\PatchlevelEventSourcingExtension;
use Patchlevel\EventSourcingBundle\Listener\ProjectionistAutoBootListener;
use Patchlevel\EventSourcingBundle\PatchlevelEventSourcingBundle;
use Patchlevel\EventSourcingBundle\Tests\Fixtures\CreatedSubscriber;
use Patchlevel\EventSourcingBundle\Tests\Fixtures\Processor1;
Expand Down Expand Up @@ -680,6 +681,27 @@ public function testProjectionistAsync(): void
self::assertInstanceOf(DefaultEventBus::class, $container->get(EventBus::class));
}

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

$this->compileContainer(
$container,
[
'patchlevel_event_sourcing' => [
'connection' => [
'service' => 'doctrine.dbal.eventstore_connection',
],
'projection' => [
'auto_boot' => true,
],
],
]
);

self::assertInstanceOf(ProjectionistAutoBootListener::class, $container->get(ProjectionistAutoBootListener::class));
}

public function testSchemaMerge(): void
{
$container = new ContainerBuilder();
Expand Down

0 comments on commit 1df169a

Please sign in to comment.