CodeIgniter v4.5 API

FileCollection
in package
implements Countable, IteratorAggregate

File Collection Class

Representation for a group of files, with utilities for locating, filtering, and ordering them.

Tags
template-implements

IteratorAggregate<int, File>

see
FileCollectionTest

Table of Contents

Interfaces

Countable
IteratorAggregate

Properties

$files  : array<int, string>
The current list of file paths.

Methods

__construct()  : mixed
Loads the Filesystem helper and adds any initial files.
add()  : $this
Adds an array/single file or directory to the list.
addDirectories()  : $this
Verifies and adds files from each directory to the list.
addDirectory()  : $this
Verifies and adds all files from a directory.
addFile()  : $this
Verifies and adds a single file to the file list.
addFiles()  : $this
Verifies and adds files to the list.
count()  : int
Returns the current number of files in the collection.
get()  : array<int, string>
Optimizes and returns the current file list.
getIterator()  : Generator<string|int, File>
Yields as an Iterator for the current files.
removeFile()  : $this
Removes a single file from the list.
removeFiles()  : $this
Removes files from the list.
removePattern()  : $this
Removes any files from the list that match the supplied pattern (within the optional scope).
retainPattern()  : $this
Keeps only the files from the list that match (within the optional scope).
set()  : $this
Sets the file list directly, files are still subject to verification.
define()  : void
Applies any initial inputs after the constructor.
filterFiles()  : array<int, string>
Removes files that are not part of the given directory (recursive).
matchFiles()  : array<int, string>
Returns any files whose `basename` matches the given pattern.
resolveDirectory()  : string
Resolves a full path and verifies it is an actual directory.
resolveFile()  : string
Resolves a full path and verifies it is an actual file.

Properties

$files

The current list of file paths.

protected array<int, string> $files = []

Methods

__construct()

Loads the Filesystem helper and adds any initial files.

public __construct([array<int, string> $files = [] ]) : mixed
Parameters
$files : array<int, string> = []

add()

Adds an array/single file or directory to the list.

public add(array<int, string>|string $paths[, bool $recursive = true ]) : $this
Parameters
$paths : array<int, string>|string
$recursive : bool = true
Return values
$this

addDirectories()

Verifies and adds files from each directory to the list.

public addDirectories(array<int, string> $directories[, bool $recursive = false ]) : $this
Parameters
$directories : array<int, string>
$recursive : bool = false
Return values
$this

addDirectory()

Verifies and adds all files from a directory.

public addDirectory(string $directory[, bool $recursive = false ]) : $this
Parameters
$directory : string
$recursive : bool = false
Return values
$this

addFile()

Verifies and adds a single file to the file list.

public addFile(string $file) : $this
Parameters
$file : string
Return values
$this

addFiles()

Verifies and adds files to the list.

public addFiles(array<int, string> $files) : $this
Parameters
$files : array<int, string>
Return values
$this

count()

Returns the current number of files in the collection.

public count() : int

Fulfills Countable.

Return values
int

get()

Optimizes and returns the current file list.

public get() : array<int, string>
Return values
array<int, string>

getIterator()

Yields as an Iterator for the current files.

public getIterator() : Generator<string|int, File>

Fulfills IteratorAggregate.

Tags
throws
FileNotFoundException
Return values
Generator<string|int, File>

removeFile()

Removes a single file from the list.

public removeFile(string $file) : $this
Parameters
$file : string
Return values
$this

removeFiles()

Removes files from the list.

public removeFiles(array<int, string> $files) : $this
Parameters
$files : array<int, string>
Return values
$this

removePattern()

Removes any files from the list that match the supplied pattern (within the optional scope).

public removePattern(string $pattern[, string|null $scope = null ]) : $this
Parameters
$pattern : string

Regex or pseudo-regex string

$scope : string|null = null

The directory to limit the scope

Return values
$this

retainPattern()

Keeps only the files from the list that match (within the optional scope).

public retainPattern(string $pattern[, string|null $scope = null ]) : $this
Parameters
$pattern : string

Regex or pseudo-regex string

$scope : string|null = null

A directory to limit the scope

Return values
$this

set()

Sets the file list directly, files are still subject to verification.

public set(array<int, string> $files) : $this

This works as a "reset" method with [].

Parameters
$files : array<int, string>

The new file list to use

Return values
$this

define()

Applies any initial inputs after the constructor.

protected define() : void

This method is a stub to be implemented by child classes.

filterFiles()

Removes files that are not part of the given directory (recursive).

protected final static filterFiles(array<int, string> $files, string $directory) : array<int, string>
Parameters
$files : array<int, string>
$directory : string
Return values
array<int, string>

matchFiles()

Returns any files whose `basename` matches the given pattern.

protected final static matchFiles(array<int, string> $files, string $pattern) : array<int, string>
Parameters
$files : array<int, string>
$pattern : string

Regex or pseudo-regex string

Return values
array<int, string>

resolveDirectory()

Resolves a full path and verifies it is an actual directory.

protected final static resolveDirectory(string $directory) : string
Parameters
$directory : string
Tags
throws
FileException
Return values
string

resolveFile()

Resolves a full path and verifies it is an actual file.

protected final static resolveFile(string $file) : string
Parameters
$file : string
Tags
throws
FileException
Return values
string

        
On this page

Search results