3declare(strict_types=1);
14use InvalidArgumentException;
15use Iterator as PhpIterator;
18use RecursiveIteratorIterator;
19use ReturnTypeWillChange;
44final class Finder implements IteratorAggregate, Countable
51 private array
$vcsPatterns = [
'.svn',
'_svn',
'CVS',
'_darcs',
'.arch-params',
'.monotone',
'.bzr',
'.git',
'.hg'];
105 public function exclude(array $directories): self
107 array_walk($directories,
static function ($directory):
void {
108 if (!is_string($directory)) {
109 if (is_object($directory)) {
110 throw new InvalidArgumentException(sprintf(
'Invalid directory given: %s', get_class($directory)));
113 throw new InvalidArgumentException(sprintf(
'Invalid directory given: %s', gettype($directory)));
117 $clone = clone $this;
118 $clone->exclude = array_merge($clone->exclude, $directories);
128 public function in(array $directories): self
130 array_walk($directories,
static function ($directory):
void {
131 if (!is_string($directory)) {
132 if (is_object($directory)) {
133 throw new InvalidArgumentException(sprintf(
'Invalid directory given: %s', get_class($directory)));
136 throw new InvalidArgumentException(sprintf(
'Invalid directory given: %s', gettype($directory)));
140 $clone = clone $this;
141 $clone->dirs = array_unique(array_merge($clone->dirs, $directories));
160 $clone = clone $this;
182 public function date(
string $date): self
184 $clone = clone $this;
210 $clone = clone $this;
212 foreach (
$sizes as $size) {
221 $clone = clone $this;
222 $clone->reverseSorting =
true;
229 $clone = clone $this;
246 array_walk($pattern,
static function ($p):
void {
247 if (!is_string($p)) {
249 throw new InvalidArgumentException(sprintf(
'Invalid pattern given: %s', get_class($p)));
252 throw new InvalidArgumentException(sprintf(
'Invalid pattern given: %s', gettype($p)));
256 $clone = clone $this;
257 foreach ($pattern as $p) {
258 $clone->vcsPatterns[] = $p;
261 $clone->vcsPatterns = array_unique($clone->vcsPatterns);
273 public function sort(Closure $closure): self
275 $clone = clone $this;
276 $clone->sort = $closure;
281 public function sortByName(
bool $useNaturalSort =
false): self
283 $clone = clone $this;
285 if ($useNaturalSort) {
294 $clone = clone $this;
302 $clone = clone $this;
315 public function append(iterable $iterator): self
317 $clone = clone $this;
319 if ($iterator instanceof IteratorAggregate) {
320 $clone->iterators[] = $iterator->getIterator();
321 } elseif ($iterator instanceof PhpIterator) {
322 $clone->iterators[] = $iterator;
323 } elseif ($iterator instanceof Traversable || is_array($iterator)) {
324 $it =
new ArrayIterator();
325 foreach ($iterator as $file) {
329 throw new InvalidArgumentException(
'Finder::append() method wrong argument type in passed iterator.');
332 $clone->iterators[] = $it;
334 throw new InvalidArgumentException(
'Finder::append() method wrong argument type.');
342 if (self::IGNORE_VCS_FILES === (self::IGNORE_VCS_FILES & $this->ignore)) {
352 $iterator =
new RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
377 $iterator = $iteratorAggregate->getIterator();
388 #[\ReturnTypeWillChange]
391 if (0 ===
count($this->dirs) && 0 ===
count($this->iterators)) {
392 throw new LogicException(
'You must call one of in() or append() methods before iterating over a Finder.');
395 if (1 ===
count($this->dirs) && 0 ===
count($this->iterators)) {
399 $iterator =
new AppendIterator();
400 foreach ($this->dirs as $dir) {
404 foreach ($this->iterators as $it) {
405 $iterator->append($it);
append(iterable $iterator)
Appends an existing set of files/directories to the finder.
searchInDirectory(string $dir)
size($sizes)
Adds tests for file sizes.
__construct(Filesystem $filesystem)
exclude(array $directories)
sort(Closure $closure)
Sorts files and directories by an anonymous function.
ILIAS Filesystem Filesystem $filesystem
addVCSPattern(array $pattern)
ignoreVCS(bool $ignoreVCS)
depth($level)
Adds tests for the directory depth.
date(string $date)
Adds tests for file dates.
sortByName(bool $useNaturalSort=false)
const SORT_BY_NAME_NATURAL
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...