PHP code example of andreo / eventsauce-snapshotting
1. Go to this page and download the library: Download andreo/eventsauce-snapshotting 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/ */
use Andreo\EventSauce\Snapshotting\Versioned\VersionedSnapshotState;
final class FooSnapshotStateV2 implements VersionedSnapshotState {
public static function getSnapshotVersion(): int|string|Stringable
{
return 2;
}
}
use Andreo\EventSauce\Snapshotting\Aggregate\VersionedSnapshottingBehaviour;
use EventSauce\EventSourcing\AggregateRootBehaviour;
use EventSauce\EventSourcing\AggregateRootId;
use EventSauce\EventSourcing\Snapshotting\AggregateRootWithSnapshotting;
final class FooAggregate implements AggregateRootWithSnapshotting
{
use AggregateRootBehaviour;
use VersionedSnapshottingBehaviour;
// Create snapshot method must have type hint of VersionedSnapshotState implementation (with default SnapshotVersionInflector)
protected function createSnapshotState(): FooSnapshotStateV2
{
return new FooSnapshotStateV2();
}
protected static function reconstituteFromSnapshotState(AggregateRootId $id, $state): AggregateRootWithSnapshotting
{
assert($state instanceof FooSnapshotStateV2);
}
}
interface ConditionalSnapshotStrategy
{
public function canStoreSnapshot(AggregateRootWithSnapshotting $aggregateRoot): bool;
}
use Andreo\EventSauce\Snapshotting\Conditional\AggregateRootRepositoryWithConditionalSnapshot;use Andreo\EventSauce\Snapshotting\Conditional\EveryNEventConditionalSnapshotStrategy;
new AggregateRootRepositoryWithConditionalSnapshot(
regularRepository: $regularRepository, // EventSauce\EventSourcing\Snapshotting\AggregateRootRepositoryWithSnapshotting
conditionalSnapshotStrategy: new EveryNEventConditionalSnapshotStrategy(numberOfEvents: 200) # or your implementation
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.