\Symfony\Component\FinderFinder

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__);

Summary

Methods
Properties
Constants
__construct()
create()
directories()
files()
depth()
date()
name()
notName()
contains()
notContains()
path()
notPath()
size()
exclude()
ignoreDotFiles()
ignoreVCS()
ignoreVCSIgnored()
addVCSPattern()
sort()
sortByName()
sortByType()
sortByAccessedTime()
reverseSorting()
sortByChangedTime()
sortByModifiedTime()
filter()
followLinks()
ignoreUnreadableDirs()
in()
getIterator()
append()
hasResults()
count()
No public properties found
IGNORE_VCS_FILES
IGNORE_DOT_FILES
IGNORE_VCS_IGNORED_FILES
No protected methods found
No protected properties found
N/A
searchInDirectory()
normalizeDir()
$mode
$names
$notNames
$exclude
$filters
$depths
$sizes
$followLinks
$reverseSorting
$sort
$ignore
$dirs
$dates
$iterators
$contains
$notContains
$paths
$notPaths
$ignoreUnreadableDirs
$vcsPatterns
N/A

Constants

IGNORE_VCS_FILES

IGNORE_VCS_FILES = 1

IGNORE_DOT_FILES

IGNORE_DOT_FILES = 2

IGNORE_VCS_IGNORED_FILES

IGNORE_VCS_IGNORED_FILES = 4

Properties

$mode

$mode

$names

$names

$notNames

$notNames

$exclude

$exclude

$filters

$filters

$depths

$depths

$sizes

$sizes

$followLinks

$followLinks

$reverseSorting

$reverseSorting

$sort

$sort

$ignore

$ignore

$dirs

$dirs

$dates

$dates

$iterators

$iterators

$contains

$contains

$notContains

$notContains

$paths

$paths

$notPaths

$notPaths

$ignoreUnreadableDirs

$ignoreUnreadableDirs

$vcsPatterns

$vcsPatterns

Methods

__construct()

__construct() : mixed

Returns

mixed —

create()

create() : static

Creates a new Finder.

Returns

static —

directories()

directories() : $this

Restricts the matching to directories only.

Returns

$this —

files()

files() : $this

Restricts the matching to files only.

Returns

$this —

depth()

depth(string|int|string[]|int[]  $levels) : $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'])

Parameters

string|int|string[]|int[] $levels

The depth level expression or an array of depth levels

Returns

$this —

date()

date(string|string[]  $dates) : $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']);

Parameters

string|string[] $dates

A date range string or an array of date ranges

Returns

$this —

name()

name(string|string[]  $patterns) : $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 $finder->name('test.php') $finder->name(['test.py', 'test.php'])

Parameters

string|string[] $patterns

A pattern (a regexp, a glob, or a string) or an array of patterns

Returns

$this —

notName()

notName(string|string[]  $patterns) : $this

Adds rules that files must not match.

Parameters

string|string[] $patterns

A pattern (a regexp, a glob, or a string) or an array of patterns

Returns

$this —

contains()

contains(string|string[]  $patterns) : $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'])

Parameters

string|string[] $patterns

A pattern (string or regexp) or an array of patterns

Returns

$this —

notContains()

notContains(string|string[]  $patterns) : $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'])

Parameters

string|string[] $patterns

A pattern (string or regexp) or an array of patterns

Returns

$this —

path()

path(string|string[]  $patterns) : $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.

Parameters

string|string[] $patterns

A pattern (a regexp or a string) or an array of patterns

Returns

$this —

notPath()

notPath(string|string[]  $patterns) : $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.

Parameters

string|string[] $patterns

A pattern (a regexp or a string) or an array of patterns

Returns

$this —

size()

size(string|int|string[]|int[]  $sizes) : $this

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

$this —

exclude()

exclude(string|array  $dirs) : $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');

Parameters

string|array $dirs

A directory path or an array of directories

Returns

$this —

ignoreDotFiles()

ignoreDotFiles(bool  $ignoreDotFiles) : $this

Excludes "hidden" directories and files (starting with a dot).

This option is enabled by default.

Parameters

bool $ignoreDotFiles

Returns

$this —

ignoreVCS()

ignoreVCS(bool  $ignoreVCS) : $this

Forces the finder to ignore version control directories.

This option is enabled by default.

Parameters

bool $ignoreVCS

Returns

$this —

ignoreVCSIgnored()

ignoreVCSIgnored(bool  $ignoreVCSIgnored) : $this

Forces Finder to obey .gitignore and ignore files based on rules listed there.

This option is disabled by default.

Parameters

bool $ignoreVCSIgnored

Returns

$this —

addVCSPattern()

addVCSPattern(string|string[]  $pattern) : mixed

Adds VCS patterns.

Parameters

string|string[] $pattern

VCS patterns to ignore

Returns

mixed —

sort()

sort(\Closure  $closure) : $this

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.

Parameters

\Closure $closure

Returns

$this —

sortByName()

sortByName(bool  $useNaturalSort = false) : $this

Sorts files and directories by name.

This can be slow as all the matching files and directories must be retrieved for comparison.

Parameters

bool $useNaturalSort

Returns

$this —

sortByType()

sortByType() : $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.

Returns

$this —

sortByAccessedTime()

sortByAccessedTime() : $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.

Returns

$this —

reverseSorting()

reverseSorting() : $this

Reverses the sorting.

Returns

$this —

sortByChangedTime()

sortByChangedTime() : $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.

Returns

$this —

sortByModifiedTime()

sortByModifiedTime() : $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.

Returns

$this —

filter()

filter(\Closure  $closure) : $this

Filters the iterator with an anonymous function.

The anonymous function receives a \SplFileInfo and must return false to remove files.

Parameters

\Closure $closure

Returns

$this —

followLinks()

followLinks() : $this

Forces the following of symlinks.

Returns

$this —

ignoreUnreadableDirs()

ignoreUnreadableDirs(bool  $ignore = true) : $this

Tells finder to ignore unreadable directories.

By default, scanning unreadable directories content throws an AccessDeniedException.

Parameters

bool $ignore

Returns

$this —

in()

in(string|string[]  $dirs) : $this

Searches files and directories which match defined rules.

Parameters

string|string[] $dirs

A directory path or an array of directories

Throws

\Symfony\Component\Finder\Exception\DirectoryNotFoundException

if one of the directories does not exist

Returns

$this —

getIterator()

getIterator() : \Iterator<string,\Symfony\Component\Finder\SplFileInfo>

Returns an Iterator for the current Finder configuration.

This method implements the IteratorAggregate interface.

Throws

\LogicException

if the in() method has not been called

Returns

\Iterator

append()

append(iterable  $iterator) : $this

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

Throws

\InvalidArgumentException

when the given argument is not iterable

Returns

$this —

hasResults()

hasResults() : bool

Check if any results were found.

Returns

bool —

count()

count() : int

Counts all the results collected by the iterators.

Returns

int —

searchInDirectory()

searchInDirectory(string  $dir) : \Iterator

Parameters

string $dir

Returns

\Iterator —

normalizeDir()

normalizeDir(string  $dir) : string

Normalizes given directory names by removing trailing slashes.

Excluding: (s)ftp:// or ssh2.(s)ftp:// wrapper

Parameters

string $dir

Returns

string —