Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Feature copyTo #80

Draft
wants to merge 1 commit into
base: neos9
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions Classes/Domain/NodeCreation/NodeCreationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePoint;
use Neos\ContentRepository\Core\DimensionSpace\OriginDimensionSpacePoint;
use Neos\ContentRepository\Core\Feature\NodeCreation\Command\CreateNodeAggregateWithNode;
use Neos\ContentRepository\Core\Feature\NodeDuplication\Command\CopyNodesRecursively;
use Neos\ContentRepository\Core\Feature\NodeModification\Command\SetNodeProperties;
use Neos\ContentRepository\Core\Feature\NodeModification\Dto\PropertyValuesToWrite;
use Neos\ContentRepository\Core\Feature\NodeReferencing\Command\SetNodeReferences;
Expand Down Expand Up @@ -105,6 +106,27 @@ public function apply(RootTemplate $template, NodeCreationCommands $commands, No
);
}

private function copyTo(Template $template, TransientNode $node)
{
if (empty($template->getCopyFrom())) {
return;
}

$sources = is_array($template->getCopyFrom()) ? $template->getCopyFrom() : [$template->getCopyFrom()];

foreach ($sources as $sourceNode) {
yield CopyNodesRecursively::createFromSubgraphAndStartNode(
$node->subgraph,
$sourceNode,
$node->originDimensionSpacePoint,
$node->nodeAggregateId,
null,
null
);
}

}

private function applyTemplateRecursively(Templates $templates, TransientNode $parentNode, NodeCreationCommands $commands, ProcessingErrors $processingErrors): NodeCreationCommands
{
foreach ($templates as $template) {
Expand Down Expand Up @@ -138,7 +160,8 @@ private function applyTemplateRecursively(Templates $templates, TransientNode $p
$node->nodeAggregateId,
$parentNode->originDimensionSpacePoint,
$this->referencesProcessor->processAndValidateReferences($node, $processingErrors)
)
),
...iterator_to_array($this->copyTo($template, $node))
);

$commands = $this->applyTemplateRecursively(
Expand Down Expand Up @@ -214,7 +237,8 @@ private function applyTemplateRecursively(Templates $templates, TransientNode $p
$node->nodeAggregateId,
$parentNode->originDimensionSpacePoint,
$this->referencesProcessor->processAndValidateReferences($node, $processingErrors)
)
),
// todo ...iterator_to_array($this->copyTo($template, $node))
);


Expand Down
22 changes: 20 additions & 2 deletions Classes/Domain/Template/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ class Template implements \JsonSerializable

private Templates $childNodes;

private mixed $copyFrom;

/**
* @internal
* @param array<string, mixed> $properties
*/
public function __construct(?NodeTypeName $type, ?NodeName $name, array $properties, Templates $childNodes)
public function __construct(?NodeTypeName $type, ?NodeName $name, array $properties, Templates $childNodes, mixed $copyFrom)
{
$this->type = $type;
$this->name = $name;
$this->properties = $properties;
$this->childNodes = $childNodes;
$this->copyFrom = $copyFrom;
}

public function getType(): ?NodeTypeName
Expand All @@ -60,13 +63,28 @@ public function getChildNodes(): Templates
return $this->childNodes;
}

public function getCopyFrom(): mixed
{
return $this->copyFrom;
}

