Skip to content

Commit

Permalink
Merge pull request #5096 from neos/task/add-workspace-contentstream-m…
Browse files Browse the repository at this point in the history
…apping-to-contentgraph

!!! TASK: Add workspace content stream mapping to content graph projection
  • Loading branch information
kitsunet authored Oct 17, 2024
2 parents 3bfc572 + a18801b commit b4fd77b
Show file tree
Hide file tree
Showing 95 changed files with 1,416 additions and 2,518 deletions.
80 changes: 0 additions & 80 deletions Neos.ContentGraph.DoctrineDbalAdapter/src/ContentGraphFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public function referenceRelation(): string
return $this->tableNamePrefix . '_referencerelation';
}

public function workspace(): string
{
return $this->tableNamePrefix . '_workspace';
}

public function contentStream(): string
{
return $this->tableNamePrefix . '_contentstream';
}

public function checkpoint(): string
{
return $this->tableNamePrefix . '_checkpoint';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Neos.ContentGraph.DoctrineDbalAdapter package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

namespace Neos\ContentGraph\DoctrineDbalAdapter;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Neos\ContentGraph\DoctrineDbalAdapter\Domain\Repository\ContentGraph;
use Neos\ContentGraph\DoctrineDbalAdapter\Domain\Repository\NodeFactory;
use Neos\ContentRepository\Core\ContentRepositoryReadModelAdapterInterface;
use Neos\ContentRepository\Core\NodeType\NodeTypeManager;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStream;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreams;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamStatus;
use Neos\ContentRepository\Core\SharedModel\Workspace\Workspace;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\ContentRepository\Core\SharedModel\Workspace\Workspaces;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceStatus;
use Neos\EventStore\Model\Event\Version;

/**
* @internal only used inside the
* @see ContentRepositoryReadModel
*/
final readonly class ContentRepositoryReadModelAdapter implements ContentRepositoryReadModelAdapterInterface
{
public function __construct(
private Connection $dbal,
private NodeFactory $nodeFactory,
private ContentRepositoryId $contentRepositoryId,
private NodeTypeManager $nodeTypeManager,
private ContentGraphTableNames $tableNames
) {
}

public function buildContentGraph(WorkspaceName $workspaceName, ContentStreamId $contentStreamId): ContentGraph
{
return new ContentGraph($this->dbal, $this->nodeFactory, $this->contentRepositoryId, $this->nodeTypeManager, $this->tableNames, $workspaceName, $contentStreamId);
}

public function findWorkspaceByName(WorkspaceName $workspaceName): ?Workspace
{
$workspaceByNameStatement = <<<SQL
SELECT
name, baseWorkspaceName, currentContentStreamId, status
FROM
{$this->tableNames->workspace()}
WHERE
name = :workspaceName
LIMIT 1
SQL;
try {
$row = $this->dbal->fetchAssociative($workspaceByNameStatement, [
'workspaceName' => $workspaceName->value,
]);
} catch (Exception $e) {
throw new \RuntimeException(sprintf('Failed to load workspace from database: %s', $e->getMessage()), 1716486077, $e);
}
if ($row === false) {
return null;
}
return self::workspaceFromDatabaseRow($row);
}

public function findWorkspaces(): Workspaces
{
$workspacesStatement = <<<SQL
SELECT
name, baseWorkspaceName, currentContentStreamId, status
FROM
{$this->tableNames->workspace()}
SQL;
try {
$rows = $this->dbal->fetchAllAssociative($workspacesStatement);
} catch (Exception $e) {
throw new \RuntimeException(sprintf('Failed to load workspaces from database: %s', $e->getMessage()), 1716902981, $e);
}
return Workspaces::fromArray(array_map(self::workspaceFromDatabaseRow(...), $rows));
}

public function findContentStreamById(ContentStreamId $contentStreamId): ?ContentStream
{
$contentStreamByIdStatement = <<<SQL
SELECT
id, sourceContentStreamId, status, version, removed
FROM
{$this->tableNames->contentStream()}
WHERE
id = :contentStreamId
LIMIT 1
SQL;
try {
$row = $this->dbal->fetchAssociative($contentStreamByIdStatement, [
'contentStreamId' => $contentStreamId->value,
]);
} catch (Exception $e) {
throw new \RuntimeException(sprintf('Failed to load content stream from database: %s', $e->getMessage()), 1716903166, $e);
}
if ($row === false) {
return null;
}
return self::contentStreamFromDatabaseRow($row);
}

public function findContentStreams(): ContentStreams
{
$contentStreamsStatement = <<<SQL
SELECT
id, sourceContentStreamId, status, version, removed
FROM
{$this->tableNames->contentStream()}
SQL;
try {
$rows = $this->dbal->fetchAllAssociative($contentStreamsStatement);
} catch (Exception $e) {
throw new \RuntimeException(sprintf('Failed to load content streams from database: %s', $e->getMessage()), 1716903042, $e);
}
return ContentStreams::fromArray(array_map(self::contentStreamFromDatabaseRow(...), $rows));
}

/**
* @param array<string, mixed> $row
*/
private static function workspaceFromDatabaseRow(array $row): Workspace
{
return new Workspace(
WorkspaceName::fromString($row['name']),
isset($row['baseWorkspaceName']) ? WorkspaceName::fromString($row['baseWorkspaceName']) : null,
ContentStreamId::fromString($row['currentContentStreamId']),
WorkspaceStatus::from($row['status']),
);
}

/**
* @param array<string, mixed> $row
*/
private static function contentStreamFromDatabaseRow(array $row): ContentStream
{
return new ContentStream(
ContentStreamId::fromString($row['id']),
isset($row['sourceContentStreamId']) ? ContentStreamId::fromString($row['sourceContentStreamId']) : null,
ContentStreamStatus::from($row['status']),
Version::fromInteger((int)$row['version']),
(bool)$row['removed']
);
}
}
Loading

0 comments on commit b4fd77b

Please sign in to comment.