ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ILIAS\Filesystem\Finder\Finder Class Reference
+ Inheritance diagram for ILIAS\Filesystem\Finder\Finder:
+ Collaboration diagram for ILIAS\Filesystem\Finder\Finder:

Public Member Functions

 __construct (private Filesystem $filesystem)
 
 files ()
 
 directories ()
 
 allTypes ()
 
 exclude (array $directories)
 
 in (array $directories)
 
 depth (string|int $level)
 Adds tests for the directory depth. More...
 
 date (string $date)
 Adds tests for file dates. More...
 
 size (string|int|array $sizes)
 Adds tests for file sizes. More...
 
 reverseSorting ()
 
 ignoreVCS (bool $ignoreVCS)
 
 addVCSPattern (array $pattern)
 
 sort (Closure $closure)
 Sorts files and directories by an anonymous function. More...
 
 sortByName (bool $useNaturalSort=false)
 
 sortByType ()
 
 sortByTime ()
 
 append (iterable $iterator)
 Appends an existing set of files/directories to the finder. More...
 
 getIterator ()
 
 count ()
 

Protected Attributes

array $dirs = []
 

Private Member Functions

 searchInDirectory (string $dir)
 

Private Attributes

const IGNORE_VCS_FILES = 1
 
const IGNORE_DOT_FILES = 2
 
array $vcsPatterns = ['.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg']
 
array $iterators = []
 
array $exclude = []
 
int $ignore = 0
 
int $mode = Iterator\FileTypeFilterIterator::ALL
 
bool $reverseSorting = false
 
array $dates = []
 
array $sizes = []
 
array $depths = []
 
 $sort = SortableIterator::SORT_BY_NONE
 

Detailed Description

Definition at line 44 of file Finder.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Filesystem\Finder\Finder::__construct ( private Filesystem  $filesystem)

Definition at line 68 of file Finder.php.

69  {
70  $this->ignore = self::IGNORE_VCS_FILES|self::IGNORE_DOT_FILES;
71  }

Member Function Documentation

◆ addVCSPattern()

ILIAS\Filesystem\Finder\Finder::addVCSPattern ( array  $pattern)
Parameters
string[]$pattern
Exceptions
InvalidArgumentException

Definition at line 225 of file Finder.php.

225  : self
226  {
227  array_walk($pattern, static function ($p): void {
228  if (!is_string($p)) {
229  throw new InvalidArgumentException(sprintf('Invalid pattern given: %s', $p::class));
230  }
231  });
232 
233  $clone = clone $this;
234  foreach ($pattern as $p) {
235  $clone->vcsPatterns[] = $p;
236  }
237 
238  $clone->vcsPatterns = array_unique($clone->vcsPatterns);
239 
240  return $clone;
241  }

◆ allTypes()

ILIAS\Filesystem\Finder\Finder::allTypes ( )

Definition at line 89 of file Finder.php.

References ILIAS\Filesystem\Finder\Iterator\FileTypeFilterIterator\ALL.

89  : self
90  {
91  $clone = clone $this;
93 
94  return $clone;
95  }

◆ append()

ILIAS\Filesystem\Finder\Finder::append ( iterable  $iterator)

Appends an existing set of files/directories to the finder.

The set can be another Finder, an Iterator, an IteratorAggregate, or even a plain array.

Exceptions
InvalidArgumentExceptionwhen the given argument is not iterable

Definition at line 288 of file Finder.php.

288  : self
289  {
290  $clone = clone $this;
291 
292  if ($iterator instanceof IteratorAggregate) {
293  $clone->iterators[] = $iterator->getIterator();
294  } elseif ($iterator instanceof PhpIterator) {
295  $clone->iterators[] = $iterator;
296  } elseif (is_iterable($iterator)) {
297  $it = new ArrayIterator();
298  foreach ($iterator as $file) {
299  if ($file instanceof MetadataType) {
300  $it->append($file);
301  } else {
302  throw new InvalidArgumentException(
303  'Finder::append() method wrong argument type in passed iterator.'
304  );
305  }
306  }
307  $clone->iterators[] = $it;
308  } else {
309  throw new InvalidArgumentException('Finder::append() method wrong argument type.');
310  }
311 
312  return $clone;
313  }

