CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 6.0 API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 6.0
      • 6.0
      • 5.4
      • 5.3
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • AttributeResolver
    • Cache
    • Collection
    • Command
    • Console
    • Container
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
    • I18n
    • Lock
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
      • Crypto
      • Exception
      • Fs
        • Iterator
    • Validation
    • View

Class Finder

Finder provides a fluent interface for finding files and directories.

Example usage:

// Find files
$finder = new Finder();
$files = $finder
    ->in('src')
    ->name('*.php')
    ->exclude('vendor')
    ->files();

foreach ($files as $file) {
    echo $file->getPathname();
}

// Find directories
$directories = (new Finder())
    ->in('src')
    ->exclude('vendor')
    ->directories();

// Find both files and directories
$all = (new Finder())
    ->in('src')
    ->all();
Namespace: Cake\Utility\Fs

Property Summary

  • $depths protected
    array<array{0: \Cake\Utility\Fs\Enum\DepthOperator, 1: int}>

    Depth conditions

  • $exclude protected
    array<string>

    Directories to exclude

  • $filters protected
    array<Closure(SplFileInfo, string): bool>

    Custom filter callbacks

  • $followLinks protected
    bool

    Whether to follow symbolic links to directories

  • $globPatterns protected
    array<string>

    Glob patterns for full path matching

  • $ignoreHiddenFiles protected
    bool

    Whether to ignore hidden files

  • $mode protected
    Cake\Utility\Fs\Enum\FinderMode|null

    The iteration mode (files, directories, or all)

  • $names protected
    array<string>

    Name patterns to match

  • $notNames protected
    array<string>

    Name patterns to exclude

  • $notPathPatterns protected
    array<string>

    Path patterns to exclude

  • $pathPatterns protected
    array<string>

    Path patterns to include

  • $paths protected
    array<string>

    Base paths to search in

  • $recursive protected
    bool

    Whether to search recursively

