Skip to content

Commit

Permalink
added Finder::files() & directories()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 2, 2023
1 parent 7828eba commit d51381f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/Utils/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,39 @@ public static function findDirectories(string|array $masks): static
}


private function addMask(array $masks, string $type): static
/**
* Finds files matching the specified masks.
*/
public function files(string|array $masks): static
{
return $this->addMask((array) $masks, 'file');
}


/**
* Finds directories matching the specified masks.
*/
public function directories(string|array $masks): static
{
return $this->addMask((array) $masks, 'dir');
}


private function addMask(array $masks, string $mode): static
{
foreach ($masks as $mask) {
$mask = FileSystem::unixSlashes($mask);
if ($mask === '') {
if ($mode === 'dir') {
$mask = rtrim($mask, '/');
}
if ($mask === '' || ($mode === 'file' && str_ends_with($mask, '/'))) {
throw new Nette\InvalidArgumentException("Invalid mask '$mask'");
}
if (str_starts_with($mask, '**/')) {
$mask = substr($mask, 3);
}
$this->find[] = [$mask, $type];
$this->find[] = [$mask, $mode];
}

return $this;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Utils/Finder.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ test('non-recursive file search', function () {
});


test('non-recursive file search alt', function () {
$finder = (new Finder)->files('file.txt')->in('fixtures.finder');
Assert::same(['fixtures.finder/file.txt'], export($finder));
});


test('recursive file search', function () {
$finder = Finder::findFiles('file.txt')->from('fixtures.finder');
Assert::same([
Expand Down Expand Up @@ -117,6 +123,14 @@ test('non-recursive directory search', function () {
});


test('non-recursive directory search alt', function () {
$finder = (new Finder)->directories('subdir*')->in('fixtures.finder');
Assert::same([
'fixtures.finder/subdir',
], export($finder));
});


test('recursive directory search', function () {
$finder = Finder::findDirectories('subdir*')->from('fixtures.finder');
Assert::same([
Expand Down

0 comments on commit d51381f

Please sign in to comment.