Skip to content

Commit

Permalink
Merge pull request #40 from neos/task/nodetype
Browse files Browse the repository at this point in the history
TASK: Refactor nodetype
  • Loading branch information
ahaeslich authored Nov 5, 2023
2 parents 3cae4c0 + eccef62 commit be84b8b
Show file tree
Hide file tree
Showing 20 changed files with 720 additions and 16 deletions.
52 changes: 36 additions & 16 deletions config/set/contentrepository-90.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@
use Neos\Rector\ContentRepository90\Rules\ContextIsInBackendRector;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\Rector\ContentRepository90\Rules\FusionPrimaryContentRector;
use Neos\Rector\ContentRepository90\Rules\NodeTypeGetAutoCreatedChildNodesRector;
use Neos\Rector\ContentRepository90\Rules\NodeTypeHasAutoCreatedChildNodesRector;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Neos\ContentRepository\Core\NodeType\NodeType;
use Neos\Rector\ContentRepository90\Rules\NodeTypeGetTypeOfAutoCreatedChildNodeRector;
use Neos\Rector\ContentRepository90\Rules\NodeTypeAllowsGrandchildNodeTypeRector;

return static function (RectorConfig $rectorConfig): void {
// Register FusionFileProcessor. All Fusion Rectors will be auto-registered at this processor.
Expand Down Expand Up @@ -119,7 +126,7 @@
// setHidden
// isHidden
$rectorConfig->rule(NodeIsHiddenRector::class);
// TODO: Fusion NodeAccess
// TODO: Fusion NodeAccess
// setHiddenBeforeDateTime
$methodCallToWarningComments[] = new MethodCallToWarningComment(NodeLegacyStub::class, 'setHiddenBeforeDateTime', '!! Node::setHiddenBeforeDateTime() is not supported by the new CR. Timed publishing will be implemented not on the read model, but by dispatching commands at a given time.');
// getHiddenBeforeDateTime
Expand All @@ -144,9 +151,9 @@
// Fusion: .depth -> Neos.NodeAccess.depth(node)
$rectorConfig->rule(FusionNodePathRector::class);
// getContextPath
// TODO: PHP
// TODO: Fusion
// - NodeAddress + LOG (WARNING)
// TODO: PHP
// TODO: Fusion
// - NodeAddress + LOG (WARNING)
// getDepth
$rectorConfig->rule(NodeGetDepthRector::class);
// Fusion: .depth -> Neos.Node.depth(node)
Expand Down Expand Up @@ -195,13 +202,13 @@
// getContext()
// getContext()->getWorkspace()
$rectorConfig->rule(NodeGetContextGetWorkspaceRector::class);
// TODO: Fusion
// TODO: Fusion
// getContext()->getWorkspaceName()
$rectorConfig->rule(NodeGetContextGetWorkspaceNameRector::class);
// TODO: Fusion
// TODO: Fusion
// getDimensions()
$rectorConfig->rule(NodeGetDimensionsRector::class);
// TODO: Fusion
// TODO: Fusion
// createVariantForContext()
// isAutoCreated()
// getOtherNodeVariants()
Expand Down Expand Up @@ -230,8 +237,21 @@
* Neos\ContentRepository\Core\NodeType\NodeType
*/
// getName()
$rectorConfig->rule(rectorClass: FusionNodeTypeNameRector::class);
$rectorConfig->rule(rectorClass: NodeTypeGetNameRector::class);
$rectorConfig->rule(FusionNodeTypeNameRector::class);
$rectorConfig->rule(NodeTypeGetNameRector::class);
// getAutoCreatedChildNodes
$rectorConfig->rule(NodeTypeGetAutoCreatedChildNodesRector::class);
// hasAutoCreatedChildNode
$rectorConfig->ruleWithConfiguration(RenameMethodRector::class, [new MethodCallRename(
NodeType::class,
'hasAutoCreatedChildNode',
'hasTetheredNode'
)]);
// getTypeOfAutoCreatedChildNode
$rectorConfig->rule(NodeTypeGetTypeOfAutoCreatedChildNodeRector::class);
// allowsGrandchildNodeType
$rectorConfig->rule(NodeTypeAllowsGrandchildNodeTypeRector::class);


/**
* Neos\ContentRepository\Domain\Projection\Content\TraversableNodeInterface
Expand All @@ -240,17 +260,17 @@
// findParentNode() -> TraversableNodeInterface
$rectorConfig->rule(NodeFindParentNodeRector::class);
// findNodePath() -> NodePath
// TODO: PHP
// TODO: PHP
// findNamedChildNode(NodeName $nodeName): TraversableNodeInterface;
// TODO: PHP
// TODO: PHP
// findChildNodes(NodeTypeConstraints $nodeTypeConstraints = null, int $limit = null, int $offset = null): TraversableNodes;
// TODO: PHP
// TODO: PHP
// countChildNodes(NodeTypeConstraints $nodeTypeConstraints = null): int;
// TODO: PHP
// TODO: PHP
// findReferencedNodes(): TraversableNodes;
// TODO: PHP
// TODO: PHP
// findNamedReferencedNodes(PropertyName $edgeName): TraversableNodes;
// TODO: PHP
// TODO: PHP
// findReferencingNodes() -> threw exception in <= Neos 8.0 - so nobody could have used this
// findNamedReferencingNodes() -> threw exception in <= Neos 8.0 - so nobody could have used this

Expand Down Expand Up @@ -327,7 +347,7 @@
/**
* Neos.Neos:PrimaryContent
*/
$rectorConfig->rule(FusionPrimaryContentRector::class );
$rectorConfig->rule(FusionPrimaryContentRector::class);

/**
* SPECIAL rules
Expand Down
10 changes: 10 additions & 0 deletions src/ContentRepository90/Legacy/NodeLegacyStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Neos\Rector\ContentRepository90\Legacy;

use Neos\ContentRepository\Core\NodeType\NodeType;

/**
* @deprecated
*/
Expand All @@ -11,4 +13,12 @@ class NodeLegacyStub
public function getContext() : LegacyContextStub {
return new LegacyContextStub([]);
}

public function getNodeType(): NodeType {
return new NodeType('foo', [], [], null);
}

public function getParent(): NodeLegacyStub {
return new NodeLegacyStub();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare (strict_types=1);

namespace Neos\Rector\ContentRepository90\Rules;

use Neos\Rector\Utility\CodeSampleLoader;
use PhpParser\Node;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\PostRector\Collector\NodesToAddCollector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Neos\ContentRepository\Core\NodeType\NodeType;
use PhpParser\NodeDumper;

final class NodeTypeAllowsGrandchildNodeTypeRector extends AbstractRector
{
use AllTraits;

public function __construct(
private readonly NodesToAddCollector $nodesToAddCollector
) {
}

public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('"$nodeType->allowsGrandchildNodeType($parentNodeName, $nodeType)" will be rewritten.', __CLASS__);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Node\Expr\MethodCall::class];
}

/**
* @param Node\Expr\MethodCall $node
*/
public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Expr\MethodCall);

