Skip to content

Commit 969d0f8

Browse files
committed
!!! TASK: Skip bubble sort of all child nodes in favor of only sorting the added node
1 parent acc1126 commit 969d0f8

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

Classes/Archivist.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function organizeNode(NodeInterface $triggeringNode, array $sortingInstru
119119
}
120120

121121
if (isset($sortingInstructions['sorting'])) {
122-
$this->sortingService->sortChildren($affectedNode->getParent(), $sortingInstructions['sorting'], null);
122+
$this->sortingService->sortChildren($affectedNode, $sortingInstructions['sorting'], null);
123123
$this->sortedNodeInstructions[$affectedNode->getIdentifier()] = $sortingInstructions['sorting'];
124124
}
125125

@@ -153,7 +153,7 @@ public function restorePathIfOrganizedDuringThisRequest(NodeInterface $node): bo
153153
}
154154

155155
if (isset($this->sortedNodeInstructions[$node->getIdentifier()])) {
156-
$this->sortingService->sortChildren($node->getParent(), $this->sortedNodeInstructions[$node->getIdentifier()] , null);
156+
$this->sortingService->sortChildren($node, $this->sortedNodeInstructions[$node->getIdentifier()] , null);
157157
return true;
158158
}
159159

Classes/Service/SortingService.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,42 +31,33 @@ class SortingService
3131
protected $logger;
3232

3333
/**
34-
* @param NodeInterface $parentNode
34+
* @param NodeInterface $node
3535
* @param string $eelOrProperty
3636
* @param string $nodeTypeFilter
3737
*/
38-
public function sortChildren(NodeInterface $parentNode, string $eelOrProperty, $nodeTypeFilter)
38+
public function sortChildren(NodeInterface $node, string $eelOrProperty, $nodeTypeFilter)
3939
{
4040
if ($this->eelEvaluationService->isValidExpression($eelOrProperty)) {
4141
$eelExpression = $eelOrProperty;
4242
} else {
4343
$eelExpression = sprintf('${String.toLowerCase(q(a).property("%s")) < String.toLowerCase(q(b).property("%s"))}', $eelOrProperty, $eelOrProperty);
4444
}
45-
$this->sortChildNodesByEelExpression($parentNode, $eelExpression, $nodeTypeFilter);
45+
46+
$this->moveNodeToCorrectPosition($node, $eelExpression, $nodeTypeFilter);
4647
}
4748

4849
/**
49-
* @param NodeInterface $parenNode
50+
* @param NodeInterface $nodeToBeSorted
5051
* @param string $eelExpression
51-
* @param string $nodeTypeFilter
52-
* @return void
52+
* @param $nodeTypeFilter
5353
*/
54-
protected function sortChildNodesByEelExpression(NodeInterface $parenNode, string $eelExpression, $nodeTypeFilter)
55-
{
56-
$nodes = $parenNode->getChildNodes($nodeTypeFilter);
57-
58-
foreach ($nodes as $nodeA) {
59-
60-
/** @var NodeInterface $nodeB */
61-
foreach ($nodes as $nodeB) {
62-
if ($this->eelEvaluationService->evaluate($eelExpression, ['a' => $nodeA, 'b' => $nodeB])) {
54+
protected function moveNodeToCorrectPosition(NodeInterface $nodeToBeSorted, string $eelExpression, $nodeTypeFilter) {
55+
$nodes = $nodeToBeSorted->getParent()->getChildNodes($nodeTypeFilter);
6356

64-
if ($nodeB !== $nodeA) {
65-
$this->logger->log(sprintf('Moving node %s before %s', $nodeA->getPath(), $nodeB->getPath()), LOG_DEBUG);
66-
$nodeA->moveBefore($nodeB);
67-
break;
68-
}
69-
}
57+
foreach ($nodes as $node) {
58+
if ($this->eelEvaluationService->evaluate($eelExpression, ['a' => $nodeToBeSorted, 'b' => $node])) {
59+
$nodeToBeSorted->moveBefore($node);
60+
break;
7061
}
7162
}
7263
}

0 commit comments

Comments
 (0)