Hoa central
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Hoa\File\Finder Class Reference
Inheritance diagram for Hoa\File\Finder:

Public Member Functions

 __construct ()
 
 in ($path)
 
 maxDepth ($depth)
 
 files ()
 
 directories ()
 
 links ()
 
 followSymlinks ($flag=true)
 
 name ($regex)
 
 notIn ($regex)
 
 size ($size)
 
 dots ($flag=true)
 
 owner ($owner)
 
 changed ($date)
 
 modified ($date)
 
 filter ($callback)
 
 sortByName ($locale= 'root')
 
 sortBySize ()
 
 sort ($callable)
 
 childFirst ()
 
 getIterator ()
 
 setSplFileInfo ($splFileInfo)
 
 getSplFileInfo ()
 
 getMaxDepth ()
 
 getTypes ()
 
 getName ()
 
 getFlags ()
 
 getFirst ()
 

Protected Member Functions

 formatDate ($date, &$operator)
 
 getPaths ()
 
 getFilters ()
 
 getSorts ()
 

Protected Attributes

 $_splFileInfo = 'Hoa\File\SplFileInfo'
 
 $_paths = []
 
 $_maxDepth = -1
 
 $_filters = []
 
 $_flags = -1
 
 $_types = []
 
 $_first = -1
 
 $_sorts = []
 

Detailed Description

Class .

This class allows to find files easily by using filters and flags.

Definition at line 49 of file Finder.php.

Constructor & Destructor Documentation

Hoa\File\Finder::__construct ( )

Initialize.

Returns
void

Definition at line 114 of file Finder.php.

115  {
116  $this->_flags = Iterator\FileSystem::KEY_AS_PATHNAME
117  | Iterator\FileSystem::CURRENT_AS_FILEINFO
118  | Iterator\FileSystem::SKIP_DOTS;
119  $this->_first = Iterator\Recursive\Iterator::SELF_FIRST;
120 
121  return;
122  }

Member Function Documentation

Hoa\File\Finder::changed (   $date)

Include files that have been changed from a certain date. Example: $this->changed('since 13 days');

Parameters
string$dateDate.
Returns

Definition at line 447 of file Finder.php.

448  {
449  $time = $this->formatDate($date, $operator);
450 
451  if (-1 === $operator) {
452  $this->_filters[] = function ($current) use ($time) {
453  return $current->getCTime() >= $time;
454  };
455  } else {
456  $this->_filters[] = function ($current) use ($time) {
457  return $current->getCTime() < $time;
458  };
459  }
460 
461  return $this;
462  }
formatDate($date, &$operator)
Definition: Finder.php:417

Here is the call graph for this function:

Hoa\File\Finder::childFirst ( )

Child comes first when iterating.

Returns

Definition at line 577 of file Finder.php.

578  {
579  $this->_first = Iterator\Recursive\Iterator::CHILD_FIRST;
580 
581  return $this;
582  }
Hoa\File\Finder::directories ( )

Include directories in the result.

Returns

Definition at line 173 of file Finder.php.

174  {
175  $this->_types[] = 'dir';
176 
177  return $this;
178  }
Hoa\File\Finder::dots (   $flag = true)

Whether we should include dots or not (respectively . and ..).

Parameters
bool$flagInclude or not.
Returns

Definition at line 376 of file Finder.php.

377  {
378  if (true === $flag) {
379  $this->_flags ^= Iterator\FileSystem::SKIP_DOTS;
380  } else {
381  $this->_flags |= Iterator\FileSystem::SKIP_DOTS;
382  }
383 
384  return $this;
385  }
Hoa\File\Finder::files ( )

Include files in the result.

Returns

Definition at line 161 of file Finder.php.

162  {
163  $this->_types[] = 'file';
164 
165  return $this;
166  }
Hoa\File\Finder::filter (   $callback)

Add your own filter. The callback will receive 3 arguments: $current, $key and $iterator. It must return a boolean: true to include the file, false to exclude it. Example: // Include files that are readable $this->filter(function ($current) { return $current->isReadable(); });

Parameters
callable$callbackCallback
Returns

Definition at line 502 of file Finder.php.

503  {
504  $this->_filters[] = $callback;
505 
506  return $this;
507  }
Hoa\File\Finder::followSymlinks (   $flag = true)

Follow symbolink links.

