1. Go to this page and download the library: Download neos/cqrs library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
neos / cqrs example snippets
use Neos\EventSourcing\EventStore\EventStoreFactory;
use Neos\Flow\Annotations as Flow;
class SomeClass {
/**
* @Flow\Inject
* @var EventStoreFactory
*/
protected $eventStoreFactory;
function someMethod() {
$eventStore = $this->eventStoreFactory->create('Some.Package:EventStore');
}
}
use Neos\EventSourcing\EventStore\EventStore;
use Neos\Flow\Annotations as Flow;
class SomeClass {
/**
* @Flow\Inject
* @var EventStore
*/
protected $eventStore;
function someMethod() {
// $this->eventStore->...
}
}
use Neos\EventSourcing\EventStore\EventStore;
use Neos\Flow\Annotations as Flow;
class SomeClass {
/**
* @Flow\Inject(name="Some.Package:EventStore")
* @var EventStore
*/
protected $eventStore;
}
namespace Some\Package;
use Neos\EventSourcing\Event\DomainEvents;
use Neos\EventSourcing\EventStore\EventStore;
use Neos\EventSourcing\EventStore\EventStoreFactory;
use Neos\EventSourcing\EventStore\EventStream;
use Neos\EventSourcing\EventStore\ExpectedVersion;
use Neos\EventSourcing\EventStore\StreamName;
use Neos\Flow\Annotations as Flow;
/**
* @Flow\Scope("singleton")
*/
final class CustomEventStore
{
/**
* @var EventStore
*/
private $instance;
public function __construct(EventStoreFactory $eventStoreFactory)
{
$this->instance = $eventStoreFactory->create('Some.Package:EventStore');
}
public function load(StreamName $streamName, int $minimumSequenceNumber = 0): EventStream
{
return $this->instance->load($streamName, $minimumSequenceNumber);
}
public function commit(StreamName $streamName, DomainEvents $events, int $expectedVersion = ExpectedVersion::ANY): void
{
$this->instance->commit($streamName, $events, $expectedVersion);
}
}
namespace Some\Package;
use Neos\EventSourcing\Event\DomainEventInterface;
final class SomethingHasHappened implements DomainEventInterface
{
/**
* @var string
*/
private $message;
public function __construct(string $message)
{
$this->message = $message;
}
public function getMessage(): string
{
return $this->message;
}
}
$event = new SomethingHasHappened('some message');
$streamName = StreamName::fromString('some-stream');
$eventStore->commit($streamName, DomainEvents::withSingleEvent($event));
$streamName = StreamName::fromString('some-stream');
$eventStream = $eventStore->load($streamName);
foreach ($eventStream as $eventEnvelope) {
// the event as it's stored in the Event Store, including its global sequence number and the serialized payload
$rawEvent = $eventEnvelope->getRawEvent();
// the deserialized DomainEventInterface instance
$domainEvent = $eventEnvelope->getDomainEvent();
}
namespace Some\Package;
use Neos\EventSourcing\EventListener\EventListenerInterface;
use Some\Package\SomethingHasHappened;
class SomeEventListener implements EventListenerInterface
{
public function whenSomethingHasHappened(SomethingHasHappened $event): void
{
// do something with the $event
}
}
// $eventStore is created by EventStoreFactory::create()
// $someListener is the instanciated projector (a class implementing EventListenerInterface or ProjectorInterface)
// usually $someListener can be injeced using @Flow\Inject(
// $dbalConnection is the database connection being used to read and update the "reading point" of the projector,
// i.e. how many events it has already seen. (interally implemented by DoctrineAppliedEventsStorage, and by default
// stored in the database table neos_eventsourcing_eventlistener_appliedeventslog).
// In a Flow Application, you can retrieve this $dbalConnection most simply by using $this->entityManager->getConnection() - where
// $this->entityManager is an injected instance of Doctrine\ORM\EntityManagerInterface.
$eventListenerInvoker = new EventListenerInvoker($eventStore, $someListener, $dbalConnection);
$eventListenerInvoker->catchup();
declare(strict_types=1);
namespace Some\Package;
use Neos\EventSourcing\AbstractEventSourcedAggregateRoot;
final class SomeAggregate extends AbstractEventSourcedAggregateRoot
{
/**
* @var SomeAggregateId
*/
private $id;
public static function create(SomeAggregateId $id): self
{
$instance = new static();
// This method will only be invoked once. Upon reconstitution only the when*() methods are called.
// So we must never change the instance state directly (i.e. $instance->id = $id) but use events:
$instance->recordThat(new SomeAggregateWasCreated($id));
return $instance;
}
public function whenSomeAggregateWasCreated(SomeAggregateWasCreated $event): void
{
$this->id = $event->getId();
}
}
final class ProductRepository
{
/**
* @var EventStore
*/
private $eventStore;
public function __construct(EventStore $eventStore)
{
$this->eventStore = $eventStore;
}
public function load(SomeAggregateId $id): SomeAggregate
{
$streamName = $this->getStreamName($id);
return SomeAggregate::reconstituteFromEventStream($this->eventStore->load($streamName));
}
public function save(SomeAggregate $aggregate): void
{
$streamName = $this->getStreamName($aggregate->id());
$this->eventStore->commit($streamName, $aggregate->pullUncommittedEvents(), $aggregate->getReconstitutionVersion());
}
private function getStreamName(SomeAggregateId $id): StreamName
{
// we assume that the aggregate stream name is "some-aggregate-<aggregate-id>"
return StreamName::fromString('some-aggregate-' . $id);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.