◆ count()

ILIAS\Filesystem\Finder\Finder::count ( )

Definition at line 389 of file Finder.php.

References ILIAS\Filesystem\Finder\Finder\getIterator().

Referenced by ILIAS\Filesystem\Finder\Finder\getIterator().

389  : int
390  {
391  return iterator_count($this->getIterator());
392  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ date()

ILIAS\Filesystem\Finder\Finder::date ( string  $date)

Adds tests for file dates.

The date must be something that strtotime() is able to parse:

$finder->date('since yesterday');
$finder->date('until 2 days ago');
$finder->date('> now - 2 hours');
$finder->date('>= 2005-10-15');
Parameters
string$dateA date range string
See also
strtotime
DateRangeFilterIterator
DateComparator
::getTimestamp()

Definition at line 167 of file Finder.php.

167  : self
168  {
169  $clone = clone $this;
170  $clone->dates[] = new Comparator\DateComparator($date);
171 
172  return $clone;
173  }

◆ depth()

ILIAS\Filesystem\Finder\Finder::depth ( string|int  $level)

Adds tests for the directory depth.

Usage:

$finder->depth('> 1') // the Finder will start matching at level 1.
$finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point.
Parameters
string | int$levelThe depth level expression
See also
DepthRangeFilterIterator
NumberComparator

Definition at line 144 of file Finder.php.

144  : self
145  {
146  $clone = clone $this;
147  $clone->depths[] = new Comparator\NumberComparator((string) $level);
148 
149  return $clone;
150  }

◆ directories()

ILIAS\Filesystem\Finder\Finder::directories ( )

Definition at line 81 of file Finder.php.

References ILIAS\Filesystem\Finder\Iterator\FileTypeFilterIterator\ONLY_DIRECTORIES.

81  : self
82  {
83  $clone = clone $this;
85 
86  return $clone;
87  }

◆ exclude()

ILIAS\Filesystem\Finder\Finder::exclude ( array  $directories)
Parameters
string[]$directories
Exceptions
InvalidArgumentException

Definition at line 101 of file Finder.php.

Referenced by ILIAS\Filesystem\Finder\Finder\searchInDirectory().

101  : self
102  {
103  array_walk($directories, static function ($directory): void {
104  if (!is_string($directory)) {
105  throw new InvalidArgumentException(sprintf('Invalid directory given: %s', $directory::class));
106  }
107  });
108 
109  $clone = clone $this;
110  $clone->exclude = array_merge($clone->exclude, $directories);
111 
112  return $clone;
113  }
+ Here is the caller graph for this function:

◆ files()

◆ getIterator()

ILIAS\Filesystem\Finder\Finder::getIterator ( )

Returns
PhpIterator|Metadata[]
Exceptions
LogicException

Definition at line 364 of file Finder.php.

References ILIAS\Filesystem\Finder\Finder\count(), and ILIAS\Filesystem\Finder\Finder\searchInDirectory().

Referenced by ILIAS\Filesystem\Finder\Finder\count().

364  : \Iterator
365  {
366  if ([] === $this->dirs && [] === $this->iterators) {
367  throw new LogicException('You must call one of in() or append() methods before iterating over a Finder.');
368  }
369 
370  if (1 === count($this->dirs) && [] === $this->iterators) {
371  return $this->searchInDirectory($this->dirs[0]);
372  }
373 
374  $iterator = new AppendIterator();
375  foreach ($this->dirs as $dir) {
376  $iterator->append($this->searchInDirectory($dir));
377  }
378 
379  foreach ($this->iterators as $it) {
380  $iterator->append($it);
381  }
382 
383  return $iterator;
384  }
searchInDirectory(string $dir)
Definition: Finder.php:315
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ignoreVCS()

ILIAS\Filesystem\Finder\Finder::ignoreVCS ( bool  $ignoreVCS)

Definition at line 209 of file Finder.php.

References ILIAS\Filesystem\Finder\Finder\IGNORE_VCS_FILES.

209  : self
210  {
211  $clone = clone $this;
212  if ($ignoreVCS) {
213  $clone->ignore |= self::IGNORE_VCS_FILES;
214  } else {
215  $clone->ignore &= ~self::IGNORE_VCS_FILES;
216  }
217 
218  return $clone;
219  }

◆ in()

ILIAS\Filesystem\Finder\Finder::in ( array  $directories)
Parameters
string[]$directories
Exceptions
InvalidArgumentException

Definition at line 119 of file Finder.php.

119  : self
120  {
121  array_walk($directories, static function ($directory): void {
122  if (!is_string($directory)) {
123  throw new InvalidArgumentException(sprintf('Invalid directory given: %s', $directory::class));
124  }
125  });
126 
127  $clone = clone $this;
128  $clone->dirs = array_unique(array_merge($clone->dirs, $directories));
129 
130  return $clone;
131  }

◆ reverseSorting()

ILIAS\Filesystem\Finder\Finder::reverseSorting ( )

Definition at line 201 of file Finder.php.

Referenced by ILIAS\Filesystem\Finder\Finder\searchInDirectory().

201  : self
202  {
203  $clone = clone $this;
204  $clone->reverseSorting = true;
205 
206  return $clone;
207  }
+ Here is the caller graph for this function:

◆ searchInDirectory()

ILIAS\Filesystem\Finder\Finder::searchInDirectory ( string  $dir)
private

Definition at line 315 of file Finder.php.

References ILIAS\Filesystem\Finder\Finder\exclude(), ILIAS\Repository\filesystem(), ILIAS\Filesystem\Finder\Finder\reverseSorting(), and ILIAS\Filesystem\Finder\Finder\sort().

Referenced by ILIAS\Filesystem\Finder\Finder\getIterator().

315  : \Traversable
316  {
317  if (self::IGNORE_VCS_FILES === (self::IGNORE_VCS_FILES&$this->ignore)) {
318  $this->exclude = array_merge($this->exclude, $this->vcsPatterns);
319  }
320 
321  $iterator = new Iterator\RecursiveDirectoryIterator($this->filesystem, $dir);
322 
323  if ($this->exclude) {
324  $iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
325  }
326 
327  $iterator = new RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
328 
329  if ($this->depths) {
330  $iterator = new Iterator\DepthRangeFilterIterator($iterator, $this->depths);
331  }
332 
333  if ($this->mode !== 0) {
334  $iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode);
335  }
336 
337  if ($this->dates) {
338  $iterator = new Iterator\DateRangeFilterIterator($this->filesystem, $iterator, $this->dates);
339  }
340 
341  if ($this->sizes) {
342  $iterator = new Iterator\SizeRangeFilterIterator($this->filesystem, $iterator, $this->sizes);
343  }
344 
345  if ($this->sort || $this->reverseSorting) {
346  $iteratorAggregate = new Iterator\SortableIterator(
347  $this->filesystem,
348  $iterator,
349  $this->sort,
350  $this->reverseSorting
351  );
352  $iterator = $iteratorAggregate->getIterator();
353  }
354 
355  return $iterator;
356  }
sort(Closure $closure)
Sorts files and directories by an anonymous function.
Definition: Finder.php:248
exclude(array $directories)
Definition: Finder.php:101
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ size()