Parameters
bool$flagWhether we follow or not.
Returns

Definition at line 198 of file Finder.php.

199  {
200  if (true === $flag) {
201  $this->_flags ^= Iterator\FileSystem::FOLLOW_SYMLINKS;
202  } else {
203  $this->_flags |= Iterator\FileSystem::FOLLOW_SYMLINKS;
204  }
205 
206  return $this;
207  }
Hoa\File\Finder::formatDate (   $date,
$operator 
)
protected

Format date. Date can have the following syntax: date since date until date If the date does not have the “ago” keyword, it will be added. Example: “42 hours” is equivalent to “since 42 hours” which is equivalent to “since 42 hours ago”.

Parameters
string$dateDate.
int&$operatorOperator (-1 for since, 1 for until).
Returns
int

Definition at line 417 of file Finder.php.

418  {
419  $time = 0;
420  $operator = -1;
421 
422  if (0 === preg_match('#\bago\b#', $date)) {
423  $date .= ' ago';
424  }
425 
426  if (0 !== preg_match('#^(since|until)\b(.+)$#', $date, $matches)) {
427  $time = strtotime($matches[2]);
428 
429  if ('until' === $matches[1]) {
430  $operator = 1;
431  }
432  } else {
433  $time = strtotime($date);
434  }
435 
436  return $time;
437  }

Here is the caller graph for this function:

Hoa\File\Finder::getFilters ( )
protected

Get filters.

Returns
array

Definition at line 722 of file Finder.php.

723  {
724  return $this->_filters;
725  }

Here is the caller graph for this function:

Hoa\File\Finder::getFirst ( )

Get first.

Returns
int

Definition at line 752 of file Finder.php.

753  {
754  return $this->_first;
755  }

Here is the caller graph for this function:

Hoa\File\Finder::getFlags ( )

Get flags.

Returns
int

Definition at line 742 of file Finder.php.

743  {
744  return $this->_flags;
745  }

Here is the caller graph for this function:

Hoa\File\Finder::getIterator ( )

Get the iterator.

Returns

Definition at line 589 of file Finder.php.

