-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from neos/feature/node-search-service
FEATURE: Add migration for NodeSearchService
- Loading branch information
Showing
7 changed files
with
431 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/ContentRepository90/Rules/NodeSearchServiceRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?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; | ||
|
||
final class NodeSearchServiceRector extends AbstractRector | ||
{ | ||
use AllTraits; | ||
|
||
public function __construct( | ||
private readonly NodesToAddCollector $nodesToAddCollector | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return CodeSampleLoader::fromFile('"NodeSearchService::findDescendantNodes()" will be rewritten', __CLASS__); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [\PhpParser\Node\Expr\MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node\Expr\MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
assert($node instanceof Node\Expr\MethodCall); | ||
|
||
if (!$this->isObjectType($node->var, new ObjectType(\Neos\Neos\Domain\Service\NodeSearchService::class))) { | ||
return null; | ||
} | ||
if (!$this->isName($node->name, 'findByProperties')) { | ||
return null; | ||
} | ||
|
||
if (!isset($node->args[3])) { | ||
$nodeExpr = self::assign('node', new \PhpParser\Node\Scalar\String_('we-need-a-node-here')); | ||
$nodeNode = $nodeExpr->expr->var; | ||
|
||
$this->nodesToAddCollector->addNodesBeforeNode( | ||
[ | ||
self::withTodoComment('The replacement needs a node as starting point for the search. Please provide a node, to make this replacement working.', $nodeExpr), | ||
$subgraphNode = self::assign('subgraph', $this->this_contentRepositoryRegistry_subgraphForNode($nodeNode)), | ||
], | ||
$node | ||
); | ||
|
||
} else { | ||
$this->nodesToAddCollector->addNodesBeforeNode( | ||
[ | ||
self::withTodoComment('This could be a suitable replacement. Please check if all your requirements are still fulfilled.', | ||
$subgraphNode = self::assign('subgraph', $this->this_contentRepositoryRegistry_subgraphForNode($node->args[3]->value)) | ||
) | ||
], | ||
$node | ||
|
||
); | ||
$nodeNode = $node->args[3]->value; | ||
|
||
} | ||
|
||
return $this->nodeFactory->createMethodCall( | ||
$subgraphNode->expr->var, | ||
'findDescendantNodes', | ||
[ | ||
$this->nodeFactory->createPropertyFetch( | ||
$nodeNode, | ||
'aggregateId' | ||
), | ||
$this->nodeFactory->createStaticCall( | ||
\Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindDescendantNodesFilter::class, | ||
'create', | ||
[ | ||
'nodeTypes' => $this->nodeFactory->createStaticCall( | ||
\Neos\ContentRepository\Core\Projection\ContentGraph\Filter\NodeType\NodeTypeCriteria::class, | ||
'create', | ||
[ | ||
$this->nodeFactory->createStaticCall( | ||
\Neos\ContentRepository\Core\NodeType\NodeTypeNames::class, | ||
'fromStringArray', | ||
[ | ||
$node->args[1]->value, | ||
] | ||
), | ||
$this->nodeFactory->createStaticCall( | ||
\Neos\ContentRepository\Core\NodeType\NodeTypeNames::class, | ||
'createEmpty', | ||
), | ||
] | ||
), | ||
'searchTerm' => $node->args[0]->value, | ||
] | ||
) | ||
] | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* \Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindDescendantNodesFilter::create( | ||
* nodeTypes: \Neos\ContentRepository\Core\Projection\ContentGraph\Filter\NodeType\NodeTypeCriteria::create( | ||
* \Neos\ContentRepository\Core\NodeType\NodeTypeNames::fromStringArray($searchNodeTypes), | ||
* \Neos\ContentRepository\Core\NodeType\NodeTypeNames::createEmpty() | ||
* ), | ||
* searchTerm: $term | ||
* ) | ||
*/ |
64 changes: 64 additions & 0 deletions
64
tests/ContentRepository90/Rules/NodeSearchServiceRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Neos\Rector\Test; | ||
|
||
use Neos\ContentRepository\Domain\Model\Node; | ||
use Neos\ContentRepository\Domain\Service\Context; | ||
|
||
class SomeClass extends AnotherClass | ||
{ | ||
/** | ||
* @var \Neos\Neos\Domain\Service\NodeSearchService | ||
*/ | ||
private $nodeSearchService; | ||
|
||
public function startingPointNodeIsGiven(Node $node, Context $context) | ||
{ | ||
$term = "term"; | ||
$searchNodeTypes = []; | ||
$nodes = $this->nodeSearchService->findByProperties($term, $searchNodeTypes, $context, $node); | ||
} | ||
|
||
public function startingPointNodeIsNotGiven(Context $context) | ||
{ | ||
$term = "term"; | ||
$searchNodeTypes = []; | ||
$nodes = $this->nodeSearchService->findByProperties($term, $searchNodeTypes, $context); | ||
} | ||
} | ||
|
||
----- | ||
<?php | ||
|
||
namespace Neos\Rector\Test; | ||
|
||
use Neos\ContentRepository\Domain\Model\Node; | ||
use Neos\ContentRepository\Domain\Service\Context; | ||
|
||
class SomeClass extends AnotherClass | ||
{ | ||
/** | ||
* @var \Neos\Neos\Domain\Service\NodeSearchService | ||
*/ | ||
private $nodeSearchService; | ||
|
||
public function startingPointNodeIsGiven(Node $node, Context $context) | ||
{ | ||
$term = "term"; | ||
$searchNodeTypes = []; | ||
// TODO 9.0 migration: This could be a suitable replacement. Please check if all your requirements are still fulfilled. | ||
$subgraph = $this->contentRepositoryRegistry->subgraphForNode($node); | ||
$nodes = $subgraph->findDescendantNodes($node->aggregateId, \Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindDescendantNodesFilter::create(nodeTypes: \Neos\ContentRepository\Core\Projection\ContentGraph\Filter\NodeType\NodeTypeCriteria::create(\Neos\ContentRepository\Core\NodeType\NodeTypeNames::fromStringArray($searchNodeTypes), \Neos\ContentRepository\Core\NodeType\NodeTypeNames::createEmpty()), searchTerm: $term)); | ||
} | ||
|
||
public function startingPointNodeIsNotGiven(Context $context) | ||
{ | ||
$term = "term"; | ||
$searchNodeTypes = []; | ||
// TODO 9.0 migration: The replacement needs a node as starting point for the search. Please provide a node, to make this replacement working. | ||
$node = 'we-need-a-node-here'; | ||
$subgraph = $this->contentRepositoryRegistry->subgraphForNode($node); | ||
$nodes = $subgraph->findDescendantNodes($node->aggregateId, \Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindDescendantNodesFilter::create(nodeTypes: \Neos\ContentRepository\Core\Projection\ContentGraph\Filter\NodeType\NodeTypeCriteria::create(\Neos\ContentRepository\Core\NodeType\NodeTypeNames::fromStringArray($searchNodeTypes), \Neos\ContentRepository\Core\NodeType\NodeTypeNames::createEmpty()), searchTerm: $term)); | ||
} | ||
} | ||
|
31 changes: 31 additions & 0 deletions
31
tests/ContentRepository90/Rules/NodeSearchServiceRector/NodeSearchServiceRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\Rector\Tests\ContentRepository90\Rules\NodeGetChildNodesRector; | ||
|
||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class NodeSearchServiceRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $fileInfo): void | ||
{ | ||
$this->doTestFile($fileInfo); | ||
} | ||
|
||
/** | ||
* @return \Iterator<string> | ||
*/ | ||
public function provideData(): \Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/ContentRepository90/Rules/NodeSearchServiceRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare (strict_types=1); | ||
|
||
use Neos\Rector\ContentRepository90\Rules\NodeGetNodeTypeGetNameRector; | ||
use Neos\Rector\ContentRepository90\Rules\NodeGetNodeTypeRector; | ||
use Neos\Rector\ContentRepository90\Rules\NodeSearchServiceRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(NodeSearchServiceRector::class); | ||
}; |
Oops, something went wrong.