public function jsonSerialize(): mixed
{
if ($this->copyFrom) {
return [
'type' => $this->type,
'name' => $this->name,
'properties' => $this->properties,
'childNodes' => $this->childNodes,
'copyFrom' => $this->copyFrom,
];
}

return [
'type' => $this->type,
'name' => $this->name,
'properties' => $this->properties,
'childNodes' => $this->childNodes
'childNodes' => $this->childNodes,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,13 @@ private function createTemplateFromTemplatePart(TemplatePart $templatePart): Tem

$type = $templatePart->processConfiguration('type');
$name = $templatePart->processConfiguration('name');
$copyFrom = $templatePart->processConfiguration('copyFrom');
return new Template(
$type !== null ? NodeTypeName::fromString($type) : null,
$name !== null ? NodeName::transliterateFromString($name) : null,
$processedProperties,
$childNodeTemplates
$childNodeTemplates,
$copyFrom
);
}

Expand Down
2 changes: 1 addition & 1 deletion Classes/Domain/TemplateConfiguration/TemplatePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private function validateTemplateConfigurationKeys(): void
{
$isRootTemplate = $this->fullPathToConfiguration === [];
foreach (array_keys($this->configuration) as $key) {
if (!in_array($key, ['type', 'name', 'properties', 'childNodes', 'when', 'withItems', 'withContext'], true)) {
if (!in_array($key, ['type', 'name', 'properties', 'childNodes', 'when', 'withItems', 'withContext', 'copyFrom'], true)) {
$this->addProcessingErrorForPath(
new \InvalidArgumentException(sprintf('Template configuration has illegal key "%s"', $key), 1686150349274),
$key
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/AbstractNodeTemplateTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class AbstractNodeTemplateTestCase extends TestCase // we don't use Flo

protected Node $homePageMainContentCollectionNode;

private ContentSubgraphInterface $subgraph;
protected ContentSubgraphInterface $subgraph;

private NodeTemplateDumper $nodeTemplateDumper;

Expand Down
5 changes: 5 additions & 0 deletions Tests/Functional/ContentRepositoryTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ private function initCleanContentRepository(ContentRepositoryId $contentReposito
$connection->executeStatement('TRUNCATE ' . $eventTableName);
$this->contentRepository->resetProjectionStates();
}

final public function getContentRepository(): ContentRepository
{
return $this->contentRepository;
}
}
75 changes: 75 additions & 0 deletions Tests/Functional/Features/CopyNodes/CopyNodesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Flowpack\NodeTemplates\Tests\Functional\Features\CopyNodes;

use Flowpack\NodeTemplates\Tests\Functional\AbstractNodeTemplateTestCase;
use Neos\ContentRepository\Core\Feature\NodeCreation\Command\CreateNodeAggregateWithNode;
use Neos\ContentRepository\Core\Feature\NodeCreation\Dto\NodeAggregateIdsByNodePaths;
use Neos\ContentRepository\Core\Feature\NodeModification\Dto\PropertyValuesToWrite;
use Neos\ContentRepository\Core\NodeType\NodeTypeName;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;

class CopyNodesTest extends AbstractNodeTemplateTestCase
{
/** @test */
public function itMatchesSnapshot1(): void
{
$this->getContentRepository()->handle(
CreateNodeAggregateWithNode::create(
$this->homePageNode->subgraphIdentity->contentStreamId,
$source = NodeAggregateId::fromString('source-node'),
NodeTypeName::fromString('Flowpack.NodeTemplates:Document.Page'),
$this->homePageNode->originDimensionSpacePoint,
$this->homePageNode->nodeAggregateId,
nodeName: NodeName::fromString(uniqid('node-')),
)->withTetheredDescendantNodeAggregateIds(NodeAggregateIdsByNodePaths::fromArray([
'main' => $main = NodeAggregateId::fromString('source-node-main-collection'),
]))
)->block();

$this->getContentRepository()->handle(
CreateNodeAggregateWithNode::create(
$this->homePageNode->subgraphIdentity->contentStreamId,
NodeAggregateId::fromString('source-node-text-1'),
NodeTypeName::fromString('Flowpack.NodeTemplates:Content.Text'),
$this->homePageNode->originDimensionSpacePoint,
$main,
nodeName: NodeName::fromString(uniqid('node-')),
initialPropertyValues: PropertyValuesToWrite::fromArray([
'text' => 'Lorem ipsum 1'
])
)
)->block();

$this->getContentRepository()->handle(
CreateNodeAggregateWithNode::create(
$this->homePageNode->subgraphIdentity->contentStreamId,
NodeAggregateId::fromString('source-node-text-2'),
NodeTypeName::fromString('Flowpack.NodeTemplates:Content.Text'),
$this->homePageNode->originDimensionSpacePoint,
$main,
nodeName: NodeName::fromString(uniqid('node-')),
initialPropertyValues: PropertyValuesToWrite::fromArray([
'text' => 'Lorem ipsum 2'
])
)
)->block();


$createdNode = $this->createNodeInto(
$this->homePageMainContentCollectionNode,
'Flowpack.NodeTemplates:Content.StaticNodeCopy',
[
'sourceNode' => $this->subgraph->findNodeById($main)
]
);

$this->assertLastCreatedTemplateMatchesSnapshot('StaticNodeCopy');

$this->assertNoExceptionsWereCaught();
$this->assertNodeDumpAndTemplateDumpMatchSnapshot('StaticNodeCopy', $createdNode);
}
}
12 changes: 12 additions & 0 deletions Tests/Functional/Features/CopyNodes/NodeTypes.StaticNodeCopy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'Flowpack.NodeTemplates:Content.StaticNodeCopy':
superTypes:
'Neos.Neos:Content': true
childNodes:
column0:
type: 'Neos.Neos:ContentCollection'
options:
template:
childNodes:
column0Tethered:
name: column0
copyFrom: "${q(parentNode).find('#source-node-main-collection').children().get()}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"childNodes": [
{
"nodeTypeName": "Neos.Neos:ContentCollection",
"nodeName": "column0",
"childNodes": [
{
"nodeTypeName": "Flowpack.NodeTemplates:Content.Text",
"properties": {
"text": "Lorem ipsum 1"
}
},
{
"nodeTypeName": "Flowpack.NodeTemplates:Content.Text",
"properties": {
"text": "Lorem ipsum 2"
}
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"properties": [],
"childNodes": [
{
"type": null,
"name": "column0",
"properties": [],
"childNodes": [],
"copyFrom": [
"Node(source-node-text-1, Flowpack.NodeTemplates:Content.Text)",
"Node(source-node-text-2, Flowpack.NodeTemplates:Content.Text)"
]
}
]
}
6 changes: 5 additions & 1 deletion Tests/Functional/JsonSerializeNodeTreeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ private function serializeValuesInArray(array $array): array
$value = $this->serializeValuesInArray($value);
}
} elseif (is_object($value)) {
$id = ObjectAccess::getProperty($value, 'Persistence_Object_Identifier', true);
try {
$id = ObjectAccess::getProperty($value, 'Persistence_Object_Identifier', true);
} catch (\Exception) {
$id = null;
}
$value = sprintf('object(%s%s)', get_class($value), $id ? (sprintf(', %s', $id)) : '');
} else {
continue;
Expand Down