Method Summary

  • all() public

    Get both files and directories matching the criteria.

  • buildIterator() protected

    Build an iterator chain with all configured filters.

  • depth() public

    Add a depth condition.

  • directories() public

    Get directories matching the criteria.

  • exclude() public

    Exclude a directory from the search.

  • files() public

    Get files matching the criteria.

  • filter() public

    Add a custom filter callback.

    The callback receives the SplFileInfo object and relative path, and should return true to include the file.

    Example:

    $finder->filter(fn(\SplFileInfo $file) => $file->getSize() > 1024);
  • followLinks() public

    Set whether to follow symbolic links to directories.

    By default, symbolic links to directories are returned as entries but are not descended into. When enabled, linked directories are traversed as well and are subject to the same filters, including the depth conditions, as any other entry.

    Links that would cause an infinite loop (for example, a link pointing to one of the directories currently being traversed) are returned as entries but never descended into.

  • ignoreHiddenFiles() public

    Set whether to ignore hidden files and directories.

  • in() public

    Add a path to search in.

  • iterate() protected

    Iterate over items matching the criteria.

  • name() public

    Add a name pattern to match.

  • notName() public

    Add a name pattern that must not be matched.

  • notPath() public

    Add a path pattern that must not be matched.

  • path() public

    Add a path pattern that must be matched.

  • pattern() public

    Add a glob pattern for full path matching.

    Supports wildcards like src/**\/*.php for recursive matching.

  • recursive() public

    Set whether to search recursively into subdirectories.

Method Detail

all() ¶ public

all(): Iterator<SplFileInfo>

Get both files and directories matching the criteria.

Returns
Iterator<SplFileInfo>

buildIterator() ¶ protected

buildIterator(string $path): Iterator<SplFileInfo>

Build an iterator chain with all configured filters.

Parameters
string $path

The directory path

Returns
Iterator<SplFileInfo>

depth() ¶ public

depth(int $level, Cake\Utility\Fs\Enum\DepthOperator $operator = DepthOperator::EQUAL): $this

Add a depth condition.

Parameters
int $level

The depth level (0 = top-level directory)

Cake\Utility\Fs\Enum\DepthOperator $operator optional

The comparison operator (default: EQUAL)

Returns
$this

directories() ¶ public

directories(): Iterator<SplFileInfo>

Get directories matching the criteria.

Returns
Iterator<SplFileInfo>

exclude() ¶ public

exclude(string $directory): $this

Exclude a directory from the search.

Parameters
string $directory

Directory name to exclude

Returns
$this

files() ¶ public

files(): Iterator<SplFileInfo>

Get files matching the criteria.

Returns
Iterator<SplFileInfo>

filter() ¶ public

filter(Closure(SplFileInfo, string): bool $callback): $this

Add a custom filter callback.

The callback receives the SplFileInfo object and relative path, and should return true to include the file.

Example:

$finder->filter(fn(\SplFileInfo $file) => $file->getSize() > 1024);
Parameters
Closure(SplFileInfo, string): bool $callback

Filter callback

Returns
$this

followLinks() ¶ public

followLinks(bool $follow = true): $this

Set whether to follow symbolic links to directories.

By default, symbolic links to directories are returned as entries but are not descended into. When enabled, linked directories are traversed as well and are subject to the same filters, including the depth conditions, as any other entry.

Links that would cause an infinite loop (for example, a link pointing to one of the directories currently being traversed) are returned as entries but never descended into.

Parameters
bool $follow optional

Whether to follow symbolic links (default: true)

Returns
$this

ignoreHiddenFiles() ¶ public

ignoreHiddenFiles(bool $ignore = true): $this

Set whether to ignore hidden files and directories.

Parameters
bool $ignore optional

Whether to ignore hidden files

Returns
$this

in() ¶ public

in(string $path): $this

Add a path to search in.

Parameters
string $path

The directory path

Returns
$this

iterate() ¶ protected

iterate(): Iterator<SplFileInfo>

Iterate over items matching the criteria.

Returns
Iterator<SplFileInfo>

name() ¶ public

name(string $pattern): $this

Add a name pattern to match.

Parameters
string $pattern

Glob pattern (e.g., '*.php')

Returns
$this

notName() ¶ public

notName(string $pattern): $this

Add a name pattern that must not be matched.

Parameters
string $pattern

Glob pattern to exclude (e.g., '.rb', 'Test.php')

Returns
$this

notPath() ¶ public

notPath(string $pattern): $this

Add a path pattern that must not be matched.

Parameters
string $pattern

Path pattern to exclude

Returns
$this

path() ¶ public

path(string $pattern): $this

Add a path pattern that must be matched.

Parameters
string $pattern

Path pattern (e.g., 'Controller')

Returns
$this

pattern() ¶ public

pattern(string $pattern): $this

Add a glob pattern for full path matching.

Supports wildcards like src/**\/*.php for recursive matching.

Parameters
string $pattern

Glob pattern (e.g., 'src/*\/.php', 'tests/*\/Test.php')

Returns
$this

recursive() ¶ public

recursive(bool $recursive = true): $this

Set whether to search recursively into subdirectories.

Parameters
bool $recursive optional

Whether to search recursively (default: true)

Returns
$this

Property Detail

$depths ¶ protected

Depth conditions

Type
array<array{0: \Cake\Utility\Fs\Enum\DepthOperator, 1: int}>

$exclude ¶ protected

Directories to exclude

Type
array<string>

$filters ¶ protected

Custom filter callbacks

Type
array<Closure(SplFileInfo, string): bool>

$followLinks ¶ protected

Whether to follow symbolic links to directories

Type
bool

$globPatterns ¶ protected

Glob patterns for full path matching

Type
array<string>

$ignoreHiddenFiles ¶ protected

Whether to ignore hidden files

Type
bool

$mode ¶ protected

The iteration mode (files, directories, or all)

Type
Cake\Utility\Fs\Enum\FinderMode|null

$names ¶ protected

Name patterns to match

Type
array<string>

$notNames ¶ protected

Name patterns to exclude

Type
array<string>

$notPathPatterns ¶ protected

Path patterns to exclude

Type
array<string>

$pathPatterns ¶ protected

Path patterns to include

Type
array<string>

$paths ¶ protected

Base paths to search in

Type
array<string>

$recursive ¶ protected

Whether to search recursively

Type
bool
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs