Skip to content

Commit

Permalink
TASK: Fix NextUntil and PrevUntil and optimize Next, NextAll,…
Browse files Browse the repository at this point in the history
… `Prev`, `PrevAll`
  • Loading branch information
mficzel committed Nov 3, 2023
1 parent 50182cd commit 4940424
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,18 @@ public function count(): int
public function first(): ?Node
{
if (count($this->nodes) > 0) {
$array = $this->nodes;
return reset($array);
$key = array_key_first($this->nodes);
return $this->nodes[$key];
}

return null;
}

public function last(): ?Node
{
if (count($this->nodes) > 0) {
$key = array_key_last($this->nodes);
return $this->nodes[$key];
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindChildNodesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindSucceedingSiblingNodesFilter;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Eel\FlowQuery\FlowQuery;
Expand Down Expand Up @@ -69,7 +70,12 @@ public function evaluate(FlowQuery $flowQuery, array $arguments)
$output = [];
$outputNodePaths = [];
foreach ($flowQuery->getContext() as $contextNode) {
foreach ($this->getNextForNode($contextNode) as $nextNode) {
$nextNodes = $this->contentRepositoryRegistry->subgraphForNode($contextNode)
->findSucceedingSiblingNodes(
$contextNode->nodeAggregateId,
FindSucceedingSiblingNodesFilter::create()
);
foreach ($nextNodes as $nextNode) {
if ($nextNode !== null && !isset($outputNodePaths[$nextNode->nodeAggregateId->value])) {
$outputNodePaths[$nextNode->nodeAggregateId->value] = true;
$output[] = $nextNode;
Expand All @@ -82,21 +88,4 @@ public function evaluate(FlowQuery $flowQuery, array $arguments)
$flowQuery->pushOperation('filter', $arguments);
}
}

/**
* @param Node $contextNode The node for which the next node should be found
* @return Nodes The next nodes of $contextNode
*/
protected function getNextForNode(Node $contextNode): Nodes
{
$subgraph = $this->contentRepositoryRegistry->subgraphForNode($contextNode);

$parentNode = $subgraph->findParentNode($contextNode->nodeAggregateId);
if ($parentNode === null) {
return Nodes::createEmpty();
}

return $subgraph->findChildNodes($parentNode->nodeAggregateId, FindChildNodesFilter::create())
->nextAll($contextNode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindChildNodesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindSucceedingSiblingNodesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Eel\FlowQuery\FlowQuery;
Expand Down Expand Up @@ -69,7 +70,11 @@ public function evaluate(FlowQuery $flowQuery, array $arguments)
$output = [];
$outputNodePaths = [];
foreach ($flowQuery->getContext() as $contextNode) {
$nextNode = $this->getNextForNode($contextNode);
$nextNode = $this->contentRepositoryRegistry->subgraphForNode($contextNode)
->findSucceedingSiblingNodes(
$contextNode->nodeAggregateId,
FindSucceedingSiblingNodesFilter::create()
)->first();
if ($nextNode !== null && !isset($outputNodePaths[$nextNode->nodeAggregateId->value])) {
$outputNodePaths[$nextNode->nodeAggregateId->value] = true;
$output[] = $nextNode;
Expand All @@ -81,21 +86,4 @@ public function evaluate(FlowQuery $flowQuery, array $arguments)
$flowQuery->pushOperation('filter', $arguments);
}
}

/**
* @param Node $contextNode The node for which the preceding node should be found
* @return Node|null The following node of $contextNode or NULL
*/
protected function getNextForNode(Node $contextNode): ?Node
{
$subgraph = $this->contentRepositoryRegistry->subgraphForNode($contextNode);

$parentNode = $subgraph->findParentNode($contextNode->nodeAggregateId);
if ($parentNode === null) {
return null;
}

return $subgraph->findChildNodes($parentNode->nodeAggregateId, FindChildNodesFilter::create())
->next($contextNode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindChildNodesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindSucceedingSiblingNodesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\Projection\ContentGraph\Nodes;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
Expand Down Expand Up @@ -70,22 +71,21 @@ public function evaluate(FlowQuery $flowQuery, array $arguments)
{
$output = [];
$outputNodeIdentifiers = [];
$until = [];

foreach ($flowQuery->getContext() as $contextNode) {
$nextNodes = $this->getNextForNode($contextNode);
$nextNodes = $this->contentRepositoryRegistry->subgraphForNode($contextNode)
->findSucceedingSiblingNodes(
$contextNode->nodeAggregateId,
FindSucceedingSiblingNodesFilter::create()
);
if (isset($arguments[0]) && !empty($arguments[0])) {
$untilQuery = new FlowQuery($nextNodes);
$untilQuery->pushOperation('filter', [$arguments[0]]);

$until = $untilQuery->getContext();
$untilNodes = Nodes::fromArray(iterator_to_array($untilQuery));
}
/** @var array<int,mixed> $until */

if (isset($until[0]) && !empty($until[0])) {
$nextNodes = $nextNodes->until($until[0]);
if (isset($untilNodes) && !$untilNodes->isEmpty()) {
$nextNodes = $nextNodes->previousAll($untilNodes->first());
}

foreach ($nextNodes as $nextNode) {
if ($nextNode !== null
&& !isset($outputNodeIdentifiers[$nextNode->nodeAggregateId->value])) {
Expand All @@ -101,21 +101,4 @@ public function evaluate(FlowQuery $flowQuery, array $arguments)
$flowQuery->pushOperation('filter', [$arguments[1]]);
}
}

/**
* @param Node $contextNode The node for which the next nodes should be found
* @return Nodes The following nodes of $contextNode
*/
protected function getNextForNode(Node $contextNode): Nodes
{
$subgraph = $this->contentRepositoryRegistry->subgraphForNode($contextNode);

$parentNode = $subgraph->findParentNode($contextNode->nodeAggregateId);
if ($parentNode === null) {
return Nodes::createEmpty();
}

return $subgraph->findChildNodes($parentNode->nodeAggregateId, FindChildNodesFilter::create())
->nextAll($contextNode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/

use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindChildNodesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindPrecedingSiblingNodesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindSucceedingSiblingNodesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\Projection\ContentGraph\Nodes;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
Expand Down Expand Up @@ -69,7 +71,12 @@ public function evaluate(FlowQuery $flowQuery, array $arguments)
$output = [];
$outputNodeAggregateIds = [];
foreach ($flowQuery->getContext() as $contextNode) {
foreach ($this->getPrevForNode($contextNode) as $prevNode) {
$prevNodes = $this->contentRepositoryRegistry->subgraphForNode($contextNode)
->findPrecedingSiblingNodes(
$contextNode->nodeAggregateId,
FindPrecedingSiblingNodesFilter::create()
)->reverse();
foreach ($prevNodes as $prevNode) {
if ($prevNode !== null
&& !isset($outputNodeAggregateIds[$prevNode->nodeAggregateId->value])
) {
Expand All @@ -84,20 +91,4 @@ public function evaluate(FlowQuery $flowQuery, array $arguments)
$flowQuery->pushOperation('filter', $arguments);
}
}

/**
* @param Node $contextNode The node for which the preceding node should be found
* @return Nodes The preceding nodes of $contextNode
*/
protected function getPrevForNode(Node $contextNode): Nodes
{
$subgraph = $this->contentRepositoryRegistry->subgraphForNode($contextNode);
$parentNode = $subgraph->findParentNode($contextNode->nodeAggregateId);
if ($parentNode === null) {
return Nodes::createEmpty();
}

return $subgraph->findChildNodes($parentNode->nodeAggregateId, FindChildNodesFilter::create())
->previousAll($contextNode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindChildNodesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindPrecedingSiblingNodesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Eel\FlowQuery\FlowQuery;
Expand Down Expand Up @@ -68,10 +69,14 @@ public function evaluate(FlowQuery $flowQuery, array $arguments): void
$output = [];
$outputNodePaths = [];
foreach ($flowQuery->getContext() as $contextNode) {
$nextNode = $this->getPrevForNode($contextNode);
if ($nextNode !== null && !isset($outputNodePaths[$nextNode->nodeAggregateId->value])) {
$outputNodePaths[$nextNode->nodeAggregateId->value] = true;
$output[] = $nextNode;
$previousNode = $this->contentRepositoryRegistry->subgraphForNode($contextNode)
->findPrecedingSiblingNodes(
$contextNode->nodeAggregateId,
FindPrecedingSiblingNodesFilter::create()
)->first();
if ($previousNode !== null && !isset($outputNodePaths[$previousNode->nodeAggregateId->value])) {
$outputNodePaths[$previousNode->nodeAggregateId->value] = true;
$output[] = $previousNode;
}
}
$flowQuery->setContext($output);
Expand All @@ -80,20 +85,4 @@ public function evaluate(FlowQuery $flowQuery, array $arguments): void
$flowQuery->pushOperation('filter', $arguments);
}
}

/**
* @param Node $contextNode The node for which the preceding node should be found
* @return Node|null The preceeding node of $contextNode or NULL
*/
protected function getPrevForNode(Node $contextNode): ?Node
{
$subgraph = $this->contentRepositoryRegistry->subgraphForNode($contextNode);
$parentNode = $subgraph->findParentNode($contextNode->nodeAggregateId);
if ($parentNode === null) {
return null;
}

return $subgraph->findChildNodes($parentNode->nodeAggregateId, FindChildNodesFilter::create())
->previous($contextNode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/

use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindChildNodesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindPrecedingSiblingNodesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\Projection\ContentGraph\Nodes;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
Expand All @@ -33,7 +34,7 @@ class PrevUntilOperation extends AbstractOperation
*
* @var string
*/
protected static $shortName = 'nextUntil';
protected static $shortName = 'prevUntil';

/**
* {@inheritdoc}
Expand Down Expand Up @@ -70,22 +71,21 @@ public function evaluate(FlowQuery $flowQuery, array $arguments): void
{
$output = [];
$outputNodeIdentifiers = [];
$until = [];

foreach ($flowQuery->getContext() as $contextNode) {
$prevNodes = $this->getPrevForNode($contextNode);
$prevNodes = $this->contentRepositoryRegistry->subgraphForNode($contextNode)
->findPrecedingSiblingNodes(
$contextNode->nodeAggregateId,
FindPrecedingSiblingNodesFilter::create()
);
if (isset($arguments[0]) && !empty($arguments[0])) {
$untilQuery = new FlowQuery($prevNodes);
$untilQuery->pushOperation('filter', [$arguments[0]]);

$until = $untilQuery->getContext();
$untilNodes = Nodes::fromArray(iterator_to_array($untilQuery));
}
/** @var array<int,mixed> $until */

if (isset($until[0]) && !empty($until[0])) {
$prevNodes = $prevNodes->until($until[0]);
if (isset($untilNodes) && !$untilNodes->isEmpty()) {
$prevNodes = $prevNodes->previousAll($untilNodes->first())->reverse();
}

foreach ($prevNodes as $prevNode) {
if ($prevNode !== null &&
!isset($outputNodeIdentifiers[$prevNode->nodeAggregateId->value])) {
Expand All @@ -101,20 +101,4 @@ public function evaluate(FlowQuery $flowQuery, array $arguments): void
$flowQuery->pushOperation('filter', [$arguments[1]]);
}
}

/**
* @param Node $contextNode The node for which the next nodes should be found
* @return Nodes The following nodes of $contextNode
*/
protected function getPrevForNode(Node $contextNode): Nodes
{
$subgraph = $this->contentRepositoryRegistry->subgraphForNode($contextNode);
$parentNode = $subgraph->findParentNode($contextNode->nodeAggregateId);
if ($parentNode === null) {
return Nodes::createEmpty();
}

return $subgraph->findChildNodes($parentNode->nodeAggregateId, FindChildNodesFilter::create())
->previousAll($contextNode);
}
}

0 comments on commit 4940424

Please sign in to comment.