apply the sort callback on the whole search result
This commit is contained in:
parent
0304f754b3
commit
126105146f
@ -624,7 +624,13 @@ class Finder implements \IteratorAggregate, \Countable
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (1 === \count($this->dirs) && 0 === \count($this->iterators)) {
|
if (1 === \count($this->dirs) && 0 === \count($this->iterators)) {
|
||||||
return $this->searchInDirectory($this->dirs[0]);
|
$iterator = $this->searchInDirectory($this->dirs[0]);
|
||||||
|
|
||||||
|
if ($this->sort || $this->reverseSorting) {
|
||||||
|
$iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $iterator;
|
||||||
}
|
}
|
||||||
|
|
||||||
$iterator = new \AppendIterator();
|
$iterator = new \AppendIterator();
|
||||||
@ -636,6 +642,10 @@ class Finder implements \IteratorAggregate, \Countable
|
|||||||
$iterator->append($it);
|
$iterator->append($it);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($this->sort || $this->reverseSorting) {
|
||||||
|
$iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
|
||||||
|
}
|
||||||
|
|
||||||
return $iterator;
|
return $iterator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -782,11 +792,6 @@ class Finder implements \IteratorAggregate, \Countable
|
|||||||
$iterator = new Iterator\PathFilterIterator($iterator, $this->paths, $notPaths);
|
$iterator = new Iterator\PathFilterIterator($iterator, $this->paths, $notPaths);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->sort || $this->reverseSorting) {
|
|
||||||
$iteratorAggregate = new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting);
|
|
||||||
$iterator = $iteratorAggregate->getIterator();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $iterator;
|
return $iterator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -832,6 +832,39 @@ class FinderTest extends Iterator\RealIteratorTestCase
|
|||||||
]), $finder->in(self::$tmpDir)->getIterator());
|
]), $finder->in(self::$tmpDir)->getIterator());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSortAcrossDirectories()
|
||||||
|
{
|
||||||
|
$finder = $this->buildFinder()
|
||||||
|
->in([
|
||||||
|
self::$tmpDir,
|
||||||
|
self::$tmpDir.'/qux',
|
||||||
|
self::$tmpDir.'/foo',
|
||||||
|
])
|
||||||
|
->depth(0)
|
||||||
|
->files()
|
||||||
|
->filter(static function (\SplFileInfo $file): bool {
|
||||||
|
return '' !== $file->getExtension();
|
||||||
|
})
|
||||||
|
->sort(static function (\SplFileInfo $a, \SplFileInfo $b): int {
|
||||||
|
return strcmp($a->getExtension(), $b->getExtension()) ?: strcmp($a->getFilename(), $b->getFilename());
|
||||||
|
})
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->assertOrderedIterator($this->toAbsolute([
|
||||||
|
'qux_0_1.php',
|
||||||
|
'qux_1000_1.php',
|
||||||
|
'qux_1002_0.php',
|
||||||
|
'qux_10_2.php',
|
||||||
|
'qux_12_0.php',
|
||||||
|
'qux_2_0.php',
|
||||||
|
'test.php',
|
||||||
|
'qux/baz_100_1.py',
|
||||||
|
'qux/baz_1_2.py',
|
||||||
|
'test.py',
|
||||||
|
'foo/bar.tmp',
|
||||||
|
]), $finder->getIterator());
|
||||||
|
}
|
||||||
|
|
||||||
public function testFilter()
|
public function testFilter()
|
||||||
{
|
{
|
||||||
$finder = $this->buildFinder();
|
$finder = $this->buildFinder();
|
||||||
|
@ -31,7 +31,8 @@ abstract class IteratorTestCase extends TestCase
|
|||||||
|
|
||||||
protected function assertOrderedIterator($expected, \Traversable $iterator)
|
protected function assertOrderedIterator($expected, \Traversable $iterator)
|
||||||
{
|
{
|
||||||
$values = array_map(function (\SplFileInfo $fileinfo) { return $fileinfo->getPathname(); }, iterator_to_array($iterator));
|
$values = array_map(function (\SplFileInfo $fileinfo) { return str_replace('/', \DIRECTORY_SEPARATOR, $fileinfo->getPathname()); }, iterator_to_array($iterator));
|
||||||
|
$expected = array_map(function ($path) { return str_replace('/', \DIRECTORY_SEPARATOR, $path); }, $expected);
|
||||||
|
|
||||||
$this->assertEquals($expected, array_values($values));
|
$this->assertEquals($expected, array_values($values));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user