Skip to content

Commit d3ab79e

Browse files
committed
added Finder::files() & directories()
1 parent 3d811a0 commit d3ab79e

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

src/Utils/Finder.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,38 @@ public static function findDirectories(string|array $masks): static
7272

7373

7474
/**
75-
* Creates filtering group by mask & type selector.
75+
* Finds files matching the specified masks.
7676
*/
77-
private function select(array $masks, string $type): static
77+
public function files(string|array $masks): static
78+
{
79+
return $this->select((array) $masks, 'file');
80+
}
81+
82+
83+
/**
84+
* Finds directories matching the specified masks.
85+
*/
86+
public function directories(string|array $masks): static
87+
{
88+
return $this->select((array) $masks, 'dir');
89+
}
90+
91+
92+
private function select(array $masks, string $mode): static
7893
{
7994
foreach ($masks as $mask) {
8095
$mask = FileSystem::unixSlashes($mask);
81-
if ($mask === '') {
96+
if ($mode === 'dir') {
97+
$mask = rtrim($mask, '/');
98+
}
99+
if ($mask === '' || ($mode === 'file' && str_ends_with($mask, '/'))) {
82100
throw new Nette\InvalidArgumentException("Invalid mask '$mask'");
83101
}
84102
if (str_starts_with($mask, '**/')) {
85103
$mask = substr($mask, 3);
86104
}
87-
$this->find[] = [$mask, $type];
105+
$this->find[] = [$mask, $mode];
88106
}
89-
90107
return $this;
91108
}
92109

tests/Utils/Finder.basic.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ test('non-recursive file search', function () {
5454
});
5555

5656

57+
test('non-recursive file search alt', function () {
58+
$finder = (new Finder)->files('file.txt')->in('fixtures.finder');
59+
Assert::same(['fixtures.finder/file.txt'], export($finder));
60+
});
61+
62+
5763
test('recursive file search', function () {
5864
$finder = Finder::findFiles('file.txt')->from('fixtures.finder');
5965
Assert::same([
@@ -117,6 +123,14 @@ test('non-recursive directory search', function () {
117123
});
118124

119125

126+
test('non-recursive directory search alt', function () {
127+
$finder = (new Finder)->directories('subdir*')->in('fixtures.finder');
128+
Assert::same([
129+
'fixtures.finder/subdir',
130+
], export($finder));
131+
});
132+
133+
120134
test('recursive directory search', function () {
121135
$finder = Finder::findDirectories('subdir*')->from('fixtures.finder');
122136
Assert::same([

0 commit comments

Comments
 (0)