Skip to content

Commit

Permalink
TASK: Fixup after merge of asset usage refactoring
Browse files Browse the repository at this point in the history
Initially the exporter got passed the `WorkspaceFinder` and had to fetch the live workspace themselves to determine the content stream id.

Temporarily we then passed the $targetContentStreamId, but some places now need the workspace name as well. Thus we pass the whole $targetWorkspace.

Also a utility `getDependantWorkspaces` was introduced to simplify the `filter` usage from the caller.
  • Loading branch information
mhsdesign committed Oct 10, 2024
1 parent 4ea3bbe commit db7861f
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ public function getBaseWorkspaces(WorkspaceName $workspaceName): Workspaces
return self::fromArray($baseWorkspaces);
}

/**
* Get all dependent workspaces (if they are included in this result set).
*/
public function getDependantWorkspaces(WorkspaceName $workspaceName): Workspaces
{
return $this->filter(
static fn (Workspace $potentiallyDependentWorkspace) => $potentiallyDependentWorkspace->baseWorkspaceName?->equals($workspaceName) ?? false
);
}

/**
* @return \Traversable<Workspace>
*/
Expand Down Expand Up @@ -116,6 +126,16 @@ public function find(\Closure $callback): ?Workspace
return null;
}

/**
* @template T
* @param \Closure(Workspace): T $callback
* @return list<T>
*/
public function map(\Closure $callback): array
{
return array_map($callback, array_values($this->workspaces));
}

