class Finder
Same name in this branch
- 11.1.x core/lib/Drupal/Core/DefaultContent/Finder.php \Drupal\Core\DefaultContent\Finder
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 chaining:
$finder = Finder::create()->files()->name('*.php')->in(__DIR__);
@author Fabien Potencier <fabien@symfony.com>
@implements \IteratorAggregate<string, SplFileInfo>
Hierarchy
- class \Symfony\Component\Finder\Finder implements \Symfony\Component\Finder\IteratorAggregate, \Symfony\Component\Finder\Countable
Expanded class hierarchy of Finder
17 files declare their use of Finder
- ArchivableFilesFinder.php in vendor/
composer/ composer/ src/ Composer/ Package/ Archiver/ ArchivableFilesFinder.php - ArchiveDownloader.php in vendor/
composer/ composer/ src/ Composer/ Downloader/ ArchiveDownloader.php - Cache.php in vendor/
composer/ composer/ src/ Composer/ Cache.php - ClassMapGenerator.php in vendor/
composer/ class-map-generator/ src/ ClassMapGenerator.php - Compiler.php in vendor/
composer/ composer/ src/ Composer/ Compiler.php
File
-
vendor/
symfony/ finder/ Finder.php, line 42
Namespace
Symfony\Component\FinderView source
class Finder implements \IteratorAggregate, \Countable {
public const IGNORE_VCS_FILES = 1;
public const IGNORE_DOT_FILES = 2;
public const IGNORE_VCS_IGNORED_FILES = 4;
private int $mode = 0;
private array $names = [];
private array $notNames = [];
private array $exclude = [];
private array $filters = [];
private array $pruneFilters = [];
private array $depths = [];
private array $sizes = [];
private bool $followLinks = false;
private bool $reverseSorting = false;
private \Closure|int|false $sort = false;
private int $ignore = 0;
private array $dirs = [];
private array $dates = [];
private array $iterators = [];
private array $contains = [];
private array $notContains = [];
private array $paths = [];
private array $notPaths = [];
private bool $ignoreUnreadableDirs = false;
private static array $vcsPatterns = [
'.svn',
'_svn',
'CVS',
'_darcs',
'.arch-params',
'.monotone',
'.bzr',
'.git',
'.hg',
];
public function __construct() {
$this->ignore = static::IGNORE_VCS_FILES | static::IGNORE_DOT_FILES;
}
/**
* Creates a new Finder.
*/
public static function create() : static {
return new static();
}
/**
* Restricts the matching to directories only.
*
* @return $this
*/
public function directories() : static {
$this->mode = Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES;
return $this;
}
/**
* Restricts the matching to files only.
*
* @return $this
*/
public function files() : static {
$this->mode = Iterator\FileTypeFilterIterator::ONLY_FILES;
return $this;
}
/**
* 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'])
*
* @param string|int|string[]|int[] $levels The depth level expression or an array of depth levels
*
* @return $this
*
* @see DepthRangeFilterIterator
* @see NumberComparator
*/
public function depth(string|int|array $levels) : static {
foreach ((array) $levels as $level) {
$this->depths[] = new NumberComparator($level);
}
return $this;
}
/**
* 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']);
*
* @param string|string[] $dates A date range string or an array of date ranges
*
* @return $this
*
* @see strtotime
* @see DateRangeFilterIterator
* @see DateComparator
*/
public function date(string|array $dates) : static {
foreach ((array) $dates as $date) {
$this->dates[] = new DateComparator($date);
}
return $this;
}
/**
* 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, without dot files
* $finder->name('test.php')
* $finder->name(['test.py', 'test.php'])
*
* @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
*
* @return $this
*
* @see FilenameFilterIterator
*/
public function name(string|array $patterns) : static {
$this->names = array_merge($this->names, (array) $patterns);
return $this;
}
/**
* Adds rules that files must not match.
*
* @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
*
* @return $this
*
* @see FilenameFilterIterator
*/
public function notName(string|array $patterns) : static {
$this->notNames = array_merge($this->notNames, (array) $patterns);
return $this;
}
/**
* 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'])
*
* @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
*
* @return $this
*
* @see FilecontentFilterIterator
*/
public function contains(string|array $patterns) : static {
$this->contains = array_merge($this->contains, (array) $patterns);
return $this;
}
/**
* 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'])
*
* @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
*
* @return $this
*
* @see FilecontentFilterIterator
*/
public function notContains(string|array $patterns) : static {
$this->notContains = array_merge($this->notContains, (array) $patterns);
return $this;
}
/**
* 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.
*
* @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
*
* @return $this
*
* @see FilenameFilterIterator
*/
public function path(string|array $patterns) : static {
$this->paths = array_merge($this->paths, (array) $patterns);
return $this;
}
/**
* 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.
*
* @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
*
* @return $this
*
* @see FilenameFilterIterator
*/
public function notPath(string|array $patterns) : static {
$this->notPaths = array_merge($this->notPaths, (array) $patterns);
return $this;
}
/**
* Adds tests for file sizes.
*
* $finder->size('> 10K');
* $finder->size('<= 1Ki');
* $finder->size(4);
* $finder->size(['> 10K', '< 20K'])
*
* @param string|int|string[]|int[] $sizes A size range string or an integer or an array of size ranges
*
* @return $this
*
* @see SizeRangeFilterIterator
* @see NumberComparator
*/
public function size(string|int|array $sizes) : static {
foreach ((array) $sizes as $size) {
$this->sizes[] = new NumberComparator($size);
}
return $this;
}
/**
* Excludes directories.
*
* Directories passed as argument must be relative to the ones defined with the `in()` method. For example:
*
* $finder->in(__DIR__)->exclude('ruby');
*
* @param string|array $dirs A directory path or an array of directories
*
* @return $this
*
* @see ExcludeDirectoryFilterIterator
*/
public function exclude(string|array $dirs) : static {
$this->exclude = array_merge($this->exclude, (array) $dirs);
return $this;
}
/**
* Excludes "hidden" directories and files (starting with a dot).
*
* This option is enabled by default.
*
* @return $this
*
* @see ExcludeDirectoryFilterIterator
*/
public function ignoreDotFiles(bool $ignoreDotFiles) : static {
if ($ignoreDotFiles) {
$this->ignore |= static::IGNORE_DOT_FILES;
}
else {
$this->ignore &= ~static::IGNORE_DOT_FILES;
}
return $this;
}
/**
* Forces the finder to ignore version control directories.
*
* This option is enabled by default.
*
* @return $this
*
* @see ExcludeDirectoryFilterIterator
*/
public function ignoreVCS(bool $ignoreVCS) : static {
if ($ignoreVCS) {
$this->ignore |= static::IGNORE_VCS_FILES;
}
else {
$this->ignore &= ~static::IGNORE_VCS_FILES;
}
return $this;
}
/**
* Forces Finder to obey .gitignore and ignore files based on rules listed there.
*
* This option is disabled by default.
*
* @return $this
*/
public function ignoreVCSIgnored(bool $ignoreVCSIgnored) : static {
if ($ignoreVCSIgnored) {
$this->ignore |= static::IGNORE_VCS_IGNORED_FILES;
}
else {
$this->ignore &= ~static::IGNORE_VCS_IGNORED_FILES;
}
return $this;
}
/**
* Adds VCS patterns.
*
* @see ignoreVCS()
*
* @param string|string[] $pattern VCS patterns to ignore
*/
public static function addVCSPattern(string|array $pattern) : void {
foreach ((array) $pattern as $p) {
self::$vcsPatterns[] = $p;
}
self::$vcsPatterns = array_unique(self::$vcsPatterns);
}
/**
* 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.
*
* @return $this
*
* @see SortableIterator
*/
public function sort(\Closure $closure) : static {
$this->sort = $closure;
return $this;
}
/**
* Sorts files and directories by extension.
*
* This can be slow as all the matching files and directories must be retrieved for comparison.
*
* @return $this
*
* @see SortableIterator
*/
public function sortByExtension() : static {
$this->sort = SortableIterator::SORT_BY_EXTENSION;
return $this;
}
/**
* Sorts files and directories by name.
*
* This can be slow as all the matching files and directories must be retrieved for comparison.
*
* @return $this
*
* @see SortableIterator
*/
public function sortByName(bool $useNaturalSort = false) : static {
$this->sort = $useNaturalSort ? SortableIterator::SORT_BY_NAME_NATURAL : SortableIterator::SORT_BY_NAME;
return $this;
}
/**
* Sorts files and directories by name case insensitive.
*
* This can be slow as all the matching files and directories must be retrieved for comparison.
*
* @return $this
*
* @see SortableIterator
*/
public function sortByCaseInsensitiveName(bool $useNaturalSort = false) : static {
$this->sort = $useNaturalSort ? SortableIterator::SORT_BY_NAME_NATURAL_CASE_INSENSITIVE : SortableIterator::SORT_BY_NAME_CASE_INSENSITIVE;
return $this;
}
/**
* Sorts files and directories by size.
*
* This can be slow as all the matching files and directories must be retrieved for comparison.
*
* @return $this
*
* @see SortableIterator
*/
public function sortBySize() : static {
$this->sort = SortableIterator::SORT_BY_SIZE;
return $this;
}
/**
* 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.
*
* @return $this
*
* @see SortableIterator
*/
public function sortByType() : static {
$this->sort = SortableIterator::SORT_BY_TYPE;
return $this;
}
/**
* 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.
*
* @return $this
*
* @see SortableIterator
*/
public function sortByAccessedTime() : static {
$this->sort = SortableIterator::SORT_BY_ACCESSED_TIME;
return $this;
}
/**
* Reverses the sorting.
*
* @return $this
*/
public function reverseSorting() : static {
$this->reverseSorting = true;
return $this;
}
/**
* 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.
*
* @return $this
*
* @see SortableIterator
*/
public function sortByChangedTime() : static {
$this->sort = SortableIterator::SORT_BY_CHANGED_TIME;
return $this;
}
/**
* 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.
*
* @return $this
*
* @see SortableIterator
*/
public function sortByModifiedTime() : static {
$this->sort = SortableIterator::SORT_BY_MODIFIED_TIME;
return $this;
}
/**
* Filters the iterator with an anonymous function.
*
* The anonymous function receives a \SplFileInfo and must return false
* to remove files.
*
* @param \Closure(SplFileInfo): bool $closure
* @param bool $prune Whether to skip traversing directories further
*
* @return $this
*
* @see CustomFilterIterator
*/
public function filter(\Closure $closure, bool $prune = false) : static {
$this->filters[] = $closure;
if ($prune) {
$this->pruneFilters[] = $closure;
}
return $this;
}
/**
* Forces the following of symlinks.
*
* @return $this
*/
public function followLinks() : static {
$this->followLinks = true;
return $this;
}
/**
* Tells finder to ignore unreadable directories.
*
* By default, scanning unreadable directories content throws an AccessDeniedException.
*
* @return $this
*/
public function ignoreUnreadableDirs(bool $ignore = true) : static {
$this->ignoreUnreadableDirs = $ignore;
return $this;
}
/**
* Searches files and directories which match defined rules.
*
* @param string|string[] $dirs A directory path or an array of directories
*
* @return $this
*
* @throws DirectoryNotFoundException if one of the directories does not exist
*/
public function in(string|array $dirs) : static {
$resolvedDirs = [];
foreach ((array) $dirs as $dir) {
if (is_dir($dir)) {
$resolvedDirs[] = [
$this->normalizeDir($dir),
];
}
elseif ($glob = glob($dir, (\defined('GLOB_BRACE') ? \GLOB_BRACE : 0) | \GLOB_ONLYDIR | \GLOB_NOSORT)) {
sort($glob);
$resolvedDirs[] = array_map($this->normalizeDir(...), $glob);
}
else {
throw new DirectoryNotFoundException(\sprintf('The "%s" directory does not exist.', $dir));
}
}
$this->dirs = array_merge($this->dirs, ...$resolvedDirs);
return $this;
}
/**
* Returns an Iterator for the current Finder configuration.
*
* This method implements the IteratorAggregate interface.
*
* @return \Iterator<string, SplFileInfo>
*
* @throws \LogicException if the in() method has not been called
*/
public function getIterator() : \Iterator {
if (0 === \count($this->dirs) && 0 === \count($this->iterators)) {
throw new \LogicException('You must call one of in() or append() methods before iterating over a Finder.');
}
if (1 === \count($this->dirs) && 0 === \count($this->iterators)) {
$iterator = $this->searchInDirectory($this->dirs[0]);
if ($this->sort || $this->reverseSorting) {
$iterator = (new SortableIterator($iterator, $this->sort, $this->reverseSorting))
->getIterator();
}
return $iterator;
}
$iterator = new \AppendIterator();
foreach ($this->dirs as $dir) {
$iterator->append(new \IteratorIterator(new LazyIterator(fn() => $this->searchInDirectory($dir))));
}
foreach ($this->iterators as $it) {
$iterator->append($it);
}
if ($this->sort || $this->reverseSorting) {
$iterator = (new SortableIterator($iterator, $this->sort, $this->reverseSorting))
->getIterator();
}
return $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.
*
* @return $this
*/
public function append(iterable $iterator) : static {
if ($iterator instanceof \IteratorAggregate) {
$this->iterators[] = $iterator->getIterator();
}
elseif ($iterator instanceof \Iterator) {
$this->iterators[] = $iterator;
}
else {
$it = new \ArrayIterator();
foreach ($iterator as $file) {
$file = $file instanceof \SplFileInfo ? $file : new \SplFileInfo($file);
$it[$file->getPathname()] = $file;
}
$this->iterators[] = $it;
}
return $this;
}
/**
* Check if any results were found.
*/
public function hasResults() : bool {
foreach ($this->getIterator() as $_) {
return true;
}
return false;
}
/**
* Counts all the results collected by the iterators.
*/
public function count() : int {
return iterator_count($this->getIterator());
}
private function searchInDirectory(string $dir) : \Iterator {
$exclude = $this->exclude;
$notPaths = $this->notPaths;
if ($this->pruneFilters) {
$exclude = array_merge($exclude, $this->pruneFilters);
}
if (static::IGNORE_VCS_FILES === (static::IGNORE_VCS_FILES & $this->ignore)) {
$exclude = array_merge($exclude, self::$vcsPatterns);
}
if (static::IGNORE_DOT_FILES === (static::IGNORE_DOT_FILES & $this->ignore)) {
$notPaths[] = '#(^|/)\\..+(/|$)#';
}
$minDepth = 0;
$maxDepth = \PHP_INT_MAX;
foreach ($this->depths as $comparator) {
switch ($comparator->getOperator()) {
case '>':
$minDepth = $comparator->getTarget() + 1;
break;
case '>=':
$minDepth = $comparator->getTarget();
break;
case '<':
$maxDepth = $comparator->getTarget() - 1;
break;
case '<=':
$maxDepth = $comparator->getTarget();
break;
default:
$minDepth = $maxDepth = $comparator->getTarget();
}
}
$flags = \RecursiveDirectoryIterator::SKIP_DOTS;
if ($this->followLinks) {
$flags |= \RecursiveDirectoryIterator::FOLLOW_SYMLINKS;
}
$iterator = new Iterator\RecursiveDirectoryIterator($dir, $flags, $this->ignoreUnreadableDirs);
if ($exclude) {
$iterator = new ExcludeDirectoryFilterIterator($iterator, $exclude);
}
$iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
if ($minDepth > 0 || $maxDepth < \PHP_INT_MAX) {
$iterator = new DepthRangeFilterIterator($iterator, $minDepth, $maxDepth);
}
if ($this->mode) {
$iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode);
}
if ($this->names || $this->notNames) {
$iterator = new FilenameFilterIterator($iterator, $this->names, $this->notNames);
}
if ($this->contains || $this->notContains) {
$iterator = new FilecontentFilterIterator($iterator, $this->contains, $this->notContains);
}
if ($this->sizes) {
$iterator = new SizeRangeFilterIterator($iterator, $this->sizes);
}
if ($this->dates) {
$iterator = new DateRangeFilterIterator($iterator, $this->dates);
}
if ($this->filters) {
$iterator = new CustomFilterIterator($iterator, $this->filters);
}
if ($this->paths || $notPaths) {
$iterator = new Iterator\PathFilterIterator($iterator, $this->paths, $notPaths);
}
if (static::IGNORE_VCS_IGNORED_FILES === (static::IGNORE_VCS_IGNORED_FILES & $this->ignore)) {
$iterator = new Iterator\VcsIgnoredFilterIterator($iterator, $dir);
}
return $iterator;
}
/**
* Normalizes given directory names by removing trailing slashes.
*
* Excluding: (s)ftp:// or ssh2.(s)ftp:// wrapper
*/
private function normalizeDir(string $dir) : string {
if ('/' === $dir) {
return $dir;
}
$dir = rtrim($dir, '/' . \DIRECTORY_SEPARATOR);
if (preg_match('#^(ssh2\\.)?s?ftp://#', $dir)) {
$dir .= '/';
}
return $dir;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
Finder::$contains | private | property | |
Finder::$dates | private | property | |
Finder::$depths | private | property | |
Finder::$dirs | private | property | |
Finder::$exclude | private | property | |
Finder::$filters | private | property | |
Finder::$followLinks | private | property | |
Finder::$ignore | private | property | |
Finder::$ignoreUnreadableDirs | private | property | |
Finder::$iterators | private | property | |
Finder::$mode | private | property | |
Finder::$names | private | property | |
Finder::$notContains | private | property | |
Finder::$notNames | private | property | |
Finder::$notPaths | private | property | |
Finder::$paths | private | property | |
Finder::$pruneFilters | private | property | |
Finder::$reverseSorting | private | property | |
Finder::$sizes | private | property | |
Finder::$sort | private | property | |
Finder::$vcsPatterns | private static | property | |
Finder::addVCSPattern | public static | function | Adds VCS patterns. |
Finder::append | public | function | Appends an existing set of files/directories to the finder. |
Finder::contains | public | function | Adds tests that file contents must match. |
Finder::count | public | function | Counts all the results collected by the iterators. |
Finder::create | public static | function | Creates a new Finder. |
Finder::date | public | function | Adds tests for file dates (last modified). |
Finder::depth | public | function | Adds tests for the directory depth. |
Finder::directories | public | function | Restricts the matching to directories only. |
Finder::exclude | public | function | Excludes directories. |
Finder::files | public | function | Restricts the matching to files only. |
Finder::filter | public | function | Filters the iterator with an anonymous function. |
Finder::followLinks | public | function | Forces the following of symlinks. |
Finder::getIterator | public | function | Returns an Iterator for the current Finder configuration. |
Finder::hasResults | public | function | Check if any results were found. |
Finder::ignoreDotFiles | public | function | Excludes "hidden" directories and files (starting with a dot). |
Finder::ignoreUnreadableDirs | public | function | Tells finder to ignore unreadable directories. |
Finder::ignoreVCS | public | function | Forces the finder to ignore version control directories. |
Finder::ignoreVCSIgnored | public | function | Forces Finder to obey .gitignore and ignore files based on rules listed there. |
Finder::IGNORE_DOT_FILES | public | constant | |
Finder::IGNORE_VCS_FILES | public | constant | |
Finder::IGNORE_VCS_IGNORED_FILES | public | constant | |
Finder::in | public | function | Searches files and directories which match defined rules. |
Finder::name | public | function | Adds rules that files must match. |
Finder::normalizeDir | private | function | Normalizes given directory names by removing trailing slashes. |
Finder::notContains | public | function | Adds tests that file contents must not match. |
Finder::notName | public | function | Adds rules that files must not match. |
Finder::notPath | public | function | Adds rules that filenames must not match. |
Finder::path | public | function | Adds rules that filenames must match. |
Finder::reverseSorting | public | function | Reverses the sorting. |
Finder::searchInDirectory | private | function | |
Finder::size | public | function | Adds tests for file sizes. |
Finder::sort | public | function | Sorts files and directories by an anonymous function. |
Finder::sortByAccessedTime | public | function | Sorts files and directories by the last accessed time. |
Finder::sortByCaseInsensitiveName | public | function | Sorts files and directories by name case insensitive. |
Finder::sortByChangedTime | public | function | Sorts files and directories by the last inode changed time. |
Finder::sortByExtension | public | function | Sorts files and directories by extension. |
Finder::sortByModifiedTime | public | function | Sorts files and directories by the last modified time. |
Finder::sortByName | public | function | Sorts files and directories by name. |
Finder::sortBySize | public | function | Sorts files and directories by size. |
Finder::sortByType | public | function | Sorts files and directories by type (directories before files), then by name. |
Finder::__construct | public | function |