Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/backward-compatibility-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v5
uses: actions/checkout@v6
with:
fetch-depth: 0

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
services:
postgres:
# Docker Hub image
image: "postgres:17.6"
image: "postgres:17.7"
# Provide the password for postgres
env:
POSTGRES_PASSWORD: postgres
Expand Down Expand Up @@ -46,7 +46,7 @@ jobs:
extensions: pdo_sqlite

- name: "Checkout base"
uses: actions/checkout@v5
uses: actions/checkout@v6
with:
ref: ${{ github.base_ref }}

Expand All @@ -58,7 +58,7 @@ jobs:
run: "vendor/bin/phpbench run tests/Benchmark --progress=none --report=default --tag=base"

- name: "Checkout"
uses: actions/checkout@v5
uses: actions/checkout@v6
with:
clean: false

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coding-standard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v5
uses: actions/checkout@v6

- name: "Install PHP"
uses: "shivammathur/setup-php@2.35.5"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deptrac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v5
uses: actions/checkout@v6

- name: "Install PHP"
uses: "shivammathur/setup-php@2.35.5"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs-build-try.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: Deploy docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: actions/checkout@v6
with:
fetch-depth: 0

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: Deploy docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: actions/checkout@v6
with:
fetch-depth: 0

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v5
uses: actions/checkout@v6

- name: "Install PHP"
uses: "shivammathur/setup-php@2.35.5"
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v5
uses: actions/checkout@v6

- name: "Install PHP"
uses: "shivammathur/setup-php@2.35.5"
Expand Down Expand Up @@ -104,7 +104,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v5
uses: actions/checkout@v6

- name: "Install PHP"
uses: "shivammathur/setup-php@2.35.5"
Expand Down Expand Up @@ -159,7 +159,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v5
uses: actions/checkout@v6

- name: "Install PHP"
uses: "shivammathur/setup-php@2.35.5"
Expand Down Expand Up @@ -193,7 +193,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v5
uses: actions/checkout@v6

- name: "Install PHP"
uses: "shivammathur/setup-php@2.35.5"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mutation-tests-diff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v5
uses: actions/checkout@v6
with:
fetch-depth: 0

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mutation-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v5
uses: actions/checkout@v6

- name: "Install PHP"
uses: "shivammathur/setup-php@2.35.5"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v5
uses: actions/checkout@v6

- name: "Install PHP"
uses: "shivammathur/setup-php@2.35.5"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v5
uses: actions/checkout@v6

- name: "Install PHP"
uses: "shivammathur/setup-php@2.35.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v5
uses: actions/checkout@v6

- name: "Release"
uses: "laminas/automatic-releases@v1"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v5
uses: actions/checkout@v6

- name: "Install PHP"
uses: "shivammathur/setup-php@2.35.5"
Expand Down
5 changes: 0 additions & 5 deletions baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,6 @@
<code><![CDATA[$streamName]]></code>
</PropertyTypeCoercion>
</file>
<file src="src/Store/InMemoryStore.php">
<MixedPropertyTypeCoercion>
<code><![CDATA[$this->messages]]></code>
</MixedPropertyTypeCoercion>
</file>
<file src="src/Store/StreamDoctrineDbalStore.php">
<DeprecatedMethod>
<code><![CDATA[setPrimaryKey]]></code>
Expand Down
3 changes: 2 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ deptrac:
- Identifier
- MetadataAggregate
Store:
- Aggregate
- Attribute
- Clock
- Message
- Metadata
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/aggregate.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ final class Hotel extends BasicAggregateRoot
throw new NoPlaceException($name);
}

$this->recordThat(new RoomBocked($name));
$this->recordThat(new RoomBooked($name));

if ($this->people !== self::SIZE) {
return;
Expand All @@ -571,7 +571,7 @@ final class Hotel extends BasicAggregateRoot
}

#[Apply]
protected function applyRoomBocked(RoomBocked $event): void
protected function applyRoomBooked(RoomBooked $event): void
{
$this->people++;
}
Expand Down
6 changes: 3 additions & 3 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
mkdocs==1.6.1
mike==2.1.3
markdown==3.9
mkdocs-material==9.6.20
markdown==3.10
mkdocs-material==9.7.0

# Markdown extensions
Pygments==2.19.2
pymdown-extensions==10.16.1
pymdown-extensions==10.17.1

# MkDocs plugins
mkdocs-material-extensions==1.3.1
5 changes: 1 addition & 4 deletions src/Store/Header/IndexHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace Patchlevel\EventSourcing\Store\Header;