public function count(): int
{
return count($this->workspaces);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function __construct(private readonly Filesystem $filesystem)
public function build(ContentRepositoryServiceFactoryDependencies $serviceFactoryDependencies): EventExportProcessor {
return new EventExportProcessor(
$this->filesystem,
$serviceFactoryDependencies->contentRepository->findWorkspaceByName(WorkspaceName::forLive())->currentContentStreamId,
$serviceFactoryDependencies->contentRepository->findWorkspaceByName(WorkspaceName::forLive()),
$serviceFactoryDependencies->eventStore
);
}
Expand Down
8 changes: 4 additions & 4 deletions Neos.ContentRepository.Export/src/ExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

use League\Flysystem\Filesystem;
use Neos\ContentRepository\Core\Factory\ContentRepositoryServiceInterface;
use Neos\ContentRepository\Core\Projection\Workspace\WorkspaceFinder;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\Workspace\Workspace;
use Neos\ContentRepository\Export\Processors\AssetExportProcessor;
use Neos\ContentRepository\Export\Processors\EventExportProcessor;
use Neos\EventStore\EventStoreInterface;
Expand All @@ -22,7 +22,7 @@ class ExportService implements ContentRepositoryServiceInterface
public function __construct(
private readonly ContentRepositoryId $contentRepositoryId,
private readonly Filesystem $filesystem,
private readonly WorkspaceFinder $workspaceFinder,
private readonly Workspace $targetWorkspace,
private readonly AssetRepository $assetRepository,
private readonly AssetUsageService $assetUsageService,
private readonly EventStoreInterface $eventStore,
Expand All @@ -35,14 +35,14 @@ public function runAllProcessors(\Closure $outputLineFn, bool $verbose = false):
$processors = [
'Exporting events' => new EventExportProcessor(
$this->filesystem,
$this->workspaceFinder,
$this->targetWorkspace,
$this->eventStore
),
'Exporting assets' => new AssetExportProcessor(
$this->contentRepositoryId,
$this->filesystem,
$this->assetRepository,
$this->workspaceFinder,
$this->targetWorkspace,
$this->assetUsageService
)
];
Expand Down
5 changes: 2 additions & 3 deletions Neos.ContentRepository.Export/src/ExportServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Neos\ContentRepository\Core\Factory\ContentRepositoryServiceFactoryDependencies;
use Neos\ContentRepository\Core\Factory\ContentRepositoryServiceFactoryInterface;
use Neos\ContentRepository\Core\SharedModel\Workspace\Workspace;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\Media\Domain\Repository\AssetRepository;
use Neos\Neos\AssetUsage\AssetUsageService;

Expand All @@ -20,7 +19,7 @@ class ExportServiceFactory implements ContentRepositoryServiceFactoryInterface

public function __construct(
private readonly Filesystem $filesystem,
private readonly ContentStreamId $targetContentStreamId,
private readonly Workspace $targetWorkspace,
private readonly AssetRepository $assetRepository,
private readonly AssetUsageService $assetUsageService,
) {
Expand All @@ -31,7 +30,7 @@ public function build(ContentRepositoryServiceFactoryDependencies $serviceFactor
return new ExportService(
$serviceFactoryDependencies->contentRepositoryId,
$this->filesystem,
$this->targetContentStreamId,
$this->targetWorkspace,
$this->assetRepository,
$this->assetUsageService,
$serviceFactoryDependencies->eventStore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace Neos\ContentRepository\Export\Processors;

use League\Flysystem\Filesystem;
use Neos\ContentRepository\Core\Projection\Workspace\WorkspaceFinder;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\ContentRepository\Core\SharedModel\Workspace\Workspace;
use Neos\ContentRepository\Export\Asset\ValueObject\SerializedAsset;
use Neos\ContentRepository\Export\Asset\ValueObject\SerializedImageVariant;
use Neos\ContentRepository\Export\ProcessorInterface;
Expand Down Expand Up @@ -33,7 +32,7 @@ public function __construct(
private readonly ContentRepositoryId $contentRepositoryId,
private readonly Filesystem $files,
private readonly AssetRepository $assetRepository,
private readonly WorkspaceFinder $workspaceFinder,
private readonly Workspace $targetWorkspace,
private readonly AssetUsageService $assetUsageService,
) {}

Expand All @@ -45,11 +44,7 @@ public function onMessage(\Closure $callback): void

public function run(): ProcessorResult
{
$liveWorkspace = $this->workspaceFinder->findOneByName(WorkspaceName::forLive());
if ($liveWorkspace === null) {
return ProcessorResult::error('Failed to find live workspace');
}
$assetFilter = AssetUsageFilter::create()->withWorkspaceName($liveWorkspace->workspaceName)->groupByAsset();
$assetFilter = AssetUsageFilter::create()->withWorkspaceName($this->targetWorkspace->workspaceName)->groupByAsset();

$numberOfExportedAssets = 0;
$numberOfExportedImageVariants = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use League\Flysystem\FilesystemException;
use Neos\ContentRepository\Core\Factory\ContentRepositoryServiceInterface;
use Neos\ContentRepository\Core\Feature\ContentStreamEventStreamName;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\Workspace;
use Neos\ContentRepository\Export\Event\ValueObject\ExportedEvent;
use Neos\ContentRepository\Export\ProcessorInterface;
use Neos\ContentRepository\Export\ProcessorResult;
Expand All @@ -23,7 +23,7 @@ final class EventExportProcessor implements ProcessorInterface, ContentRepositor

public function __construct(
private readonly Filesystem $files,
private readonly ContentStreamId $targetContentStreamId,
private readonly Workspace $targetWorkspace,
private readonly EventStoreInterface $eventStore,
) {
}
Expand All @@ -35,7 +35,7 @@ public function onMessage(\Closure $callback): void

public function run(): ProcessorResult
{
$streamName = ContentStreamEventStreamName::fromContentStreamId($this->targetContentStreamId)->getEventStreamName();
$streamName = ContentStreamEventStreamName::fromContentStreamId($this->targetWorkspace->currentContentStreamId)->getEventStreamName();
$eventStream = $this->eventStore->load($streamName);

$eventFileResource = fopen('php://temp/maxmemory:5242880', 'rb+');
Expand Down
7 changes: 3 additions & 4 deletions Neos.Neos/Classes/AssetUsage/AssetUsageIndexingProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindChildNodesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
use Neos\ContentRepository\Core\SharedModel\Exception\NodeAggregateCurrentlyDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Exception\WorkspaceDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\Neos\AssetUsage\Service\AssetUsageIndexingService;
Expand All @@ -28,8 +27,8 @@ public function buildIndex(ContentRepository $contentRepository, NodeTypeName $n
{
$variationGraph = $contentRepository->getVariationGraph();

$workspaceFinder = $contentRepository->getWorkspaceFinder();
$liveWorkspace = $workspaceFinder->findOneByName(WorkspaceName::forLive());
$allWorkspaces = $contentRepository->getWorkspaces();
$liveWorkspace = $contentRepository->findWorkspaceByName(WorkspaceName::forLive());
if ($liveWorkspace === null) {
throw WorkspaceDoesNotExist::butWasSupposedTo(WorkspaceName::forLive());
}
Expand Down Expand Up @@ -73,7 +72,7 @@ public function buildIndex(ContentRepository $contentRepository, NodeTypeName $n
}
}

array_push($workspaces, ...array_values($workspaceFinder->findByBaseWorkspace($workspace->workspaceName)));
array_push($workspaces, ...iterator_to_array($allWorkspaces->getDependantWorkspaces($workspace->workspaceName)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use Neos\ContentRepository\Core\NodeType\NodeType;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\Projection\ContentGraph\PropertyCollection;
use Neos\ContentRepository\Core\Projection\Workspace\Workspace;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\Exception\WorkspaceDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Workspace\Workspace;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Persistence\Doctrine\PersistenceManager;
Expand Down Expand Up @@ -201,8 +201,8 @@ public function pruneIndex(ContentRepositoryId $contentRepositoryId): void
private function getWorkspaceBasesAndWorkspace(ContentRepositoryId $contentRepositoryId, WorkspaceName $workspaceName): array
{
if (!isset($this->workspaceBases[$contentRepositoryId->value][$workspaceName->value])) {
$workspaceFinder = $this->contentRepositoryRegistry->get($contentRepositoryId)->getWorkspaceFinder();
$workspace = $workspaceFinder->findOneByName($workspaceName);
$contentRepository = $this->contentRepositoryRegistry->get($contentRepositoryId);
$workspace = $contentRepository->findWorkspaceByName($workspaceName);
if ($workspace === null) {
throw WorkspaceDoesNotExist::butWasSupposedTo($workspaceName);
}
Expand All @@ -214,7 +214,7 @@ private function getWorkspaceBasesAndWorkspace(ContentRepositoryId $contentRepos
while ($stack !== []) {
$workspace = array_shift($stack);
if ($workspace->baseWorkspaceName) {
$ancestor = $workspaceFinder->findOneByName($workspace->baseWorkspaceName);
$ancestor = $contentRepository->findWorkspaceByName($workspace->baseWorkspaceName);
if ($ancestor === null) {
throw WorkspaceDoesNotExist::butWasSupposedTo($workspace->baseWorkspaceName);
}
Expand All @@ -235,8 +235,8 @@ private function getWorkspaceBasesAndWorkspace(ContentRepositoryId $contentRepos
private function getWorkspaceDependents(ContentRepositoryId $contentRepositoryId, WorkspaceName $workspaceName): array
{
if (!isset($this->workspaceDependents[$contentRepositoryId->value][$workspaceName->value])) {
$workspaceFinder = $this->contentRepositoryRegistry->get($contentRepositoryId)->getWorkspaceFinder();
$workspace = $workspaceFinder->findOneByName($workspaceName);
$contentRepository = $this->contentRepositoryRegistry->get($contentRepositoryId);
$workspace = $contentRepository->findWorkspaceByName($workspaceName);
if ($workspace === null) {
throw WorkspaceDoesNotExist::butWasSupposedTo($workspaceName);
}
Expand All @@ -246,7 +246,7 @@ private function getWorkspaceDependents(ContentRepositoryId $contentRepositoryId
while ($stack !== []) {
/** @var Workspace $workspace */
$workspace = array_shift($stack);
$descendants = $workspaceFinder->findByBaseWorkspace($workspace->workspaceName);
$descendants = $contentRepository->getWorkspaces()->getDependantWorkspaces($workspace->workspaceName);
foreach ($descendants as $descendant) {
$collectedWorkspaceNames[] = $descendant->workspaceName;
$stack[] = $descendant;
Expand Down
17 changes: 8 additions & 9 deletions Neos.Neos/Classes/Command/CrCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
use Neos\ContentRepository\Core\Projection\CatchUpOptions;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\ContentRepository\Export\ExportService;
use Neos\ContentRepository\Export\ExportServiceFactory;
use Neos\ContentRepository\Export\ImportService;
use Neos\ContentRepository\Export\ImportServiceFactory;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\ContentRepositoryRegistry\Service\ProjectionReplayServiceFactory;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Flow\ResourceManagement\ResourceManager;
Expand All @@ -26,12 +26,6 @@

class CrCommandController extends CommandController
{
/**
* @var array<string|int,mixed>
*/
#[Flow\InjectConfiguration(package: 'Neos.Flow')]
protected array $flowSettings;

public function __construct(
private readonly AssetRepository $assetRepository,
private readonly ResourceRepository $resourceRepository,
Expand All @@ -55,16 +49,21 @@ public function __construct(
public function exportCommand(string $path, string $contentRepository = 'default', bool $verbose = false): void
{
$contentRepositoryId = ContentRepositoryId::fromString($contentRepository);
$contentRepository = $this->contentRepositoryRegistry->get($contentRepositoryId);
$contentRepositoryInstance = $this->contentRepositoryRegistry->get($contentRepositoryId);

Files::createDirectoryRecursively($path);
$filesystem = new Filesystem(new LocalFilesystemAdapter($path));

$liveWorkspace = $contentRepositoryInstance->findWorkspaceByName(WorkspaceName::forLive());
if ($liveWorkspace === null) {
throw new \RuntimeException('Failed to find live workspace', 1716652280);
}

$exportService = $this->contentRepositoryRegistry->buildService(
$contentRepositoryId,
new ExportServiceFactory(
$filesystem,
$contentRepository->getWorkspaceFinder(),
$liveWorkspace,
$this->assetRepository,
$this->assetUsageService,
)
Expand Down
4 changes: 1 addition & 3 deletions Neos.Neos/Classes/Command/WorkspaceCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,7 @@ public function deleteCommand(string $workspace, bool $force = false, string $co
$this->quit(2);
}

$dependentWorkspaces = $contentRepositoryInstance->getWorkspaces()->filter(
static fn (Workspace $potentiallyDependentWorkspace) => $potentiallyDependentWorkspace->baseWorkspaceName?->equals($workspaceName) ?? false
);
$dependentWorkspaces = $contentRepositoryInstance->getWorkspaces()->getDependantWorkspaces($workspaceName);
if (!$dependentWorkspaces->isEmpty()) {
$this->outputLine('<error>Workspace "%s" cannot be deleted because the following workspaces are based on it:</error>', [$workspaceName->value]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateIds;
use Neos\ContentRepository\Core\SharedModel\Workspace\Workspace;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\ContentRepository\Core\SharedModel\Workspace\Workspaces;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Media\Domain\Model\AssetInterface;
Expand All @@ -19,7 +21,7 @@

class AssetChangeHandlerForCacheFlushing
{
/** @var array <string, array<string, WorkspaceName[]>> */
/** @var array<string, array<string, WorkspaceName[]>> */
private array $workspaceRuntimeCache = [];

public function __construct(
Expand Down Expand Up @@ -100,15 +102,15 @@ private function getWorkspaceNameAndChildWorkspaceNames(ContentRepository $conte
{
if (!isset($this->workspaceRuntimeCache[$contentRepository->id->value][$workspaceName->value])) {
$workspaceNames = [];
$workspace = $contentRepository->getWorkspaceFinder()->findOneByName($workspaceName);
$workspace = $contentRepository->findWorkspaceByName($workspaceName);
if ($workspace !== null) {
$stack[] = $workspace;

while ($stack !== []) {
$workspace = array_shift($stack);
$workspaceNames[] = $workspace->workspaceName;

$stack = array_merge($stack, array_values($contentRepository->getWorkspaceFinder()->findByBaseWorkspace($workspace->workspaceName)));
$stack = array_merge($stack, iterator_to_array($contentRepository->getWorkspaces()->getDependantWorkspaces($workspace->workspaceName)));
}
}
$this->workspaceRuntimeCache[$contentRepository->id->value][$workspaceName->value] = $workspaceNames;
Expand Down
Loading

0 comments on commit db7861f

Please sign in to comment.