Finder
class Finder implements IteratorAggregate, Countable
Finder allows to build rules to find files and directories.
It is a thin wrapper around several specialized iterator classes.
All rules may be invoked several times.
All methods return the current Finder object to allow easy chaining:
$finder = Finder::create()->files()->name('*.php')->in(__DIR__);
Constants
IGNORE_VCS_FILES |
|
IGNORE_DOT_FILES |
|
Methods
No description
Restricts the matching to directories only.
Restricts the matching to files only.
Adds tests for the directory depth.
Adds tests for file dates (last modified).
Adds rules that files must match.
Adds rules that files must not match.
Adds tests that file contents must match.
Adds tests that file contents must not match.
Adds rules that filenames must match.
Adds rules that filenames must not match.
Adds tests for file sizes.
Excludes directories.
Excludes "hidden" directories and files (starting with a dot).
Forces the finder to ignore version control directories.
Adds VCS patterns.
Sorts files and directories by name.
Sorts files and directories by type (directories before files), then by name.
Sorts files and directories by the last accessed time.
Reverses the sorting.
Sorts files and directories by the last inode changed time.
Sorts files and directories by the last modified time.
Forces the following of symlinks.
Tells finder to ignore unreadable directories.
Searches files and directories which match defined rules.
Returns an Iterator for the current Finder configuration.
Appends an existing set of files/directories to the finder.
Check if the any results were found.
Counts all the results collected by the iterators.
Details
$this
depth(string|int|string[]|int[] $levels)
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.
$finder->depth(['>= 1', '< 3'])
$this
date(string|string[] $dates)
Adds tests for file dates (last modified).
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');
$finder->date(['>= 2005-10-15', '<= 2006-05-27']);
$this
name(string|string[] $patterns)
Adds rules that files must match.
You can use patterns (delimited with / sign), globs or simple strings.
$finder->name('*.php')
$finder->name('/\.php$/') // same as above
$finder->name('test.php')
$finder->name(['test.py', 'test.php'])
$this
contains(string|string[] $patterns)
Adds tests that file contents must match.
Strings or PCRE patterns can be used:
$finder->contains('Lorem ipsum')
$finder->contains('/Lorem ipsum/i')
$finder->contains(['dolor', '/ipsum/i'])
$this
notContains(string|string[] $patterns)
Adds tests that file contents must not match.
Strings or PCRE patterns can be used:
$finder->notContains('Lorem ipsum')
$finder->notContains('/Lorem ipsum/i')
$finder->notContains(['lorem', '/dolor/i'])
$this
path(string|string[] $patterns)
Adds rules that filenames must match.
You can use patterns (delimited with / sign) or simple strings.
$finder->path('some/special/dir')
$finder->path('/some\/special\/dir/') // same as above
$finder->path(['some dir', 'another/dir'])
Use only / as dirname separator.
$this
notPath(string|string[] $patterns)
Adds rules that filenames must not match.
You can use patterns (delimited with / sign) or simple strings.
$finder->notPath('some/special/dir')
$finder->notPath('/some\/special\/dir/') // same as above
$finder->notPath(['some/file.txt', 'another/file.log'])
Use only / as dirname separator.
$this
size(string|int|string[]|int[] $sizes)
Adds tests for file sizes.
$finder->size('> 10K'); $finder->size('<= 1Ki'); $finder->size(4); $finder->size(['> 10K', '< 20K'])
$this
exclude(string|array $dirs)
Excludes directories.
Directories passed as argument must be relative to the ones defined with the in()
method. For example:
$finder->in(__DIR__)->exclude('ruby');
$this
ignoreDotFiles(bool $ignoreDotFiles)
Excludes "hidden" directories and files (starting with a dot).
This option is enabled by default.
$this
ignoreVCS(bool $ignoreVCS)
Forces the finder to ignore version control directories.
This option is enabled by default.
$this
sort(Closure $closure)
Sorts files and directories by an anonymous function.
The anonymous function receives two \SplFileInfo instances to compare.
This can be slow as all the matching files and directories must be retrieved for comparison.
$this
sortByName()
Sorts files and directories by name.
This can be slow as all the matching files and directories must be retrieved for comparison.
$this
sortByType()
Sorts files and directories by type (directories before files), then by name.
This can be slow as all the matching files and directories must be retrieved for comparison.
$this
sortByAccessedTime()
Sorts files and directories by the last accessed time.
This is the time that the file was last accessed, read or written to.
This can be slow as all the matching files and directories must be retrieved for comparison.
$this
sortByChangedTime()
Sorts files and directories by the last inode changed time.
This is the time that the inode information was last modified (permissions, owner, group or other metadata).
On Windows, since inode is not available, changed time is actually the file creation time.
This can be slow as all the matching files and directories must be retrieved for comparison.
$this
sortByModifiedTime()
Sorts files and directories by the last modified time.
This is the last time the actual contents of the file were last modified.
This can be slow as all the matching files and directories must be retrieved for comparison.
$this
filter(Closure $closure)
Filters the iterator with an anonymous function.
The anonymous function receives a \SplFileInfo and must return false to remove files.
$this
ignoreUnreadableDirs(bool $ignore = true)
Tells finder to ignore unreadable directories.
By default, scanning unreadable directories content throws an AccessDeniedException.
Iterator|SplFileInfo[]
getIterator()
Returns an Iterator for the current Finder configuration.
This method implements the IteratorAggregate interface.