if (!$this->isObjectType($node->var, new ObjectType(NodeType::class))) {
return null;
}

if (!$this->isName($node->name, 'allowsGrandchildNodeType')) {
return null;
}

$this->nodesToAddCollector->addNodesBeforeNode(
[
self::withTodoComment(
'Make this code aware of multiple Content Repositories.',
self::assign('contentRepository', $this->this_contentRepositoryRegistry_get($this->contentRepositoryId_fromString('default'))),
)
],
$node
);

return $this->nodeFactory->createMethodCall(
$this->contentRepository_getNodeTypeManager(),
'isNodeTypeAllowedAsChildToTetheredNode',
[
$node->var,
$this->nodeFactory->createStaticCall(
\Neos\ContentRepository\Core\SharedModel\Node\NodeName::class,
'fromString',
[
$node->args[0]
]
),
$node->args[1]
]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare (strict_types=1);
namespace Neos\Rector\ContentRepository90\Rules;

use Neos\Rector\Utility\CodeSampleLoader;
use PhpParser\Node;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\PostRector\Collector\NodesToAddCollector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Neos\ContentRepository\Core\NodeType\NodeType;

final class NodeTypeGetAutoCreatedChildNodesRector extends AbstractRector
{
use AllTraits;

public function __construct(
private readonly NodesToAddCollector $nodesToAddCollector
)
{
}

public function getRuleDefinition() : RuleDefinition
{
return CodeSampleLoader::fromFile('"$nodeType->getAutoCreatedChildNodes()" will be rewritten.', __CLASS__);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Node\Expr\MethodCall::class];
}
/**
* @param Node\Expr\MethodCall $node
*/
public function refactor(Node $node) : ?Node
{
assert($node instanceof Node\Expr\MethodCall);

if (!$this->isObjectType($node->var, new ObjectType(NodeType::class))) {
return null;
}

if (!$this->isName($node->name, 'getAutoCreatedChildNodes')) {
return null;
}

$this->nodesToAddCollector->addNodesBeforeNode(
[
self::withTodoComment(
'Make this code aware of multiple Content Repositories.',
self::assign('contentRepository', $this->this_contentRepositoryRegistry_get($this->contentRepositoryId_fromString('default'))),
)
],
$node
);

return $this->nodeFactory->createMethodCall(
$this->contentRepository_getNodeTypeManager(),
'getTetheredNodesConfigurationForNodeType',
[$node->var]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare (strict_types=1);
namespace Neos\Rector\ContentRepository90\Rules;

use Neos\Rector\Utility\CodeSampleLoader;
use PhpParser\Node;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\PostRector\Collector\NodesToAddCollector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Neos\ContentRepository\Core\NodeType\NodeType;
use PhpParser\NodeDumper;

final class NodeTypeGetTypeOfAutoCreatedChildNodeRector extends AbstractRector
{
use AllTraits;

public function __construct(
private readonly NodesToAddCollector $nodesToAddCollector
)
{
}

public function getRuleDefinition() : RuleDefinition
{
return CodeSampleLoader::fromFile('"$nodeType->getTypeOfAutoCreatedChildNode($nodeName)" will be rewritten.', __CLASS__);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Node\Expr\MethodCall::class];
}
/**
* @param Node\Expr\MethodCall $node
*/
public function refactor(Node $node) : ?Node
{
assert($node instanceof Node\Expr\MethodCall);

if (!$this->isObjectType($node->var, new ObjectType(NodeType::class))) {
return null;
}

if (!$this->isName($node->name, 'getTypeOfAutoCreatedChildNode')) {
return null;
}

$this->nodesToAddCollector->addNodesBeforeNode(
[
self::withTodoComment(
'Make this code aware of multiple Content Repositories.',
self::assign('contentRepository', $this->this_contentRepositoryRegistry_get($this->contentRepositoryId_fromString('default'))),
)
],
$node
);

return $this->nodeFactory->createMethodCall(
$this->contentRepository_getNodeTypeManager(),
'getTypeOfTetheredNode',
[
$node->var,
$node->args[0]
]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub;
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;

class SomeClass
{
public function run(NodeLegacyStub $node)
{
$parentNodeName = 'name';
$nodeType = $node->getNodeType();
$grandParentsNodeType = $node->getParent()->getParent()->getNodeType();

$grandParentsNodeType->allowsGrandchildNodeType($parentNodeName, $nodeType);
}
}

?>
-----
<?php

use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub;
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;

class SomeClass
{
#[\Neos\Flow\Annotations\Inject]
protected \Neos\ContentRepositoryRegistry\ContentRepositoryRegistry $contentRepositoryRegistry;
public function run(NodeLegacyStub $node)
{
$parentNodeName = 'name';
$nodeType = $node->getNodeType();
$grandParentsNodeType = $node->getParent()->getParent()->getNodeType();
// TODO 9.0 migration: Make this code aware of multiple Content Repositories.
$contentRepository = $this->contentRepositoryRegistry->get(\Neos\ContentRepository\Core\Factory\ContentRepositoryId::fromString('default'));

$contentRepository->getNodeTypeManager()->isNodeTypeAllowedAsChildToTetheredNode($grandParentsNodeType, \Neos\ContentRepository\Core\SharedModel\Node\NodeName::fromString($parentNodeName), $nodeType);
}
}

?>
Loading

0 comments on commit be84b8b

Please sign in to comment.