ILIAS  release_8 Revision v8.19-1-g4e8f2f9140c
All Data Structures Namespaces Files Functions Variables Modules Pages
ILIAS\Filesystem\Finder\Finder Class Reference
+ Inheritance diagram for ILIAS\Filesystem\Finder\Finder:
+ Collaboration diagram for ILIAS\Filesystem\Finder\Finder:

Public Member Functions

 __construct (Filesystem $filesystem)
 
 files ()
 
 directories ()
 
 allTypes ()
 
 exclude (array $directories)
 
 in (array $directories)
 
 depth ($level)
 Adds tests for the directory depth. More...
 
 date (string $date)
 Adds tests for file dates. More...
 
 size ($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
 
ILIAS Filesystem Filesystem $filesystem
 
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 ( Filesystem  $filesystem)

Definition at line 70 of file Finder.php.

References ILIAS\Filesystem\Finder\Finder\$filesystem, and ILIAS\Repository\filesystem().

71  {
72  $this->filesystem = $filesystem;
73  $this->ignore = self::IGNORE_VCS_FILES | self::IGNORE_DOT_FILES;
74  }
ILIAS Filesystem Filesystem $filesystem
Definition: Finder.php:49
+ Here is the call graph for this function:

Member Function Documentation

◆ addVCSPattern()

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

Definition at line 244 of file Finder.php.

244  : self
245  {
246  array_walk($pattern, static function ($p): void {
247  if (!is_string($p)) {
248  if (is_object($p)) {
249  throw new InvalidArgumentException(sprintf('Invalid pattern given: %s', get_class($p)));
250  }
251 
252  throw new InvalidArgumentException(sprintf('Invalid pattern given: %s', gettype($p)));
253  }
254  });
255 
256  $clone = clone $this;
257  foreach ($pattern as $p) {
258  $clone->vcsPatterns[] = $p;
259  }
260 
261  $clone->vcsPatterns = array_unique($clone->vcsPatterns);
262 
263  return $clone;
264  }

◆ allTypes()

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

Definition at line 92 of file Finder.php.

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

92  : self
93  {
94  $clone = clone $this;
96 
97  return $clone;
98  }

◆ 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.

Parameters
iterable$iterator
Returns
Finder
Exceptions
InvalidArgumentExceptionwhen the given argument is not iterable

Definition at line 315 of file Finder.php.

315  : self
316  {
317  $clone = clone $this;
318 
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) {
326  if ($file instanceof MetadataType) {
327  $it->append($file);
328  } else {
329  throw new InvalidArgumentException('Finder::append() method wrong argument type in passed iterator.');
330  }
331  }
332  $clone->iterators[] = $it;
333  } else {
334  throw new InvalidArgumentException('Finder::append() method wrong argument type.');
335  }
336 
337  return $clone;
338  }

◆ count()

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

Definition at line 414 of file Finder.php.

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

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

414  : int
415  {
416  return iterator_count($this->getIterator());
417  }
+ 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
Returns
Finder
See also
strtotime
DateRangeFilterIterator
DateComparator
::getTimestamp()

Definition at line 182 of file Finder.php.

182  : self
183  {
184  $clone = clone $this;
185  $clone->dates[] = new Comparator\DateComparator($date);
186 
187  return $clone;
188  }

◆ depth()

ILIAS\Filesystem\Finder\Finder::depth (   $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
Returns
Finder
See also
DepthRangeFilterIterator
NumberComparator

Definition at line 158 of file Finder.php.

158  : self
159  {
160  $clone = clone $this;
161  $clone->depths[] = new Comparator\NumberComparator((string) $level);
162 
163  return $clone;
164  }

◆ directories()

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

Definition at line 84 of file Finder.php.

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

84  : self
85  {
86  $clone = clone $this;
88 
89  return $clone;
90  }

◆ exclude()

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

Definition at line 105 of file Finder.php.

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

105  : self
106  {
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)));
111  }
112 
113  throw new InvalidArgumentException(sprintf('Invalid directory given: %s', gettype($directory)));
114  }
115  });
116 
117  $clone = clone $this;
118  $clone->exclude = array_merge($clone->exclude, $directories);
119 
120  return $clone;
121  }
+ Here is the caller graph for this function:

◆ files()

◆ getIterator()

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

Returns
PhpIterator|Metadata[]
Exceptions
LogicException

Definition at line 389 of file Finder.php.

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

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

390  {
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.');
393  }
394 
395  if (1 === count($this->dirs) && 0 === count($this->iterators)) {
396  return $this->searchInDirectory($this->dirs[0]);
397  }
398 
399  $iterator = new AppendIterator();
400  foreach ($this->dirs as $dir) {
401  $iterator->append($this->searchInDirectory($dir));
402  }
403 
404  foreach ($this->iterators as $it) {
405  $iterator->append($it);
406  }
407 
408  return $iterator;
409  }
searchInDirectory(string $dir)
Definition: Finder.php:340
+ 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 227 of file Finder.php.

