Skip to content

Commit

Permalink
TASK: Allow NodeType::tetheredNodeTypeDefinitions as forward compat…
Browse files Browse the repository at this point in the history
…ible hack
  • Loading branch information
mhsdesign committed May 14, 2024
1 parent 0e55e7e commit 5df367d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ protected function requireNodeTypeToDeclareReference(NodeTypeName $nodeTypeName,
protected function requireNodeTypeNotToDeclareTetheredChildNodeName(NodeTypeName $nodeTypeName, NodeName $nodeName): void
{
$nodeType = $this->requireNodeType($nodeTypeName);
if ($nodeType->hasTetheredNode($nodeName)) {
if ($nodeType->tetheredNodeTypeDefinitions->contain($nodeName)) {
throw new NodeNameIsAlreadyCovered(
'Node name "' . $nodeName->value . '" is reserved for a tethered child of parent node aggregate of type "'
. $nodeTypeName->value . '".'
Expand Down Expand Up @@ -369,7 +369,7 @@ protected function areNodeTypeConstraintsImposedByGrandparentValid(
NodeType $nodeType
): bool {
return !($parentNodeName
&& $grandParentsNodeType->hasTetheredNode($parentNodeName)
&& $grandParentsNodeType->tetheredNodeTypeDefinitions->contain($parentNodeName)
&& !$this->getNodeTypeManager()->isNodeTypeAllowedAsChildToTetheredNode($grandParentsNodeType->name, $parentNodeName, $nodeType->name));
}

Expand Down
39 changes: 22 additions & 17 deletions Neos.ContentRepository.Core/Classes/NodeType/NodeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ final class NodeType
*/
public readonly NodeTypeName $name;

/** @phpstan-ignore-next-line */
public readonly TetheredNodeTypeDefinitions $tetheredNodeTypeDefinitions;

/**
* Configuration for this node type, can be an arbitrarily nested array. Does not include inherited configuration.
*
Expand Down Expand Up @@ -73,8 +76,6 @@ final class NodeType
*/
protected bool $initialized = false;

private ?TetheredNodeTypeDefinitions $tetheredNodeTypeDefinitions = null;

/**
* @param NodeTypeName $name Name of the node type
* @param array<string,NodeType|null> $declaredSuperTypes Parent types instances of this node type, if null it should be unset
Expand Down Expand Up @@ -102,6 +103,23 @@ public function __construct(
}

$this->localConfiguration = $configuration;
/** lazy properties {@see __get()} */
/** @phpstan-ignore-next-line */
unset($this->tetheredNodeTypeDefinitions);
}

/**
* We unset the readonly properties in the constructor, so that this magic getter is invoked, which initializes the properties.
* {@see https://peakd.com/hive-168588/@crell/php-tricks-lazy-public-readonly-properties}
* This is a temporary hack until https://github.com/neos/neos-development-collection/pull/4999 is merged.
*/
public function __get(string $key): mixed
{
if ($key === 'tetheredNodeTypeDefinitions') {
/** @phpstan-ignore-next-line */
return $this->tetheredNodeTypeDefinitions = $this->getTetheredNodeTypeDefinitions();
}
throw new \BadMethodCallException(sprintf('NodeType::%s does not exist.', $key), 1715710576);
}

/**
Expand Down Expand Up @@ -488,21 +506,8 @@ public function getDefaultValuesForProperties(): array
return $defaultValues;
}

/**
* @return bool true if $nodeName is an autocreated child node, false otherwise
*/
public function hasTetheredNode(NodeName $nodeName): bool
{
return $this->getTetheredNodeTypeDefinitions()->contain($nodeName);
}

public function getTetheredNodeTypeDefinitions(): TetheredNodeTypeDefinitions
private function getTetheredNodeTypeDefinitions(): TetheredNodeTypeDefinitions
{
if ($this->tetheredNodeTypeDefinitions) {
return $this->tetheredNodeTypeDefinitions;
}
$this->initialize();

$childNodeConfiguration = $this->getConfiguration('childNodes') ?? [];
$tetheredNodeTypeDefinitions = [];
foreach ($childNodeConfiguration as $childNodeName => $configurationForChildNode) {
Expand All @@ -513,7 +518,7 @@ public function getTetheredNodeTypeDefinitions(): TetheredNodeTypeDefinitions
);
}
}
return $this->tetheredNodeTypeDefinitions = TetheredNodeTypeDefinitions::fromArray($tetheredNodeTypeDefinitions);
return TetheredNodeTypeDefinitions::fromArray($tetheredNodeTypeDefinitions);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public function getTetheredNodesConfigurationForNodeType(NodeTypeName $nodeTypeN
public function isNodeTypeAllowedAsChildToTetheredNode(NodeTypeName $parentNodeTypeName, NodeName $tetheredNodeName, NodeTypeName $nodeTypeNameToCheck): bool
{
$parentNodeType = $this->getNodeType($parentNodeTypeName);
$nodeTypeNameOfTetheredNode = $parentNodeType?->getTetheredNodeTypeDefinitions()->get($tetheredNodeName)?->nodeTypeName;
$nodeTypeNameOfTetheredNode = $parentNodeType?->tetheredNodeTypeDefinitions->get($tetheredNodeName)?->nodeTypeName;
if (!$parentNodeType || !$nodeTypeNameOfTetheredNode) {
// Cannot determine if grandchild is allowed, because the given child node name is not auto-created.
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ private function isAutoCreatedChildNode(NodeTypeName $parentNodeTypeName, NodeNa
if (!$nodeTypeOfParent) {
return false;
}
return $nodeTypeOfParent->hasTetheredNode($nodeName);
return $nodeTypeOfParent->tetheredNodeTypeDefinitions->contain($nodeName);
}

private function dispatch(Severity $severity, string $message, mixed ...$args): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function findAdjustmentsForNodeType(NodeTypeName $nodeTypeName): \Generat
$parentNodeType = $this->nodeTypeManager->getNodeType($parentNode->nodeTypeName);
if ($parentNodeType) {
$allowedByParent = $parentNodeType->allowsChildNodeType($nodeType)
|| ($nodeAggregate->nodeName && $parentNodeType->hasTetheredNode($nodeAggregate->nodeName));
|| ($nodeAggregate->nodeName && $parentNodeType->tetheredNodeTypeDefinitions->contain($nodeAggregate->nodeName));
}
}

Expand Down

0 comments on commit 5df367d

Please sign in to comment.