From 0266e11a4ae660b059b9d8ee3722658259489632 Mon Sep 17 00:00:00 2001 From: mhsdesign <85400359+mhsdesign@users.noreply.github.com> Date: Thu, 14 Mar 2024 15:29:33 +0100 Subject: [PATCH] TASK: Make `ContentStreamIdOverride` code flow more obvious --- .../Feature/Common/ConstraintChecks.php | 7 ++-- .../Common/ContentStreamIdOverride.php | 35 +++++++++++++++++-- .../Feature/WorkspaceCommandHandler.php | 23 +++--------- 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/Neos.ContentRepository.Core/Classes/Feature/Common/ConstraintChecks.php b/Neos.ContentRepository.Core/Classes/Feature/Common/ConstraintChecks.php index d1b8c877fe7..831aada832e 100644 --- a/Neos.ContentRepository.Core/Classes/Feature/Common/ConstraintChecks.php +++ b/Neos.ContentRepository.Core/Classes/Feature/Common/ConstraintChecks.php @@ -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 ); } diff --git a/Neos.ContentRepository.Core/Classes/Feature/Common/ContentStreamIdOverride.php b/Neos.ContentRepository.Core/Classes/Feature/Common/ContentStreamIdOverride.php index 434ca8e2c63..3944bb1f06e 100644 --- a/Neos.ContentRepository.Core/Classes/Feature/Common/ContentStreamIdOverride.php +++ b/Neos.ContentRepository.Core/Classes/Feature/Common/ContentStreamIdOverride.php @@ -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 @@ -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; } } diff --git a/Neos.ContentRepository.Core/Classes/Feature/WorkspaceCommandHandler.php b/Neos.ContentRepository.Core/Classes/Feature/WorkspaceCommandHandler.php index f631052e700..8d1d649a78a 100644 --- a/Neos.ContentRepository.Core/Classes/Feature/WorkspaceCommandHandler.php +++ b/Neos.ContentRepository.Core/Classes/Feature/WorkspaceCommandHandler.php @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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); - } }