Skip to content

Commit

Permalink
TASK Refactor context methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dlubitz committed Nov 1, 2023
1 parent 99a562f commit 236e661
Show file tree
Hide file tree
Showing 53 changed files with 783 additions and 117 deletions.
64 changes: 37 additions & 27 deletions config/set/contentrepository-90.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
use Neos\Rector\ContentRepository90\Rules\NodeTypeGetNameRector;
use Neos\Rector\Generic\ValueObject\AddInjection;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Neos\Domain\Service\RenderingModeService;
use Neos\Rector\ContentRepository90\Rules\ContextGetCurrentRenderingModeRector;
use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub;
use Neos\Rector\ContentRepository90\Rules\ContextIsLiveRector;
use Neos\Rector\ContentRepository90\Rules\ContextIsInBackendRector;

return static function (RectorConfig $rectorConfig): void {
// Register FusionFileProcessor. All Fusion Rectors will be auto-registered at this processor.
Expand All @@ -64,9 +69,9 @@


$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
'Neos\\ContentRepository\\Domain\\Model\\NodeInterface' => Node::class,
'Neos\\ContentRepository\\Domain\\Projection\\Content\\NodeInterface' => Node::class,
'Neos\\ContentRepository\\Domain\\Projection\\Content\\TraversableNodeInterface' => Node::class,
'Neos\\ContentRepository\\Domain\\Model\\NodeInterface' => NodeLegacyStub::class,
'Neos\\ContentRepository\\Domain\\Projection\\Content\\NodeInterface' => NodeLegacyStub::class,
'Neos\\ContentRepository\\Domain\\Projection\\Content\\TraversableNodeInterface' => NodeLegacyStub::class,

'Neos\ContentRepository\Domain\Service\Context' => LegacyContextStub::class,
'Neos\Neos\Domain\Service\ContentContext' => LegacyContextStub::class,
Expand All @@ -92,47 +97,47 @@
*/
// setName
// getName
$methodCallToPropertyFetches[] = new MethodCallToPropertyFetch(Node::class, 'getName', 'nodeName');
$methodCallToPropertyFetches[] = new MethodCallToPropertyFetch(NodeLegacyStub::class, 'getName', 'nodeName');
// getLabel -> compatible with ES CR node (nothing to do)
// setProperty
// hasProperty -> compatible with ES CR Node (nothing to do)
// getProperty -> compatible with ES CR Node (nothing to do)
// removeProperty
// getProperties -> PropertyCollectionInterface
$methodCallToPropertyFetches[] = new MethodCallToPropertyFetch(Node::class, 'getProperties', 'properties');
$methodCallToPropertyFetches[] = new MethodCallToPropertyFetch(NodeLegacyStub::class, 'getProperties', 'properties');
// getPropertyNames
// setContentObject -> DEPRECATED / NON-FUNCTIONAL
$methodCallToWarningComments[] = new MethodCallToWarningComment(Node::class, 'setContentObject', '!! Node::setContentObject() is not supported by the new CR. Referencing objects can be done by storing them in Node::properties (and the serialization/deserialization is extensible).');
$methodCallToWarningComments[] = new MethodCallToWarningComment(NodeLegacyStub::class, 'setContentObject', '!! Node::setContentObject() is not supported by the new CR. Referencing objects can be done by storing them in Node::properties (and the serialization/deserialization is extensible).');
// getContentObject -> DEPRECATED / NON-FUNCTIONAL
$methodCallToWarningComments[] = new MethodCallToWarningComment(Node::class, 'getContentObject', '!! Node::getContentObject() is not supported by the new CR. Referencing objects can be done by storing them in Node::properties (and the serialization/deserialization is extensible).');
$methodCallToWarningComments[] = new MethodCallToWarningComment(NodeLegacyStub::class, 'getContentObject', '!! Node::getContentObject() is not supported by the new CR. Referencing objects can be done by storing them in Node::properties (and the serialization/deserialization is extensible).');
// unsetContentObject -> DEPRECATED / NON-FUNCTIONAL
$methodCallToWarningComments[] = new MethodCallToWarningComment(Node::class, 'unsetContentObject', '!! Node::unsetContentObject() is not supported by the new CR. Referencing objects can be done by storing them in Node::properties (and the serialization/deserialization is extensible).');
$methodCallToWarningComments[] = new MethodCallToWarningComment(NodeLegacyStub::class, 'unsetContentObject', '!! Node::unsetContentObject() is not supported by the new CR. Referencing objects can be done by storing them in Node::properties (and the serialization/deserialization is extensible).');
// setNodeType
// getNodeType: NodeType
$methodCallToPropertyFetches[] = new MethodCallToPropertyFetch(Node::class, 'getNodeType', 'nodeType');
$methodCallToPropertyFetches[] = new MethodCallToPropertyFetch(NodeLegacyStub::class, 'getNodeType', 'nodeType');
// setHidden
// isHidden
$rectorConfig->rule(NodeIsHiddenRector::class);
// TODO: Fusion NodeAccess
// setHiddenBeforeDateTime
$methodCallToWarningComments[] = new MethodCallToWarningComment(Node::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.');
$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
$methodCallToWarningComments[] = new MethodCallToWarningComment(Node::class, 'getHiddenBeforeDateTime', '!! Node::getHiddenBeforeDateTime() 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.');
$methodCallToWarningComments[] = new MethodCallToWarningComment(NodeLegacyStub::class, 'getHiddenBeforeDateTime', '!! Node::getHiddenBeforeDateTime() 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.');
$fusionNodePropertyPathToWarningComments[] = new FusionNodePropertyPathToWarningComment('hiddenBeforeDateTime', 'Line %LINE: !! node.hiddenBeforeDateTime 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.');
// setHiddenAfterDateTime
$methodCallToWarningComments[] = new MethodCallToWarningComment(Node::class, 'setHiddenAfterDateTime', '!! Node::setHiddenAfterDateTime() 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.');
$methodCallToWarningComments[] = new MethodCallToWarningComment(NodeLegacyStub::class, 'setHiddenAfterDateTime', '!! Node::setHiddenAfterDateTime() 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.');
// getHiddenAfterDateTime
$methodCallToWarningComments[] = new MethodCallToWarningComment(Node::class, 'getHiddenAfterDateTime', '!! Node::getHiddenAfterDateTime() 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.');
$methodCallToWarningComments[] = new MethodCallToWarningComment(NodeLegacyStub::class, 'getHiddenAfterDateTime', '!! Node::getHiddenAfterDateTime() 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.');
$fusionNodePropertyPathToWarningComments[] = new FusionNodePropertyPathToWarningComment('hiddenAfterDateTime', 'Line %LINE: !! node.hiddenAfterDateTime 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.');
// setHiddenInIndex
// isHiddenInIndex
$rectorConfig->rule(NodeIsHiddenInIndexRector::class);
// Fusion: .hiddenInIndex -> node.properties._hiddenInIndex
$rectorConfig->rule(FusionNodeHiddenInIndexRector::class);
// setAccessRoles
$methodCallToWarningComments[] = new MethodCallToWarningComment(Node::class, 'setAccessRoles', '!! Node::setAccessRoles() is not supported by the new CR.');
$methodCallToWarningComments[] = new MethodCallToWarningComment(NodeLegacyStub::class, 'setAccessRoles', '!! Node::setAccessRoles() is not supported by the new CR.');
// getAccessRoles
$methodCallToWarningComments[] = new MethodCallToWarningComment(Node::class, 'getAccessRoles', '!! Node::getAccessRoles() is not supported by the new CR.');
$methodCallToWarningComments[] = new MethodCallToWarningComment(NodeLegacyStub::class, 'getAccessRoles', '!! Node::getAccessRoles() is not supported by the new CR.');
// getPath
$rectorConfig->rule(NodeGetPathRector::class);
// Fusion: .depth -> Neos.NodeAccess.depth(node)
Expand All @@ -146,16 +151,16 @@
// Fusion: .depth -> Neos.Node.depth(node)
$rectorConfig->rule(FusionNodeDepthRector::class);
// setWorkspace -> internal
$methodCallToWarningComments[] = new MethodCallToWarningComment(Node::class, 'setWorkspace', '!! Node::setWorkspace() was always internal, and the workspace system has been fundamentally changed with the new CR. Try to rewrite your code around Content Streams.');
$methodCallToWarningComments[] = new MethodCallToWarningComment(NodeLegacyStub::class, 'setWorkspace', '!! Node::setWorkspace() was always internal, and the workspace system has been fundamentally changed with the new CR. Try to rewrite your code around Content Streams.');
// getWorkspace
$methodCallToWarningComments[] = new MethodCallToWarningComment(Node::class, 'getWorkspace', '!! Node::getWorkspace() does not make sense anymore concept-wise. In Neos < 9, it pointed to the workspace where the node was *at home at*. Now, the closest we have here is the node identity.');
$methodCallToWarningComments[] = new MethodCallToWarningComment(NodeLegacyStub::class, 'getWorkspace', '!! Node::getWorkspace() does not make sense anymore concept-wise. In Neos < 9, it pointed to the workspace where the node was *at home at*. Now, the closest we have here is the node identity.');
// getIdentifier
$rectorConfig->rule(NodeGetIdentifierRector::class);
$rectorConfig->rule(FusionNodeIdentifierRector::class);
// setIndex -> internal
$methodCallToWarningComments[] = new MethodCallToWarningComment(Node::class, 'setIndex', '!! Node::setIndex() was always internal. To reorder nodes, use the "MoveNodeAggregate" command');
$methodCallToWarningComments[] = new MethodCallToWarningComment(NodeLegacyStub::class, 'setIndex', '!! Node::setIndex() was always internal. To reorder nodes, use the "MoveNodeAggregate" command');
// getIndex
$methodCallToWarningComments[] = new MethodCallToWarningComment(Node::class, 'getIndex', '!! Node::getIndex() is not supported. You can fetch all siblings and inspect the ordering');
$methodCallToWarningComments[] = new MethodCallToWarningComment(NodeLegacyStub::class, 'getIndex', '!! Node::getIndex() is not supported. You can fetch all siblings and inspect the ordering');
// getParent -> Node
$rectorConfig->rule(NodeGetParentRector::class);
// Fusion: .parent -> Neos.NodeAccess.findParent(node)
Expand All @@ -172,7 +177,7 @@
// remove()
// setRemoved()
// isRemoved()
$methodCallToWarningComments[] = new MethodCallToWarningComment(Node::class, 'isRemoved', '!! Node::isRemoved() - the new CR *never* returns removed nodes; so you can simplify your code and just assume removed == FALSE in all scenarios.');
$methodCallToWarningComments[] = new MethodCallToWarningComment(NodeLegacyStub::class, 'isRemoved', '!! Node::isRemoved() - the new CR *never* returns removed nodes; so you can simplify your code and just assume removed == FALSE in all scenarios.');
$fusionNodePropertyPathToWarningComments[] = new FusionNodePropertyPathToWarningComment('removed', 'Line %LINE: !! node.removed - the new CR *never* returns removed nodes; so you can simplify your code and just assume removed == FALSE in all scenarios.');
// isVisible()
// isAccessible()
Expand All @@ -185,7 +190,7 @@
// copyAfter()
// copyInto()
// getNodeData()
$methodCallToWarningComments[] = new MethodCallToWarningComment(Node::class, 'getNodeData', '!! Node::getNodeData() - the new CR is not based around the concept of NodeData anymore. You need to rewrite your code here.');
$methodCallToWarningComments[] = new MethodCallToWarningComment(NodeLegacyStub::class, 'getNodeData', '!! Node::getNodeData() - the new CR is not based around the concept of NodeData anymore. You need to rewrite your code here.');
// getContext()
// getContext()->getWorkspace()
$rectorConfig->rule(NodeGetContextGetWorkspaceRector::class);
Expand All @@ -207,13 +212,13 @@
// isTethered()
// getContentStreamIdentifier() -> threw exception in <= Neos 8.0 - so nobody could have used this
// getNodeAggregateIdentifier()
$methodCallToPropertyFetches[] = new MethodCallToPropertyFetch(Node::class, 'getNodeAggregateIdentifier', 'nodeAggregateId');
$methodCallToPropertyFetches[] = new MethodCallToPropertyFetch(NodeLegacyStub::class, 'getNodeAggregateIdentifier', 'nodeAggregateId');
$rectorConfig->rule(rectorClass: FusionNodeAggregateIdentifierRector::class);
// getNodeTypeName()
$methodCallToPropertyFetches[] = new MethodCallToPropertyFetch(Node::class, 'getNodeTypeName', 'nodeTypeName');
$methodCallToPropertyFetches[] = new MethodCallToPropertyFetch(NodeLegacyStub::class, 'getNodeTypeName', 'nodeTypeName');
// getNodeType() ** (included/compatible in old NodeInterface)
// getNodeName()
$methodCallToPropertyFetches[] = new MethodCallToPropertyFetch(Node::class, 'getNodeName', 'nodeName');
$methodCallToPropertyFetches[] = new MethodCallToPropertyFetch(NodeLegacyStub::class, 'getNodeName', 'nodeName');
// getOriginDimensionSpacePoint() -> threw exception in <= Neos 8.0 - so nobody could have used this
// getProperties() ** (included/compatible in old NodeInterface)
// getProperty() ** (included/compatible in old NodeInterface)
Expand Down Expand Up @@ -285,13 +290,13 @@
// TODO: PHP
// TODO: Fusion
// ContentContext::isLive -> renderingMode.isLive
// TODO: PHP
$rectorConfig->rule(ContextIsLiveRector::class);
$rectorConfig->rule(FusionContextLiveRector::class);
// ContentContext::isInBackend -> renderingMode.inBackend
// TODO: PHP
$rectorConfig->rule(ContextIsInBackendRector::class);
$rectorConfig->rule(FusionContextInBackendRector::class);
// ContentContext::getCurrentRenderingMode... -> renderingMode...
// TODO: PHP
$rectorConfig->rule(ContextGetCurrentRenderingModeRector::class);
$rectorConfig->rule(FusionContextCurrentRenderingModeRector::class);


Expand Down Expand Up @@ -379,9 +384,14 @@
\Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateIds::class => 'toJson()',
]);

