Skip to content

Commit

Permalink
Merge pull request #5332 from mhsdesign/feature/workspace-has-changes…
Browse files Browse the repository at this point in the history
…-flag

FEATURE: Introduce `Workspace::hasPublishableChanges`
  • Loading branch information
kitsunet authored Nov 1, 2024
2 parents 6a5e797 + c08c3d0 commit 6f2890e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 54 deletions.
3 changes: 2 additions & 1 deletion Classes/Feature/Common/PublishableToWorkspaceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Neos\ContentRepository\Core\Feature\Common;

use Neos\ContentRepository\Core\EventStore\EventInterface;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;

Expand All @@ -26,7 +27,7 @@
*
* @internal used internally for the publishing mechanism of workspaces
*/
interface PublishableToWorkspaceInterface
interface PublishableToWorkspaceInterface extends EventInterface
{
public function withWorkspaceNameAndContentStreamId(WorkspaceName $targetWorkspaceName, ContentStreamId $contentStreamId): self;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
*/

use Neos\ContentRepository\Core\EventStore\EventInterface;
use Neos\ContentRepository\Core\Feature\Common\EmbedsContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\EventStore\Model\Event\Version;

/**
* @api events are the persistence-API of the content repository
*/
final readonly class ContentStreamWasForked implements EventInterface
final readonly class ContentStreamWasForked implements EventInterface, EmbedsContentStreamId
{
public function __construct(
/**
Expand All @@ -33,6 +34,11 @@ public function __construct(
) {
}

public function getContentStreamId(): ContentStreamId
{
return $this->newContentStreamId;
}

public static function fromArray(array $values): self
{
return new self(
Expand Down
74 changes: 23 additions & 51 deletions Classes/Feature/WorkspaceCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ private function handlePublishWorkspace(
): \Generator {
$workspace = $this->requireWorkspace($command->workspaceName, $commandHandlingDependencies);
$baseWorkspace = $this->requireBaseWorkspace($workspace, $commandHandlingDependencies);
if (!$workspace->hasPublishableChanges()) {
// no-op
return;
}

if (!$commandHandlingDependencies->contentStreamExists($workspace->currentContentStreamId)) {
throw new \RuntimeException('Cannot publish nodes on a workspace with a stateless content stream', 1729711258);
}
Expand All @@ -197,15 +202,6 @@ private function handlePublishWorkspace(
)
);

if ($rebaseableCommands->isEmpty()) {
// we have no changes, we just reopen; partial no-op
yield $this->reopenContentStream(
$workspace->currentContentStreamId,
$commandHandlingDependencies
);
return;
}

try {
yield from $this->publishWorkspace(
$workspace,
Expand Down Expand Up @@ -319,7 +315,6 @@ private function getCopiedEventsOfEventStream(
$event = $this->eventNormalizer->denormalize($eventEnvelope->event);

if ($event instanceof PublishableToWorkspaceInterface) {
/** @var EventInterface $copiedEvent */
$copiedEvent = $event->withWorkspaceNameAndContentStreamId($targetWorkspaceName, $targetContentStreamId);
// We need to add the event metadata here for rebasing in nested workspace situations
// (and for exporting)
Expand Down Expand Up @@ -358,14 +353,7 @@ private function handleRebaseWorkspace(
$commandHandlingDependencies
);

$rebaseableCommands = RebaseableCommands::extractFromEventStream(
$this->eventStore->load(
ContentStreamEventStreamName::fromContentStreamId($workspace->currentContentStreamId)
->getEventStreamName()
)
);

if ($rebaseableCommands->isEmpty()) {
if (!$workspace->hasPublishableChanges()) {
// if we have no changes in the workspace we can fork from the base directly
yield from $this->rebaseWorkspaceWithoutChanges(
$workspace,
Expand All @@ -376,6 +364,13 @@ private function handleRebaseWorkspace(
return;
}

$rebaseableCommands = RebaseableCommands::extractFromEventStream(
$this->eventStore->load(
ContentStreamEventStreamName::fromContentStreamId($workspace->currentContentStreamId)
->getEventStreamName()
)
);

$commandSimulator = $this->commandSimulatorFactory->createSimulatorForWorkspace($baseWorkspace->workspaceName);

$commandSimulator->run(
Expand Down Expand Up @@ -438,16 +433,17 @@ private function handlePublishIndividualNodesFromWorkspace(
PublishIndividualNodesFromWorkspace $command,
CommandHandlingDependencies $commandHandlingDependencies,
): \Generator {
if ($command->nodesToPublish->isEmpty()) {
$workspace = $this->requireWorkspace($command->workspaceName, $commandHandlingDependencies);
$baseWorkspace = $this->requireBaseWorkspace($workspace, $commandHandlingDependencies);
if ($command->nodesToPublish->isEmpty() || !$workspace->hasPublishableChanges()) {
// noop
return;
}

$workspace = $this->requireWorkspace($command->workspaceName, $commandHandlingDependencies);
// todo check that fetching workspace throws if there is no content stream id for it
if (!$commandHandlingDependencies->contentStreamExists($workspace->currentContentStreamId)) {
throw new \RuntimeException('Cannot publish nodes on a workspace with a stateless content stream', 1710410114);
}
$baseWorkspace = $this->requireBaseWorkspace($workspace, $commandHandlingDependencies);
$this->requireContentStreamToNotBeClosed($baseWorkspace->currentContentStreamId, $commandHandlingDependencies);
$baseContentStreamVersion = $commandHandlingDependencies->getContentStreamVersion($baseWorkspace->currentContentStreamId);

Expand Down Expand Up @@ -572,16 +568,17 @@ private function handleDiscardIndividualNodesFromWorkspace(
DiscardIndividualNodesFromWorkspace $command,
CommandHandlingDependencies $commandHandlingDependencies,
): \Generator {
if ($command->nodesToDiscard->isEmpty()) {
$workspace = $this->requireWorkspace($command->workspaceName, $commandHandlingDependencies);
$baseWorkspace = $this->requireBaseWorkspace($workspace, $commandHandlingDependencies);

if ($command->nodesToDiscard->isEmpty() || !$workspace->hasPublishableChanges()) {
// noop
return;
}

$workspace = $this->requireWorkspace($command->workspaceName, $commandHandlingDependencies);
if (!$commandHandlingDependencies->contentStreamExists($workspace->currentContentStreamId)) {
throw new \RuntimeException('Cannot discard nodes on a workspace with a stateless content stream', 1710408112);
}
$baseWorkspace = $this->requireBaseWorkspace($workspace, $commandHandlingDependencies);

yield $this->closeContentStream(
$workspace->currentContentStreamId,
Expand Down Expand Up @@ -673,7 +670,7 @@ private function handleDiscardWorkspace(
$workspace = $this->requireWorkspace($command->workspaceName, $commandHandlingDependencies);
$baseWorkspace = $this->requireBaseWorkspace($workspace, $commandHandlingDependencies);

if (!$this->hasEventsInContentStreamExceptForking(ContentStreamEventStreamName::fromContentStreamId($workspace->currentContentStreamId))) {
if (!$workspace->hasPublishableChanges()) {
return;
}

Expand Down Expand Up @@ -885,33 +882,8 @@ private function requireNonCircularRelationBetweenWorkspaces(Workspace $workspac
*/
private function requireEmptyWorkspace(Workspace $workspace): void
{
$workspaceContentStreamName = ContentStreamEventStreamName::fromContentStreamId(
$workspace->currentContentStreamId
);
if ($this->hasEventsInContentStreamExceptForking($workspaceContentStreamName)) {
if ($workspace->hasPublishableChanges()) {
throw new WorkspaceIsNotEmptyException('The user workspace needs to be empty before switching the base workspace.', 1681455989);
}
}

/**
* @return bool
*/
private function hasEventsInContentStreamExceptForking(
ContentStreamEventStreamName $workspaceContentStreamName,
): bool {
// todo introduce workspace has changes instead
$workspaceContentStream = $this->eventStore->load($workspaceContentStreamName->getEventStreamName());

$fullQualifiedEventClassName = ContentStreamWasForked::class;
$shortEventClassName = substr($fullQualifiedEventClassName, strrpos($fullQualifiedEventClassName, '\\') + 1);

foreach ($workspaceContentStream as $eventEnvelope) {
if ($eventEnvelope->event->type->value === EventType::fromString($shortEventClassName)->value) {
continue;
}
return true;
}

return false;
}
}
15 changes: 14 additions & 1 deletion Classes/SharedModel/Workspace/Workspace.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ private function __construct(
public ?WorkspaceName $baseWorkspaceName,
public ContentStreamId $currentContentStreamId,
public WorkspaceStatus $status,
private bool $hasPublishableChanges
) {
if ($this->isRootWorkspace() && $this->hasPublishableChanges) {
throw new \InvalidArgumentException('Root workspaces cannot have changes', 1730371566);
}
}

/**
Expand All @@ -43,8 +47,17 @@ public static function create(
?WorkspaceName $baseWorkspaceName,
ContentStreamId $currentContentStreamId,
WorkspaceStatus $status,
bool $hasPublishableChanges
): self {
return new self($workspaceName, $baseWorkspaceName, $currentContentStreamId, $status);
return new self($workspaceName, $baseWorkspaceName, $currentContentStreamId, $status, $hasPublishableChanges);
}

/**
* Indicates if the workspace contains changed to be published
*/
public function hasPublishableChanges(): bool
{
return $this->hasPublishableChanges;
}

/**
Expand Down

0 comments on commit 6f2890e

Please sign in to comment.