wwwision / subscription-engine-neos-adapter
neos/eventstore adapter for wwwision/subscription-engine
Package info
github.com/bwaidelich/subscription-engine-neos-adapter
pkg:composer/wwwision/subscription-engine-neos-adapter
Requires
- php: >=8.3
- neos/eventstore: ^1.0.3 || ^2.0.2
- wwwision/subscription-engine: ^2
Requires (Dev)
- doctrine/dbal: ^3.6
- neos/eventstore-doctrineadapter: ^2.0.1 || ^3
- phpstan/phpstan: ^2
- phpunit/phpunit: ^11
- psr/clock: ^1
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^4
Suggests
None
Provides
None
Conflicts
- neos/eventstore-doctrineadapter: <2.0.1
Replaces
None
README
Keep your projections up to date with the events of a neos/eventstore.
This package connects the event store to the Subscription Engine. Your subscribers receive each event as an EventEnvelope.
Installation
Install the adapter together with an event store implementation and a subscription store, for example:
composer require wwwision/subscription-engine-neos-adapter neos/eventstore-doctrineadapter wwwision/subscription-engine-doctrine
Usage
<?php declare(strict_types=1); use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Tools\DsnParser; use Neos\EventStore\DoctrineAdapter\DoctrineEventStore; use Neos\EventStore\Model\Event; use Neos\EventStore\Model\Event\EventData; use Neos\EventStore\Model\Event\EventId; use Neos\EventStore\Model\Event\EventType; use Neos\EventStore\Model\Event\StreamName; use Neos\EventStore\Model\EventEnvelope; use Neos\EventStore\Model\EventStream\ExpectedVersion; use Psr\Clock\ClockInterface; use Wwwision\SubscriptionEngine\Subscriber\Subscriber; use Wwwision\SubscriptionEngine\Subscriber\Subscribers; use Wwwision\SubscriptionEngine\Subscription\RunMode; use Wwwision\SubscriptionEngine\SubscriptionEngine; use Wwwision\SubscriptionEngineDoctrine\DoctrineSubscriptionStore; use Wwwision\SubscriptionEngineNeosAdapter\NeosEventStoreAdapter; require __DIR__ . '/vendor/autoload.php'; $connection = DriverManager::getConnection((new DsnParser())->parse('pdo-sqlite:///' . __DIR__ . '/demo.sqlite')); $clock = new class implements ClockInterface { public function now(): DateTimeImmutable { return new DateTimeImmutable(); } }; $eventStore = new DoctrineEventStore($connection, eventTableName: 'events', clock: $clock); $engine = new SubscriptionEngine( eventStoreAdapter: new NeosEventStoreAdapter($eventStore), subscriptionStore: new DoctrineSubscriptionStore($connection, tableName: 'subscriptions'), subscribers: Subscribers::fromArray([ Subscriber::create( id: 'some-projection', handler: fn (EventEnvelope $envelope) => print("Projecting {$envelope->event->type->value} at position {$envelope->sequenceNumber->value}\n"), reset: fn () => print("Resetting projection for replay\n"), ), Subscriber::create( id: 'some-process', handler: fn (EventEnvelope $envelope) => print("Invoking process for {$envelope->event->type->value}\n"), runMode: RunMode::FROM_NOW, setup: fn () => print("Setting up process\n"), ), ]), ); // Once per deployment $eventStore->setup(); $engine->setup(); $engine->boot(); // Store an event and pass it to the subscribers $eventStore->commit( StreamName::fromString('some-stream'), new Event(EventId::create(), EventType::fromString('SomethingHappened'), EventData::fromString('{"foo": "bar"}')), ExpectedVersion::ANY(), ); $engine->catchUpActive();
Setting up process Invoking process for SomethingHappened Projecting SomethingHappened at position 1
Filtered subscribers
A subscriber can declare a NeosEventFilter to receive only the events of one stream, optionally narrowed to some event types:
use Neos\EventStore\Model\Event\EventType; use Neos\EventStore\Model\Event\EventTypes; use Neos\EventStore\Model\EventStream\EventStreamFilter; use Neos\EventStore\Model\EventStream\VirtualStreamName; use Wwwision\SubscriptionEngineNeosAdapter\NeosEventFilter; Subscriber::create( id: 'orders', handler: $handler, filter: NeosEventFilter::create( VirtualStreamName::forCategory('order-'), EventStreamFilter::create(EventTypes::create(EventType::fromString('OrderPlaced'))), ), );
If a run contains several subscriptions, the engine applies the filters itself: Stream names are compared exactly, categories by prefix and correlation ids by equality. Avoid % and _ in categories, and case-insensitive collations, where the database would decide differently. See Filtered subscribers for how the engine applies filters.
Next steps
The guides of the Subscription Engine explain how to rebuild projections, handle failures and more. To process events in the background, see wwwision/subscription-engine-worker.
Contribution
Contributions in the form of issues or pull requests are highly appreciated
License
See LICENSE