ILIAS  trunk Revision v11.0_alpha-1811-gd2d5443e411
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator 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 (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 = FileTypeFilterIterator::ALL
 
bool $reverseSorting = false
 
array $dates = []
 
array $sizes = []
 
array $depths = []
 
 $sort = SortableIterator::SORT_BY_NONE
 

Detailed Description

Definition at line 52 of file Finder.php.

Constructor & Destructor Documentation

◆ __construct()

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

Definition at line 82 of file Finder.php.

83  {
84  $this->ignore = self::IGNORE_VCS_FILES | self::IGNORE_DOT_FILES;
85  }

Member Function Documentation

◆ addVCSPattern()

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

Definition at line 239 of file Finder.php.

239  : self
240  {
241  array_walk($pattern, static function ($p): void {
242  if (!is_string($p)) {
243  throw new InvalidArgumentException(sprintf('Invalid pattern given: %s', $p::class));
244  }
245  });
246 
247  $clone = clone $this;
248  foreach ($pattern as $p) {
249  $clone->vcsPatterns[] = $p;
250  }
251 
252  $clone->vcsPatterns = array_unique($clone->vcsPatterns);
253 
254  return $clone;
255  }

◆ allTypes()

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

Definition at line 103 of file Finder.php.

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

103  : self
104  {
105  $clone = clone $this;
106  $clone->mode = FileTypeFilterIterator::ALL;
107 
108  return $clone;
109  }

◆ 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 302 of file Finder.php.

302  : self
303  {
304  $clone = clone $this;
305 
306  if ($iterator instanceof IteratorAggregate) {
307  $clone->iterators[] = $iterator->getIterator();
308  } elseif ($iterator instanceof PhpIterator) {
309  $clone->iterators[] = $iterator;
310  } elseif (is_iterable($iterator)) {
311  $it = new ArrayIterator();
312  foreach ($iterator as $file) {
313  if ($file instanceof MetadataType) {
314  $it->append($file);
315  } else {
316  throw new InvalidArgumentException(
317  'Finder::append() method wrong argument type in passed iterator.'
318  );
319  }
320  }
321  $clone->iterators[] = $it;
322  } else {
323  throw new InvalidArgumentException('Finder::append() method wrong argument type.');
324  }
325 
326  return $clone;
327  }

◆ count()

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

Definition at line 403 of file Finder.php.

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

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

403  : int
404  {
405  return iterator_count($this->getIterator());
406  }
+ 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 181 of file Finder.php.

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

◆ 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 158 of file Finder.php.

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

◆ directories()

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

Definition at line 95 of file Finder.php.

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

95  : self
96  {
97  $clone = clone $this;
99 
100  return $clone;
101  }

◆ exclude()

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

Definition at line 115 of file Finder.php.

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

115  : self
116  {
117  array_walk($directories, static function ($directory): void {
118  if (!is_string($directory)) {
119  throw new InvalidArgumentException(sprintf('Invalid directory given: %s', $directory::class));
120  }
121  });
122 
123  $clone = clone $this;
124  $clone->exclude = array_merge($clone->exclude, $directories);
125 
126  return $clone;
127  }
+ Here is the caller graph for this function:

◆ files()

◆ getIterator()

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

Returns
PhpIterator|Metadata[]
Exceptions
LogicException

Definition at line 378 of file Finder.php.

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

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

378  : \Iterator
379  {
380  if ([] === $this->dirs && [] === $this->iterators) {
381  throw new LogicException('You must call one of in() or append() methods before iterating over a Finder.');
382  }
383 
384  if (1 === count($this->dirs) && [] === $this->iterators) {
385  return $this->searchInDirectory($this->dirs[0]);
386  }
387 
388  $iterator = new AppendIterator();
389  foreach ($this->dirs as $dir) {
390  $iterator->append($this->searchInDirectory($dir));
391  }
392 
393  foreach ($this->iterators as $it) {
394  $iterator->append($it);
395  }
396 
397  return $iterator;
398  }
searchInDirectory(string $dir)
Definition: Finder.php:329
+ 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 223 of file Finder.php.

References ILIAS\Filesystem\Finder\Finder\IGNORE_VCS_FILES.

223  : self
224  {
225  $clone = clone $this;
226  if ($ignoreVCS) {
227  $clone->ignore |= self::IGNORE_VCS_FILES;
228  } else {
229  $clone->ignore &= ~self::IGNORE_VCS_FILES;
230  }
231 
232  return $clone;
233  }

◆ in()

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

Definition at line 133 of file Finder.php.

133  : self
134  {
135  array_walk($directories, static function ($directory): void {
136  if (!is_string($directory)) {
137  throw new InvalidArgumentException(sprintf('Invalid directory given: %s', $directory::class));
138  }
139  });
140 
141  $clone = clone $this;
142  $clone->dirs = array_unique(array_merge($clone->dirs, $directories));
143 
144  return $clone;
145  }

◆ reverseSorting()

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

Definition at line 215 of file Finder.php.

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

215  : self
216  {
217  $clone = clone $this;
218  $clone->reverseSorting = true;
219 
220  return $clone;
221  }
+ Here is the caller graph for this function:

◆ searchInDirectory()

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

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

329  : \Traversable
330  {
331  if (self::IGNORE_VCS_FILES === (self::IGNORE_VCS_FILES & $this->ignore)) {
332  $this->exclude = array_merge($this->exclude, $this->vcsPatterns);
333  }
334 
335  $iterator = new RecursiveDirectoryIterator($this->filesystem, $dir);
336 
337  if ($this->exclude) {
338  $iterator = new ExcludeDirectoryFilterIterator($iterator, $this->exclude);
339  }
340 
341  $iterator = new RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
342 
343  if ($this->depths) {
344  $iterator = new DepthRangeFilterIterator($iterator, $this->depths);
345  }
346 
347  if ($this->mode !== 0) {
348  $iterator = new FileTypeFilterIterator($iterator, $this->mode);
349  }
350 
351  if ($this->dates) {
352  $iterator = new DateRangeFilterIterator($this->filesystem, $iterator, $this->dates);
353  }
354 
355  if ($this->sizes) {
356  $iterator = new SizeRangeFilterIterator($this->filesystem, $iterator, $this->sizes);
357  }
358 
359  if ($this->sort || $this->reverseSorting) {
360  $iteratorAggregate = new SortableIterator(
361  $this->filesystem,
362  $iterator,
363  $this->sort,
364  $this->reverseSorting
365  );
366  $iterator = $iteratorAggregate->getIterator();
367  }
368 
369  return $iterator;
370  }
sort(Closure $closure)
Sorts files and directories by an anonymous function.
Definition: Finder.php:262
exclude(array $directories)
Definition: Finder.php:115
+ 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 202 of file Finder.php.

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

202  : self
203  {
204  $sizes = is_array($sizes) ? $sizes : [$sizes];
205 
206  $clone = clone $this;
207 
208  foreach ($sizes as $size) {
209  $clone->sizes[] = new NumberComparator((string) $size);
210  }
211 
212  return $clone;
213  }

◆ 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 262 of file Finder.php.

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

262  : self
263  {
264  $clone = clone $this;
265  $clone->sort = $closure;
266 
267  return $clone;
268  }
+ Here is the caller graph for this function:

◆ sortByName()

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

Definition at line 270 of file Finder.php.

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

270  : self
271  {
272  $clone = clone $this;
273  $clone->sort = SortableIterator::SORT_BY_NAME;
274  if ($useNaturalSort) {
276  }
277 
278  return $clone;
279  }

◆ sortByTime()

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

Definition at line 289 of file Finder.php.

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

289  : self
290  {
291  $clone = clone $this;
292  $clone->sort = SortableIterator::SORT_BY_TIME;
293 
294  return $clone;
295  }

◆ sortByType()

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

Definition at line 281 of file Finder.php.

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

281  : self
282  {
283  $clone = clone $this;
284  $clone->sort = SortableIterator::SORT_BY_TYPE;
285 
286  return $clone;
287  }

Field Documentation

◆ $dates

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

Definition at line 74 of file Finder.php.

◆ $depths

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

Definition at line 78 of file Finder.php.

◆ $dirs

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

Definition at line 67 of file Finder.php.

◆ $exclude

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

Definition at line 69 of file Finder.php.

◆ $ignore

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

Definition at line 70 of file Finder.php.

◆ $iterators

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

Definition at line 65 of file Finder.php.

◆ $mode

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

Definition at line 71 of file Finder.php.

◆ $reverseSorting

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

Definition at line 72 of file Finder.php.

◆ $sizes

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

Definition at line 76 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 80 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 63 of file Finder.php.

◆ IGNORE_DOT_FILES

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

Definition at line 61 of file Finder.php.

◆ IGNORE_VCS_FILES

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

Definition at line 57 of file Finder.php.

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


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