/**
* @psalm-immutable
* @experimental
*/
/** @psalm-immutable */
final class IndexHeader
{
/** @param positive-int $index */
Expand Down
86 changes: 79 additions & 7 deletions src/Store/InMemoryStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,28 @@
namespace Patchlevel\EventSourcing\Store;

use Closure;
use Patchlevel\EventSourcing\Clock\SystemClock;
use Patchlevel\EventSourcing\Message\HeaderNotFound;
use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Metadata\Event\EventRegistry;
use Patchlevel\EventSourcing\Store\Criteria\ArchivedCriterion;
use Patchlevel\EventSourcing\Store\Criteria\Criteria;
use Patchlevel\EventSourcing\Store\Criteria\EventsCriterion;
use Patchlevel\EventSourcing\Store\Criteria\FromIndexCriterion;
use Patchlevel\EventSourcing\Store\Criteria\FromPlayheadCriterion;
use Patchlevel\EventSourcing\Store\Criteria\StreamCriterion;
use Patchlevel\EventSourcing\Store\Criteria\ToIndexCriterion;
use Patchlevel\EventSourcing\Store\Header\EventIdHeader;
use Patchlevel\EventSourcing\Store\Header\IndexHeader;
use Patchlevel\EventSourcing\Store\Header\PlayheadHeader;
use Patchlevel\EventSourcing\Store\Header\RecordedOnHeader;
use Patchlevel\EventSourcing\Store\Header\StreamNameHeader;
use Psr\Clock\ClockInterface;
use Ramsey\Uuid\Uuid;
use Throwable;

use function array_filter;
use function array_map;
use function array_push;
use function array_reverse;
use function array_slice;
use function array_unique;
Expand All @@ -32,10 +41,16 @@

final class InMemoryStore implements Store
{
/** @param array<positive-int|0, Message> $messages */
/** @var array<0|positive-int, Message> */
private array $messages = [];

/** @param list<Message> $messages */
public function __construct(
private array $messages = [],
array $messages = [],
private readonly EventRegistry|null $eventRegistry = null,
private readonly ClockInterface $clock = new SystemClock(),
) {
$this->save(...$messages);
}

public function load(
Expand Down Expand Up @@ -68,7 +83,27 @@ public function count(Criteria|null $criteria = null): int

public function save(Message ...$messages): void
{
array_push($this->messages, ...$messages);
$this->transactional(function () use ($messages): void {
$count = count($this->messages);

foreach ($messages as $message) {
$count++;

if (!$message->hasHeader(IndexHeader::class)) {
$message = $message->withHeader(new IndexHeader($count));
}

if (!$message->hasHeader(EventIdHeader::class)) {
$message = $message->withHeader(new EventIdHeader(Uuid::uuid7()->toString()));
}

if (!$message->hasHeader(RecordedOnHeader::class)) {
$message = $message->withHeader(new RecordedOnHeader($this->clock->now()));
}

$this->messages[] = $message;
}
});
}

/**
Expand All @@ -78,7 +113,14 @@ public function save(Message ...$messages): void
*/
public function transactional(Closure $function): void
{
$function();
$messages = $this->messages;
try {
$function();
} catch (Throwable $e) {
$this->messages = $messages;

throw $e;
}
}

/** @return list<string> */
Expand Down Expand Up @@ -127,9 +169,11 @@ private function filter(Criteria|null $criteria): array
return $this->messages;
}

$eventRegistry = $this->eventRegistry;

return array_filter(
$this->messages,
static function (Message $message, int $index) use ($criteria): bool {
static function (Message $message) use ($criteria, $eventRegistry): bool {
foreach ($criteria->all() as $criterion) {
switch ($criterion::class) {
case StreamCriterion::class:
Expand Down Expand Up @@ -187,7 +231,35 @@ static function (Message $message, int $index) use ($criteria): bool {

break;
case FromIndexCriterion::class:
if ($index < $criterion->fromIndex) {
try {
$index = $message->header(IndexHeader::class)->index;
} catch (HeaderNotFound) {
return false;
}

if ($index <= $criterion->fromIndex) {
return false;
}

break;
case ToIndexCriterion::class:
try {
$index = $message->header(IndexHeader::class)->index;
} catch (HeaderNotFound) {
return false;
}

if ($index >= $criterion->toIndex) {
return false;
}

break;
case EventsCriterion::class:
if ($eventRegistry === null) {
throw new MissingEventRegistry($criterion::class);
}

if (!in_array($eventRegistry->eventName($message->event()::class), $criterion->events)) {
return false;
}

Expand Down
Loading
Loading