ILIAS\Filesystem\Finder\Finder::size ( string|int|array  $sizes)

Adds tests for file sizes.

$finder->size('> 10K'); $finder->size('<= 1Ki'); $finder->size(4); $finder->size(['> 10K', '< 20K'])

Parameters
string|int|string[]|int[]$sizes A size range string or an integer or an array of size ranges
See also
SizeRangeFilterIterator
NumberComparator
::getSize()

Definition at line 188 of file Finder.php.

References ILIAS\Filesystem\Finder\Finder\$sizes.

188  : self
189  {
190  $sizes = is_array($sizes) ? $sizes : [$sizes];
191 
192  $clone = clone $this;
193 
194  foreach ($sizes as $size) {
195  $clone->sizes[] = new Comparator\NumberComparator((string) $size);
196  }
197 
198  return $clone;
199  }

◆ sort()

ILIAS\Filesystem\Finder\Finder::sort ( Closure  $closure)

Sorts files and directories by an anonymous function.

The anonymous function receives two Metadata instances to compare. This can be slow as all the matching files and directories must be retrieved for comparison.

Definition at line 248 of file Finder.php.

Referenced by ILIAS\Filesystem\Finder\Iterator\SortableIterator\__construct(), ILIAS\Filesystem\Finder\Iterator\SortableIterator\getIterator(), and ILIAS\Filesystem\Finder\Finder\searchInDirectory().

