8000 bug #39703 [Finder] apply the sort callback on the whole search resul… · symfony/symfony@1dead9a · GitHub
[go: up one dir, main page]

Skip to content

Commit 1dead9a

Browse files
committed
bug #39703 [Finder] apply the sort callback on the whole search result (xabbuh)
This PR was merged into the 4.4 branch. Discussion ---------- [Finder] apply the sort callback on the whole search result | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #34879 | License | MIT | Doc PR | Commits ------- 1261051 apply the sort callback on the whole search result
2 parents ef11c2c + 1261051 commit 1dead9a

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

src/Symfony/Component/Finder/Finder.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,13 @@ public function getIterator()
624624
}
625625

626626
if (1 === \count($this->dirs) && 0 === \count($this->iterators)) {
627-
return $this->searchInDirectory($this->dirs[0]);
627+
$iterator = $this->searchInDirectory($this->dirs[0]);
628+
629+
if ($this->sort || $this->reverseSorting) {
630+
$iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
631+
}
632+
633+
return $iterator;
628634
}
629635

630636
$iterator = new \AppendIterator();
@@ -636,6 +642,10 @@ public function getIterator()
636642
$iterator->append($it);
637643
}
638644

645+
if ($this->sort || $this->reverseSorting) {
646+
$iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
647+
}
648+
639649
return $iterator;
640650
}
641651

@@ -782,11 +792,6 @@ private function searchInDirectory(string $dir): \Iterator
782792
$iterator = new Iterator\PathFilterIterator($iterator, $this->paths, $notPaths);
783793
}
784794

785-
if ($this->sort || $this->reverseSorting) {
786-
$iteratorAggregate = new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting);
787-
$iterator = $iteratorAggregate->getIterator();
788-
}
789-
790795
return $iterator;
791796
}
792797

src/Symfony/Component/Finder/Tests/FinderTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,39 @@ public function testSort()
832832
]), $finder->in(self::$tmpDir)->getIterator());
833833
}
834834

835+
public function testSortAcrossDirectories()
836+
{
837+
$finder = $this->buildFinder()
838+
->in([
839+
self::$tmpDir,
840+
self::$tmpDir.'/qux',
841+
self::$tmpDir.'/foo',
842+
])
843+
->depth(0)
844+
->files()
845+
->filter(static function (\SplFileInfo $file): bool {
846+
return '' !== $file->getExtension();
847+
})
848+
->sort(static function (\SplFileInfo $a, \SplFileInfo $b): int {
849+
return strcmp($a->getExtension(), $b->getExtension()) ?: strcmp($a->getFilename(), $b->getFilename());
850+
})
851+
;
852+
853+
$this->assertOrderedIterator($this->toAbsolute([
854+
'qux_0_1.php',
855+
'qux_1000_1.php',
856+
'qux_1002_0.php',
857+
'qux_10_2.php',
858+
'qux_12_0.php',
859+
'qux_2_0.php',
860+
'test.php',
861+
'qux/baz_100_1.py',
862+
'qux/baz_1_2.py',
863+
'test.py',
864+
'foo/bar.tmp',
865+
]), $finder->getIterator());
866+
}
867+
835868
public function testFilter()
836869
{
837870
$finder = $this->buildFinder();

src/Symfony/Component/Finder/Tests/Iterator/IteratorTestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ protected function assertIterator($expected, \Traversable $iterator)
3131

3232
protected function assertOrderedIterator($expected, \Traversable $iterator)
3333
{
34-
$values = array_map(function (\SplFileInfo $fileinfo) { return $fileinfo->getPathname(); }, iterator_to_array($iterator));
34+
$values = array_map(function (\SplFileInfo $fileinfo) { return str_replace('/', \DIRECTORY_SEPARATOR, $fileinfo->getPathname()); }, iterator_to_array($iterator));
35+
$expected = array_map(function ($path) { return str_replace('/', \DIRECTORY_SEPARATOR, $path); }, $expected);
3536

3637
$this->assertEquals($expected, array_values($values));
3738
}

0 commit comments

Comments
 (0)
0