References ILIAS\Filesystem\Finder\Finder\IGNORE_VCS_FILES.

227  : self
228  {
229  $clone = clone $this;
230  if ($ignoreVCS) {
231  $clone->ignore |= self::IGNORE_VCS_FILES;
232  } else {
233  $clone->ignore &= ~self::IGNORE_VCS_FILES;
234  }
235 
236  return $clone;
237  }

◆ in()

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

Definition at line 128 of file Finder.php.

128  : self
129  {
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)));
134  }
135 
136  throw new InvalidArgumentException(sprintf('Invalid directory given: %s', gettype($directory)));
137  }
138  });
139 
140  $clone = clone $this;
141  $clone->dirs = array_unique(array_merge($clone->dirs, $directories));
142 
143  return $clone;
144  }

◆ reverseSorting()

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

Definition at line 219 of file Finder.php.

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

219  : self
220  {
221  $clone = clone $this;
222  $clone->reverseSorting = true;
223 
224  return $clone;
225  }
+ Here is the caller graph for this function:

◆ searchInDirectory()

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

Definition at line 340 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().

340  : PhpIterator
341  {
342  if (self::IGNORE_VCS_FILES === (self::IGNORE_VCS_FILES & $this->ignore)) {
343  $this->exclude = array_merge($this->exclude, $this->vcsPatterns);
344  }
345 
346  $iterator = new Iterator\RecursiveDirectoryIterator($this->filesystem, $dir);
347 
348  if ($this->exclude) {
349  $iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
350  }
351 
352  $iterator = new RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
353 
354  if ($this->depths) {
355  $iterator = new Iterator\DepthRangeFilterIterator($iterator, $this->depths);
356  }
357 
358  if ($this->mode) {
359  $iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode);
360  }
361 
362  if ($this->dates) {
363  $iterator = new Iterator\DateRangeFilterIterator($this->filesystem, $iterator, $this->dates);
364  }
365 
366  if ($this->sizes) {
367  $iterator = new Iterator\SizeRangeFilterIterator($this->filesystem, $iterator, $this->sizes);
368  }
369 
370  if ($this->sort || $this->reverseSorting) {
371  $iteratorAggregate = new Iterator\SortableIterator(
372  $this->filesystem,
373  $iterator,
374  $this->sort,
375  $this->reverseSorting
376  );
377  $iterator = $iteratorAggregate->getIterator();
378  }
379 
380  return $iterator;
381  }
sort(Closure $closure)
Sorts files and directories by an anonymous function.
Definition: Finder.php:273
exclude(array $directories)
Definition: Finder.php:105
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ size()

ILIAS\Filesystem\Finder\Finder::size (   $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
Returns
Finder
See also
SizeRangeFilterIterator
NumberComparator
::getSize()

Definition at line 204 of file Finder.php.

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

204  : self
205  {
206  if (!is_array($sizes)) {
207  $sizes = [$sizes];
208  }
209 
210  $clone = clone $this;
211 
212  foreach ($sizes as $size) {
213  $clone->sizes[] = new Comparator\NumberComparator((string) $size);
214  }
215 
216  return $clone;
217  }

◆ 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.

Parameters
Closure$closure
Returns
Finder

Definition at line 273 of file Finder.php.

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

273  : self
274  {
275  $clone = clone $this;
276  $clone->sort = $closure;
277 
278  return $clone;
279  }
+ Here is the caller graph for this function:

◆ sortByName()

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

Definition at line 281 of file Finder.php.

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

281  : self
282  {
283  $clone = clone $this;
285  if ($useNaturalSort) {
287  }
288 
289  return $clone;
290  }

◆ sortByTime()

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

Definition at line 300 of file Finder.php.

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

300  : self
301  {
302  $clone = clone $this;
304 
305  return $clone;
306  }

◆ sortByType()

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

Definition at line 292 of file Finder.php.

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

292  : self
293  {
294  $clone = clone $this;
296 
297  return $clone;
298  }

Field Documentation

◆ $dates

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

Definition at line 62 of file Finder.php.

◆ $depths

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

Definition at line 66 of file Finder.php.

◆ $dirs

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

Definition at line 55 of file Finder.php.

◆ $exclude

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

Definition at line 57 of file Finder.php.

◆ $filesystem

ILIAS Filesystem Filesystem ILIAS\Filesystem\Finder\Finder::$filesystem
private

Definition at line 49 of file Finder.php.

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

◆ $ignore

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

Definition at line 58 of file Finder.php.

◆ $iterators

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

Definition at line 53 of file Finder.php.

◆ $mode

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

Definition at line 59 of file Finder.php.

◆ $reverseSorting

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

Definition at line 60 of file Finder.php.

◆ $sizes

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

Definition at line 64 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 68 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 51 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: