\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 easy 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()
addVCSPattern()
sort()
sortByName()
sortByType()
sortByAccessedTime()
sortByChangedTime()
sortByModifiedTime()
filter()
followLinks()
ignoreUnreadableDirs()
in()
getIterator()
append()
hasResults()
count()
No public properties found
IGNORE_VCS_FILES
IGNORE_DOT_FILES
No protected methods found
No protected properties found
N/A
searchInDirectory()
normalizeDir()
$mode
$names
$notNames
$exclude
$filters
$depths
$sizes
$followLinks
$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

Properties

$mode

$mode : 

Type

$names

$names : 

Type

$notNames

$notNames : 

Type

$exclude

$exclude : 

Type

$filters

$filters : 

Type

$depths

$depths : 

Type

$sizes

$sizes : 

Type

$followLinks

$followLinks : 

Type

$sort

$sort : 

Type

$ignore

$ignore : 

Type

$dirs

$dirs : 

Type

$dates

$dates : 

Type

$iterators

$iterators : 

Type

$contains

$contains : 

Type

$notContains

$notContains : 

Type

$paths

$paths : 

Type

$notPaths

$notPaths : 

Type

$ignoreUnreadableDirs

$ignoreUnreadableDirs : 

Type

$vcsPatterns

$vcsPatterns : 

Type

Methods

__construct()

__construct() 

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|integer  $level) : $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.

Parameters

string|integer $level

The depth level expression

Returns

$this

date()

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

Parameters

string $date

A date range string

Returns

$this

name()

name(string  $pattern) : $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')

Parameters

string $pattern

A pattern (a regexp, a glob, or a string)

Returns

$this

notName()

notName(string  $pattern) : $this

Adds rules that files must not match.

Parameters

string $pattern

A pattern (a regexp, a glob, or a string)

Returns

$this

contains()

contains(string  $pattern) : $this

Adds tests that file contents must match.

Strings or PCRE patterns can be used:

$finder->contains('Lorem ipsum')
$finder->contains('/Lorem ipsum/i')

Parameters

string $pattern

A pattern (string or regexp)

Returns

$this

notContains()

notContains(string  $pattern) : $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')

Parameters

string $pattern

A pattern (string or regexp)

Returns

$this

path()

path(string  $pattern) : $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

Use only / as dirname separator.

Parameters

string $pattern

A pattern (a regexp or a string)

Returns

$this

notPath()

notPath(string  $pattern) : $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

Use only / as dirname separator.

Parameters

string $pattern

A pattern (a regexp or a string)

Returns

$this

size()

size(string|integer  $size) : $this

Adds tests for file sizes.

$finder->size('> 10K'); $finder->size('<= 1Ki'); $finder->size(4);

Parameters

string|integer $size

A size range string or an integer

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(boolean  $ignoreDotFiles) : $this

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

This option is enabled by default.

Parameters

boolean $ignoreDotFiles

Whether to exclude "hidden" files or not

Returns

$this

ignoreVCS()

ignoreVCS(boolean  $ignoreVCS) : $this

Forces the finder to ignore version control directories.

This option is enabled by default.

Parameters

boolean $ignoreVCS

Whether to exclude VCS files or not

Returns

$this

addVCSPattern()

addVCSPattern(string|array<mixed,string>  $pattern) 

Adds VCS patterns.

Parameters

string|array<mixed,string> $pattern

VCS patterns to ignore

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

Sorts files and directories by name.

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

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

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(boolean  $ignore = true) : $this

Tells finder to ignore unreadable directories.

By default, scanning unreadable directories content throws an AccessDeniedException.

Parameters

boolean $ignore

Returns

$this

in()

in(string|array  $dirs) : $this

Searches files and directories which match defined rules.

Parameters

string|array $dirs

A directory path or an array of directories

Throws

\InvalidArgumentException

if one of the directories does not exist

Returns

$this

getIterator()

getIterator() : \Iterator|array<mixed,\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|array<mixed,\Symfony\Component\Finder\SplFileInfo> —

An iterator

append()

append(\Symfony\Component\Finder\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

\Symfony\Component\Finder\iterable $iterator

Throws

\InvalidArgumentException

when the given argument is not iterable

Returns

$this

hasResults()

hasResults() : boolean

Check if the any results were found.

Returns

boolean

count()

count() : integer

Counts all the results collected by the iterators.

Returns

integer

searchInDirectory()

searchInDirectory(\Symfony\Component\Finder\string  $dir) 

Parameters

\Symfony\Component\Finder\string $dir

normalizeDir()

normalizeDir(string  $dir) : string

Normalizes given directory names by removing trailing slashes.

Excluding: (s)ftp:// wrapper

Parameters

string $dir

Returns

string