590  {
591  $_iterator = new Iterator\Append();
592  $types = $this->getTypes();
593 
594  if (!empty($types)) {
595  $this->_filters[] = function ($current) use ($types) {
596  return in_array($current->getType(), $types);
597  };
598  }
599 
600  $maxDepth = $this->getMaxDepth();
601  $splFileInfo = $this->getSplFileInfo();
602 
603  foreach ($this->getPaths() as $path) {
604  if (1 == $maxDepth) {
605  $iterator = new Iterator\IteratorIterator(
606  new Iterator\Recursive\Directory(
607  $path,
608  $this->getFlags(),
609  $splFileInfo
610  ),
611  $this->getFirst()
612  );
613  } else {
614  $iterator = new Iterator\Recursive\Iterator(
615  new Iterator\Recursive\Directory(
616  $path,
617  $this->getFlags(),
618  $splFileInfo
619  ),
620  $this->getFirst()
621  );
622 
623  if (1 < $maxDepth) {
624  $iterator->setMaxDepth($maxDepth - 1);
625  }
626  }
627 
628  $_iterator->append($iterator);
629  }
630 
631  foreach ($this->getFilters() as $filter) {
632  $_iterator = new Iterator\CallbackFilter(
633  $_iterator,
634  $filter
635  );
636  }
637 
638  $sorts = $this->getSorts();
639 
640  if (empty($sorts)) {
641  return $_iterator;
642  }
643 
644  $array = iterator_to_array($_iterator);
645 
646  foreach ($sorts as $sort) {
647  uasort($array, $sort);
648  }
649 
650  return new Iterator\Map($array);
651  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\File\Finder::getMaxDepth ( )

Get max depth.

Returns
int

Definition at line 692 of file Finder.php.

693  {
694  return $this->_maxDepth;
695  }

Here is the caller graph for this function:

Hoa\File\Finder::getName ( )

Get name.

Returns
string

Definition at line 712 of file Finder.php.

713  {
714  return $this->_name;
715  }
Hoa\File\Finder::getPaths ( )
protected

Get all paths.

Returns
array

Definition at line 682 of file Finder.php.

683  {
684  return $this->_paths;
685  }

Here is the caller graph for this function:

Hoa\File\Finder::getSorts ( )
protected

Get sorts.

Returns
array

Definition at line 732 of file Finder.php.

733  {
734  return $this->_sorts;
735  }

Here is the caller graph for this function:

Hoa\File\Finder::getSplFileInfo ( )

Get SplFileInfo classname.

Returns
string

Definition at line 672 of file Finder.php.

673  {
674  return $this->_splFileInfo;
675  }

Here is the caller graph for this function:

Hoa\File\Finder::getTypes ( )

Get types.

Returns
array

Definition at line 702 of file Finder.php.

703  {
704  return $this->_types;
705  }

Here is the caller graph for this function:

Hoa\File\Finder::in (   $path)

Select a directory to scan.

Parameters
string$pathPath.
Returns

Definition at line 130 of file Finder.php.

131  {
132  if (!is_array($path)) {
133  $path = [$path];
134  }
135 
136  foreach ($path as $p) {
137  $this->_paths[] = $p;
138  }
139 
140  return $this;
141  }
Hoa\File\Finder::links ( )

Include links in the result.

Returns

Definition at line 185 of file Finder.php.

186  {
187  $this->_types[] = 'link';
188 
189  return $this;
190  }
Hoa\File\Finder::maxDepth (   $depth)

Set max depth for recursion.

Parameters
int$depthDepth.
Returns

Definition at line 149 of file Finder.php.

150  {
151  $this->_maxDepth = $depth;
152 
153  return $this;
154  }
Hoa\File\Finder::modified (   $date)

Include files that have been modified from a certain date. Example: $this->modified('since 13 days');

Parameters
string$dateDate.
Returns

Definition at line 472 of file Finder.php.

473  {
474  $time = $this->formatDate($date, $operator);
475 
476  if (-1 === $operator) {
477  $this->_filters[] = function ($current) use ($time) {
478  return $current->getMTime() >= $time;
479  };
480  } else {
481  $this->_filters[] = function ($current) use ($time) {
482  return $current->getMTime() < $time;
483  };
484  }
485 
486  return $this;
487  }
formatDate($date, &$operator)
Definition: Finder.php:417

Here is the call graph for this function:

Hoa\File\Finder::name (   $regex)

Include files that match a regex. Example: $this->name('#.php$#');

Returns

Definition at line 216 of file Finder.php.

217  {
218  $this->_filters[] = function ($current) use ($regex) {
219  return 0 !== preg_match($regex, $current->getBasename());
220  };
221 
222  return $this;
223  }
Hoa\File\Finder::notIn (   $regex)

Exclude directories that match a regex. Example: $this->notIn('#^.(git|hg)$#');

Returns

Definition at line 232 of file Finder.php.

233  {
234  $this->_filters[] = function ($current) use ($regex) {
235  foreach (explode(DS, $current->getPathname()) as $part) {
236  if (0 !== preg_match($regex, $part)) {
237  return false;
238  }
239  }
240 
241  return true;
242  };
243 
244  return $this;
245  }
Hoa\File\Finder::owner (   $owner)

Include files that are owned by a certain owner.

Parameters
int$ownerOwner.
Returns

Definition at line 393 of file Finder.php.

394  {
395  $this->_filters[] = function ($current) use ($owner) {
396 
397  return $current->getOwner() === $owner;
398  };
399 
400  return $this;
401  }
Hoa\File\Finder::setSplFileInfo (   $splFileInfo)

Set SplFileInfo classname.

Parameters
string$splFileInfoSplFileInfo classname.
Returns
string

Definition at line 659 of file Finder.php.

660  {
661  $old = $this->_splFileInfo;
662  $this->_splFileInfo = $splFileInfo;
663 
664  return $old;
665  }
Hoa\File\Finder::size (   $size)

Include files that respect a certain size. The size is a string of the form: operator number unit where • operator could be: <, <=, >, >= or =; • number is a positive integer; • unit could be: b (default), Kb, Mb, Gb, Tb, Pb, Eb, Zb, Yb. Example: $this->size('>= 12Kb');

Parameters
string$sizeSize.
Returns

Definition at line 261 of file Finder.php.

262  {
263  if (0 === preg_match('#^(<|<=|>|>=|=)\s*(\d+)\s*((?:[KMGTPEZY])b)?$#', $size, $matches)) {
264  return $this;
265  }
266 
267  $number = floatval($matches[2]);
268  $unit = isset($matches[3]) ? $matches[3] : 'b';
269  $operator = $matches[1];
270 
271  switch ($unit) {
272 
273  case 'b':
274  break;
275 
276  // kilo
277  case 'Kb':
278  $number <<= 10;
279 
280  break;
281 
282  // mega.
283  case 'Mb':
284  $number <<= 20;
285 
286  break;
287 
288  // giga.
289  case 'Gb':
290  $number <<= 30;
291 
292  break;
293 
294  // tera.
295  case 'Tb':
296  $number *= 1099511627776;
297 
298  break;
299 
300  // peta.
301  case 'Pb':
302  $number *= pow(1024, 5);
303 
304  break;
305 
306  // exa.
307  case 'Eb':
308  $number *= pow(1024, 6);
309 
310  break;
311 
312  // zetta.
313  case 'Zb':
314  $number *= pow(1024, 7);
315 
316  break;
317 
318  // yota.
319  case 'Yb':
320  $number *= pow(1024, 8);
321 
322  break;
323  }
324 
325  $filter = null;
326 
327  switch ($operator) {
328 
329  case '<':
330  $filter = function ($current, $key, $iterator) use ($number) {
331  return $current->getSize() < $number;
332  };
333 
334  break;
335 
336  case '<=':
337  $filter = function ($current, $key, $iterator) use ($number) {
338  return $current->getSize() <= $number;
339  };
340 
341  break;
342 
343  case '>':
344  $filter = function ($current, $key, $iterator) use ($number) {
345  return $current->getSize() > $number;
346  };
347 
348  break;
349 
350  case '>=':
351  $filter = function ($current, $key, $iterator) use ($number) {
352  return $current->getSize() >= $number;
353  };
354 
355  break;
356 
357  case '=':
358  $filter = function ($current, $key, $iterator) use ($number) {
359  return $current->getSize() === $number;
360  };
361 
362  break;
363  }
364 
365  $this->_filters[] = $filter;
366 
367  return $this;
368  }
Hoa\File\Finder::sort (   $callable)

Add your own sort. The callback will receive 2 arguments: $a and $b. Please see the uasort() function. Example: // Sort files by their modified time. $this->sort(function ($a, $b) { return $a->getMTime() < $b->getMTime(); });

Parameters
callable$callbackCallback
Returns

Definition at line 565 of file Finder.php.

566  {
567  $this->_sorts[] = $callable;
568 
569  return $this;
570  }
Hoa\File\Finder::sortByName (   $locale = 'root')

Sort result by name. If exists (from ext/intl), the $locale argument will be used for its constructor. Else, strcmp() will be used. Example: $this->sortByName('fr_FR');

Parameters
string$localeLocale.
Returns

Definition at line 519 of file Finder.php.

520  {
521  if (true === class_exists('Collator', false)) {
522  $collator = new \Collator($locale);
523 
524  $this->_sorts[] = function ($a, $b) use ($collator) {
525  return $collator->compare($a->getPathname(), $b->getPathname());
526  };
527  } else {
528  $this->_sorts[] = function ($a, $b) {
529  return strcmp($a->getPathname(), $b->getPathname());
530  };
531  }
532 
533  return $this;
534  }
Hoa\File\Finder::sortBySize ( )

Sort result by size. Example: $this->sortBySize();

Returns

Definition at line 543 of file Finder.php.

544  {
545  $this->_sorts[] = function ($a, $b) {
546  return $a->getSize() < $b->getSize();
547  };
548 
549  return $this;
550  }

Member Data Documentation

Hoa\File\Finder::$_filters = []
protected

Definition at line 77 of file Finder.php.

Hoa\File\Finder::$_first = -1
protected

Definition at line 98 of file Finder.php.

Hoa\File\Finder::$_flags = -1
protected

Definition at line 84 of file Finder.php.

Hoa\File\Finder::$_maxDepth = -1
protected

Definition at line 70 of file Finder.php.

Hoa\File\Finder::$_paths = []
protected

Definition at line 63 of file Finder.php.

Hoa\File\Finder::$_sorts = []
protected

Definition at line 105 of file Finder.php.

Hoa\File\Finder::$_splFileInfo = 'Hoa\File\SplFileInfo'
protected

Definition at line 56 of file Finder.php.

Hoa\File\Finder::$_types = []
protected

Definition at line 91 of file Finder.php.


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