$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
LegacyContextStub::class => NodeLegacyStub::class,
]);

// Should run LAST - as other rules above might create $this->contentRepositoryRegistry calls.
$rectorConfig->ruleWithConfiguration(InjectServiceIfNeededRector::class, [
new AddInjection('contentRepositoryRegistry', ContentRepositoryRegistry::class),
new AddInjection('renderingModeService', RenderingModeService::class),
]);
// TODO: does not fully seem to work.$rectorConfig->rule(RemoveDuplicateCommentRector::class);
};
18 changes: 18 additions & 0 deletions src/ContentRepository90/Legacy/NodeLegacyStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Neos\Rector\ContentRepository90\Legacy;

/**
* @deprecated
*/
class NodeLegacyStub
{

public function getContext() : LegacyContextStub {
return new LegacyContextStub([]);
}

public function isLive() :bool {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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 Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Rector\PostRector\Collector\NodesToAddCollector;
use PhpParser\Node\Expr\Assign;

final class ContextGetCurrentRenderingModeRector extends AbstractRector
{
use AllTraits;
use ContextRectorTrait;

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


public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('"ContentContext::getCurrentRenderingMode()" will be replaced with RenderingModeService::findByCurrentUser().', __CLASS__);
}

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

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

$oldContextMethod = 'getCurrentRenderingMode';
if (
$this->isContextWithMethod($node, $oldContextMethod)
) {

return $this->nodeFactory->createMethodCall(
$this->nodeFactory->createPropertyFetch(
'this',
'renderingModeService'
),
'findByCurrentUser'
);
}

return null;
}

}
67 changes: 67 additions & 0 deletions src/ContentRepository90/Rules/ContextIsInBackendRector.php
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 Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Rector\PostRector\Collector\NodesToAddCollector;
use PhpParser\Node\Expr\Assign;
use PhpParser\NodeDumper;

final class ContextIsInBackendRector extends AbstractRector
{
use AllTraits;
use ContextRectorTrait;

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


public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('"ContentContext::isLive()" will be replaced with RenderingModeService::findByCurrentUser().', __CLASS__);
}

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

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

$oldContextMethod = 'isInBackend';
if ($this->isContextWithMethod($node, $oldContextMethod)) {

$renderingModeService = $this->nodeFactory->createMethodCall(
$this->nodeFactory->createPropertyFetch(
'this',
'renderingModeService'
),
'findByCurrentUser'
);

return $this->nodeFactory->createPropertyFetch(
$renderingModeService, 'isEdit'
);

}

return null;
}

}
Loading

0 comments on commit 236e661

Please sign in to comment.