248  : self
249  {
250  $clone = clone $this;
251  $clone->sort = $closure;
252 
253  return $clone;
254  }
+ Here is the caller graph for this function:

◆ sortByName()

ILIAS\Filesystem\Finder\Finder::sortByName ( bool  $useNaturalSort = false)

Definition at line 256 of file Finder.php.

References ILIAS\Filesystem\Finder\Iterator\SortableIterator\SORT_BY_NAME, and ILIAS\Filesystem\Finder\Iterator\SortableIterator\SORT_BY_NAME_NATURAL.

256  : self
257  {
258  $clone = clone $this;
260  if ($useNaturalSort) {
262  }
263 
264  return $clone;
265  }

◆ sortByTime()

ILIAS\Filesystem\Finder\Finder::sortByTime ( )

Definition at line 275 of file Finder.php.

References ILIAS\Filesystem\Finder\Iterator\SortableIterator\SORT_BY_TIME.

275  : self
276  {
277  $clone = clone $this;
279 
280  return $clone;
281  }

◆ sortByType()

ILIAS\Filesystem\Finder\Finder::sortByType ( )

Definition at line 267 of file Finder.php.

References ILIAS\Filesystem\Finder\Iterator\SortableIterator\SORT_BY_TYPE.

267  : self
268  {
269  $clone = clone $this;
271 
272  return $clone;
273  }

Field Documentation

◆ $dates

array ILIAS\Filesystem\Finder\Finder::$dates = []
private

Definition at line 60 of file Finder.php.

◆ $depths

array ILIAS\Filesystem\Finder\Finder::$depths = []
private

Definition at line 64 of file Finder.php.

◆ $dirs

array ILIAS\Filesystem\Finder\Finder::$dirs = []
protected

Definition at line 53 of file Finder.php.

◆ $exclude

array ILIAS\Filesystem\Finder\Finder::$exclude = []
private

Definition at line 55 of file Finder.php.

◆ $ignore

int ILIAS\Filesystem\Finder\Finder::$ignore = 0
private

Definition at line 56 of file Finder.php.

◆ $iterators

array ILIAS\Filesystem\Finder\Finder::$iterators = []
private

Definition at line 51 of file Finder.php.

◆ $mode

int ILIAS\Filesystem\Finder\Finder::$mode = Iterator\FileTypeFilterIterator::ALL
private

Definition at line 57 of file Finder.php.

◆ $reverseSorting

bool ILIAS\Filesystem\Finder\Finder::$reverseSorting = false
private

Definition at line 58 of file Finder.php.

◆ $sizes

array ILIAS\Filesystem\Finder\Finder::$sizes = []
private

Definition at line 62 of file Finder.php.

Referenced by ILIAS\Filesystem\Finder\Finder\size().

◆ $sort

ILIAS\Filesystem\Finder\Finder::$sort = SortableIterator::SORT_BY_NONE
private

Definition at line 66 of file Finder.php.

◆ $vcsPatterns

array ILIAS\Filesystem\Finder\Finder::$vcsPatterns = ['.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg']
private

Definition at line 49 of file Finder.php.

◆ IGNORE_DOT_FILES

const ILIAS\Filesystem\Finder\Finder::IGNORE_DOT_FILES = 2
private

Definition at line 47 of file Finder.php.

◆ IGNORE_VCS_FILES

const ILIAS\Filesystem\Finder\Finder::IGNORE_VCS_FILES = 1
private

Definition at line 46 of file Finder.php.

Referenced by ILIAS\Filesystem\Finder\Finder\ignoreVCS().


The documentation for this class was generated from the following file: