Skip to content

Commit

Permalink
added Finder::append()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 18, 2023
1 parent 5267333 commit 27251ce
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Utils/Finder.php
Expand Up @@ -37,6 +37,9 @@ class Finder implements \IteratorAggregate

/** @var \Closure[] */
private array $descentFilters = [];

/** @var array<string|self> */
private array $appends = [];
private bool $childFirst = false;

/** @var ?callable */
Expand Down Expand Up @@ -187,6 +190,20 @@ public function sortByName(): static
}


/**
* Adds the specified paths or appends a new finder that returns.
*/
public function append(string|array|null $paths = null): static
{
if ($paths === null) {
return $this->appends[] = new static;
}

$this->appends = array_merge($this->appends, (array) $paths);
return $this;
}


/********************* filtering ****************d*g**/


Expand Down Expand Up @@ -306,6 +323,15 @@ public function getIterator(): \Generator
foreach ($plan as $dir => $searches) {
yield from $this->traverseDir($dir, $searches);
}

foreach ($this->appends as $item) {
if ($item instanceof self) {
yield from $item->getIterator();
} else {
$item = FileSystem::platformSlashes($item);
yield $item => new FileInfo($item);
}
}
}


Expand Down
46 changes: 46 additions & 0 deletions tests/Utils/Finder.append.phpt
@@ -0,0 +1,46 @@
<?php

/**
* Test: Nette\Utils\Finder append.
*/

declare(strict_types=1);

use Nette\Utils\FileSystem;
use Nette\Utils\Finder;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


test('append finder', function () {
($finder = new Finder)
->files('file.txt')
->in('fixtures.finder')
->append()
->directories('subdir*')
->from('fixtures.finder')
->append()
->files('file.txt')
->from('fixtures.finder/*/subdir*');

$ds = DIRECTORY_SEPARATOR;
Assert::same([
"fixtures.finder{$ds}file.txt",
"fixtures.finder{$ds}subdir",
"fixtures.finder{$ds}subdir{$ds}subdir2",
"fixtures.finder{$ds}subdir{$ds}subdir2{$ds}file.txt",
], array_keys($finder->collect()));
});

test('append files', function () {
($finder = new Finder)
->append(__FILE__)
->append(FileSystem::unixSlashes(__DIR__));

Assert::equal([
__FILE__ => new Nette\Utils\FileInfo(__FILE__),
__DIR__ => new Nette\Utils\FileInfo(__DIR__),
], $finder->collect());
});

0 comments on commit 27251ce

Please sign in to comment.