Skip to content

Commit

Permalink
TASK: Make ContentStreamIdOverride code flow more obvious
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsdesign committed Mar 14, 2024
1 parent 328e1ad commit 0266e11
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ protected function requireContentStream(
WorkspaceName $workspaceName,
ContentRepository $contentRepository
): ContentStreamId {
$contentStreamId = ContentStreamIdOverride::$contentStreamIdToUse
?: $contentRepository->getWorkspaceFinder()->findOneByName($workspaceName)?->currentContentStreamId;
if (!$contentStreamId || !$contentRepository->getContentStreamFinder()->hasContentStream($contentStreamId)) {
$contentStreamId = ContentStreamIdOverride::findContentStreamIdForWorkspace($contentRepository, $workspaceName);
if (!$contentRepository->getContentStreamFinder()->hasContentStream($contentStreamId)) {
throw new ContentStreamDoesNotExistYet(
'Content stream "' . $contentStreamId?->value . '" does not exist yet.',
'Content stream "' . $contentStreamId->value . '" does not exist yet.',
1521386692
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

namespace Neos\ContentRepository\Core\Feature\Common;

use Neos\ContentRepository\Core\ContentRepository;
use Neos\ContentRepository\Core\SharedModel\Exception\ContentStreamDoesNotExistYet;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;

/**
* @internal (slightly hacky) implementation details for the workspace command handler
Expand All @@ -24,13 +27,41 @@ class ContentStreamIdOverride
/**
* A content stream id that to use instead of the workspace one's {@see ConstraintChecks::requireContentStream()}
*/
public static ?ContentStreamId $contentStreamIdToUse = null;
private static ?ContentStreamId $contentStreamIdToUse = null;

/**
* @internal
*/
public static function useContentStreamId(?ContentStreamId $contentStreamIdToUse): void
public static function withContentStreamIdToUse(ContentStreamId $contentStreamIdToUse, \Closure $fn): void
{
if (self::$contentStreamIdToUse !== null) {
throw new \Exception('Recursive content stream override is not supported', 1710426945);
}
self::$contentStreamIdToUse = $contentStreamIdToUse;
try {
$fn();
} catch (\Throwable $th) {
self::$contentStreamIdToUse = null;
throw $th;
}
self::$contentStreamIdToUse = null;
}

/**
* @internal
*/
public static function findContentStreamIdForWorkspace(ContentRepository $contentRepository, WorkspaceName $workspaceName): ContentStreamId
{
$contentStreamId = self::$contentStreamIdToUse
?: $contentRepository->getWorkspaceFinder()->findOneByName($workspaceName)?->currentContentStreamId;

if (!$contentStreamId) {
throw new ContentStreamDoesNotExistYet(
'Content stream for workspace "' . $workspaceName->value . '" does not exist yet.',
1710407870
);
}

return $contentStreamId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ private function handleRebaseWorkspace(
// - extract the commands from the to-be-rebased content stream; and applies them on the new content stream
$originalCommands = $this->extractCommandsFromContentStreamMetadata($workspaceContentStreamName);
$rebaseStatistics = new WorkspaceRebaseStatistics();
$this->withContentStreamIdToUse(
ContentStreamIdOverride::withContentStreamIdToUse(
$command->rebasedContentStreamId,
function () use ($originalCommands, $contentRepository, $rebaseStatistics, $workspaceContentStreamName, $baseWorkspace): void {
foreach ($originalCommands as $i => $originalCommand) {
Expand Down Expand Up @@ -531,7 +531,7 @@ private function handlePublishIndividualNodesFromWorkspace(

try {
// 4) using the new content stream, apply the matching commands
$this->withContentStreamIdToUse(
ContentStreamIdOverride::withContentStreamIdToUse(
$command->contentStreamIdForMatchingPart,
function () use ($matchingCommands, $contentRepository, $baseWorkspace, $command): void {
foreach ($matchingCommands as $matchingCommand) {
Expand Down Expand Up @@ -565,7 +565,7 @@ function () use ($matchingCommands, $contentRepository, $baseWorkspace, $command
)->block();

// 7) apply REMAINING commands to the workspace's new content stream
$this->withContentStreamIdToUse(
ContentStreamIdOverride::withContentStreamIdToUse(
$command->contentStreamIdForRemainingPart,
function () use ($contentRepository, $remainingCommands) {
foreach ($remainingCommands as $remainingCommand) {
Expand Down Expand Up @@ -668,7 +668,7 @@ private function handleDiscardIndividualNodesFromWorkspace(

// 4) using the new content stream, apply the commands to keep
try {
$this->withContentStreamIdToUse(
ContentStreamIdOverride::withContentStreamIdToUse(
$command->newContentStreamId,
function () use ($commandsToKeep, $contentRepository, $baseWorkspace, $command): void {
foreach ($commandsToKeep as $matchingCommand) {
Expand Down Expand Up @@ -990,19 +990,4 @@ private function hasEventsInContentStreamExceptForking(

return false;
}

private function withContentStreamIdToUse(ContentStreamId $contentStreamIdToUse, callable $function): void
{
if (ContentStreamIdOverride::$contentStreamIdToUse !== null) {
throw new \Exception('Recursive content stream override is not supported');
}
ContentStreamIdOverride::useContentStreamId($contentStreamIdToUse);
try {
$function();
} catch (\Exception $exception) {
ContentStreamIdOverride::useContentStreamId(null);
throw $exception;
}
ContentStreamIdOverride::useContentStreamId(null);
}
}

0 comments on commit 0266e11

Please sign in to comment.