Skip to content

Commit 748b8a5

Browse files
committed
added Finder::sortByName()
1 parent d8f1495 commit 748b8a5

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/Utils/Finder.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Finder implements \IteratorAggregate
3030
private array $batches = [];
3131
private FinderBatch $batch;
3232
private bool $selfFirst = true;
33+
private bool $sort = false;
3334
private int $maxDepth = -1;
3435
private bool $ignoreUnreadableDirs = true;
3536

@@ -168,6 +169,13 @@ public function ignoreUnreadableDirs(bool $state = true): static
168169
}
169170

170171

172+
public function sortByName(bool $state = true): static
173+
{
174+
$this->sort = $state;
175+
return $this;
176+
}
177+
178+
171179
/**
172180
* Starts defining a new search group.
173181
*/
@@ -296,6 +304,11 @@ public function toArray(): array
296304
public function getIterator(): \Generator
297305
{
298306
$groups = $this->prepare();
307+
308+
if ($this->sort) {
309+
ksort($groups, SORT_NATURAL);
310+
}
311+
299312
foreach ($groups as $dir => $searches) {
300313
yield from $this->traverseDir($dir, $searches);
301314
}
@@ -324,6 +337,11 @@ private function traverseDir(string $dir, array $searches, array $subDirs = []):
324337
}
325338
}
326339

340+
if ($this->sort) {
341+
$items = iterator_to_array($items);
342+
natsort($items);
343+
}
344+
327345
$relativePath = implode(DIRECTORY_SEPARATOR, $subDirs);
328346

329347
foreach ($items as $pathName) {

tests/Utils/Finder.sort.phpt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Utils\Finder sorting.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Utils\Finder;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
function export($iterator)
17+
{
18+
$arr = [];
19+
foreach ($iterator as $key => $value) {
20+
$arr[] = strtr($key, '\\', '/');
21+
}
22+
23+
return $arr;
24+
}
25+
26+
27+
test('basic', function () {
28+
$finder = Finder::find('*')
29+
->from('files')
30+
->sortByName();
31+
32+
Assert::same([
33+
'files/file.txt',
34+
'files/images',
35+
'files/images/logo.gif',
36+
'files/subdir',
37+
'files/subdir/file.txt',
38+
'files/subdir/readme',
39+
'files/subdir/subdir2',
40+
'files/subdir/subdir2/file.txt',
41+
], export($finder));
42+
43+
$finder->childFirst();
44+
Assert::same([
45+
'files/file.txt',
46+
'files/images/logo.gif',
47+
'files/images',
48+
'files/subdir/file.txt',
49+
'files/subdir/readme',
50+
'files/subdir/subdir2/file.txt',
51+
'files/subdir/subdir2',
52+
'files/subdir',
53+
], export($finder));
54+
});
55+
56+
57+
test('and', function () {
58+
$finder = Finder::find('*')->in('files/subdir')
59+
->and()
60+
->files('*')->in('files/images')
61+
->sortByName();
62+
63+
Assert::same([
64+
'files/images/logo.gif',
65+
'files/subdir/file.txt',
66+
'files/subdir/readme',
67+
'files/subdir/subdir2',
68+
], export($finder));
69+
});

0 commit comments

Comments
 (0)