Skip to content

Commit

Permalink
TASK: Add the processed events as argument to the CommandHookInterfac…
Browse files Browse the repository at this point in the history
…e->onAfterHandle()
  • Loading branch information
mficzel committed Feb 20, 2025
1 parent 048d5c2 commit 11dc3c4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Neos\ContentRepository\Core\CommandHandler;

use Neos\ContentRepository\Core\ContentRepository;
use Neos\ContentRepository\Core\EventStore\Events;

/**
* Contract for a hook that is invoked just before any command is processed via {@see ContentRepository::handle()}
Expand All @@ -25,7 +26,8 @@ public function onBeforeHandle(CommandInterface $command): CommandInterface;

/**
* @param CommandInterface $command The command that was just handled
* @param Events $events The events that resulted from the handled command
* @return Commands This hook must return Commands that will be handled after the incoming $command. The Commands can be empty.
*/
public function onAfterHandle(CommandInterface $command): Commands;
public function onAfterHandle(CommandInterface $command, Events $events): Commands;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Neos\ContentRepository\Core\CommandHandler;

use Neos\ContentRepository\Core\EventStore\Events;

/**
* Collection of {@see CommandHookInterface} instances, functioning as a delegating command hook implementation
*
Expand Down Expand Up @@ -54,11 +56,11 @@ public function onBeforeHandle(CommandInterface $command): CommandInterface
return $command;
}

public function onAfterHandle(CommandInterface $command): Commands
public function onAfterHandle(CommandInterface $command, Events $events): Commands
{
$commands = Commands::createEmpty();
foreach ($this->commandHooks as $commandHook) {
$commands = $commands->merge($commandHook->onAfterHandle($command));
$commands = $commands->merge($commandHook->onAfterHandle($command, $events));
}
return $commands;
}
Expand Down
6 changes: 4 additions & 2 deletions Neos.ContentRepository.Core/Classes/ContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,20 @@ public function handle(CommandInterface $command): void
if ($fullCatchUpResult->hadErrors()) {
throw CatchUpHadErrors::createFromErrors($fullCatchUpResult->errors);
}
$additionalCommands = $this->commandHook->onAfterHandle($command);
$additionalCommands = $this->commandHook->onAfterHandle($command, $toPublish->events);
foreach ($additionalCommands as $additionalCommand) {
$this->handle($additionalCommand);
}
return;
}

// control-flow aware command handling via generator
$events = DomainEvents::createEmpty();
try {
foreach ($toPublish as $eventsToPublish) {
try {
$this->eventStore->commit($eventsToPublish->streamName, $this->enrichAndNormalizeEvents($eventsToPublish->events, $correlationId), $eventsToPublish->expectedVersion);
$events = $events->withAppendedEvents($eventsToPublish->events);
} catch (ConcurrencyException $concurrencyException) {
// we pass the exception into the generator (->throw), so it could be try-caught and reacted upon:
//
Expand All @@ -141,7 +143,7 @@ public function handle(CommandInterface $command): void
if ($fullCatchUpResult->hadErrors()) {
throw CatchUpHadErrors::createFromErrors($fullCatchUpResult->errors);
}
$additionalCommands = $this->commandHook->onAfterHandle($command);
$additionalCommands = $this->commandHook->onAfterHandle($command, $events);
foreach ($additionalCommands as $additionalCommand) {
$this->handle($additionalCommand);
}
Expand Down
8 changes: 8 additions & 0 deletions Neos.ContentRepository.Core/Classes/EventStore/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public static function fromArray(array $events): self
return new self(...$events);
}

/**
* @return static
*/
public static function createEmpty(): self
{
return new self();
}

public function getIterator(): \Traversable
{
yield from $this->items;
Expand Down

0 comments on commit 11dc3c4

Please sign in to comment.