class Filesystem

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable

Methods

mixed
when(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

mixed
unless(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

static void
macro(string $name, callable|object $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Check if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically handle calls to the class.

bool
exists(string $path)

Determine if a file or directory exists.

bool
missing(string $path)

Determine if a file or directory is missing.

string
get(string $path, bool $lock = false)

Get the contents of a file.

mixed
json(string $path, int $flags = 0, bool $lock = false)

Get the contents of a file as decoded JSON.

string
sharedGet(string $path)

Get contents of a file with shared access.

mixed
getRequire(string $path, array $data = [])

Get the returned value of a file.

mixed
requireOnce(string $path, array $data = [])

Require the given file once.

lines(string $path)

Get the contents of a file one line at a time.

string|false
hash(string $path, string $algorithm = 'xxh128')

Get the hash of the file at the given path.

int|false
put(string $path, resource|string $contents, bool $lock = false)

Write the contents of a file.

void
replace(string $path, string $content, int|null $mode = null)

Write the contents of a file, replacing it atomically if it already exists.

void
replaceInFile(array|string $search, array|string $replace, string $path)

Replace a given string within a given file.

int|false
prepend(string $path, string $data)

Prepend to a file.

int|false
append(string $path, string $data, bool $lock = false)

Append to a file.

string|bool
chmod(string $path, int|null $mode = null)

Get or set UNIX mode of a file or directory.

bool
delete(array|string $paths)

Delete the file at a given path.

bool
move(string $path, string $target)

Move a file to a new location.

bool
copy(string $path, string $target)

Copy a file to a new location.

bool|null
link(string $target, string $link)

Create a symlink to the target file or directory. On Windows, a hard link is created if the target is a file.

void
relativeLink(string $target, string $link)

Create a relative symlink to the target file or directory.

string
name(string $path)

Extract the file name from a file path.

string
basename(string $path)

Extract the trailing name component from a file path.

string
dirname(string $path)

Extract the parent directory from a file path.

string
extension(string $path)

Extract the file extension from a file path.

string|null
guessExtension(string $path)

Guess the file extension from the MIME type of a given file.

string|false
type(string $path)

Get the file type of a given file.

string|false
mimeType(string $path)

Get the MIME type of a given file.

int|false
size(string $path)

Get the file size of a given file.

int|false
lastModified(string $path)

Get the file's last modification time.

bool
isDirectory(string $directory)

Determine if the given path is a directory.

bool
isEmptyDirectory(string $directory, bool $ignoreDotFiles = false)

Determine if the given path is a directory that does not contain any other files or directories.

bool
isReadable(string $path)

Determine if the given path is readable.

bool
isWritable(string $path)

Determine if the given path is writable.

bool
hasSameHash(string $firstFile, string $secondFile)

Determine if two files are the same by comparing their hashes.

bool
isFile(string $file)

Determine if the given path is a file.

array|false
glob(string $pattern, int $flags = 0)

Find path names matching a given pattern.

array
files(array|string $directory, bool $hidden = false, array|string|int $depth = 0)

Get an array of all files in a directory.

array
allFiles(array|string $directory, bool $hidden = false)

Get all of the files from the given directory (recursive).

array
directories(array|string $directory, array|string|int $depth = 0)

Get all of the directories within a given directory.

array
allDirectories(array|string $directory)

Get all the directories within a given directory (recursive).

void
ensureDirectoryExists(string $path, int $mode = 0755, bool $recursive = true)

Ensure a directory exists.

bool
makeDirectory(string $path, int $mode = 0755, bool $recursive = false, bool $force = false)

Create a directory.

bool
moveDirectory(string $from, string $to, bool $overwrite = false)

Move a directory.

bool
copyDirectory(string $directory, string $destination, int|null $options = null)

Copy a directory from one location to another.

bool
deleteDirectory(string $directory, bool $preserve = false)

Recursively delete a directory.

bool
deleteDirectories(string $directory)

Remove all of the directories within a given directory.

bool
cleanDirectory(string $directory)

Empty the specified directory of all files and folders.

void
clearStatCache(string $path)

Clear file status cache.

static void
flushState()

Flush all static state.

Details

in Conditionable at line 23
mixed when(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

Parameters

mixed $value
callable|null $callback
callable|null $default

Return Value

mixed

in Conditionable at line 56
mixed unless(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

Parameters

mixed $value
callable|null $callback
callable|null $default

Return Value

mixed

in Macroable at line 28
static void macro(string $name, callable|object $macro)

Register a custom macro.

Boot-only. Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.

Parameters

string $name
callable|object $macro

Return Value

void

in Macroable at line 41
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Boot-only. Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

in Macroable at line 57
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 68
static void flushMacros()

Flush the existing macros.

Boot or tests only. Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.

Return Value

void

in Macroable at line 78
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

in Macroable at line 103
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

at line 32
bool exists(string $path)

Determine if a file or directory exists.

Parameters

string $path

Return Value

bool

at line 40
bool missing(string $path)

Determine if a file or directory is missing.

Parameters

string $path

Return Value

bool

at line 50
string get(string $path, bool $lock = false)

Get the contents of a file.

Parameters

string $path
bool $lock

Return Value

string

Exceptions

FileNotFoundException

at line 70
mixed json(string $path, int $flags = 0, bool $lock = false)

Get the contents of a file as decoded JSON.

Parameters

string $path
int $flags
bool $lock

Return Value

mixed

Exceptions

FileNotFoundException

at line 78
string sharedGet(string $path)

Get contents of a file with shared access.

Parameters

string $path

Return Value

string

at line 136
mixed getRequire(string $path, array $data = [])

Get the returned value of a file.

Parameters

string $path
array $data

Return Value

mixed

Exceptions

FileNotFoundException

at line 157
mixed requireOnce(string $path, array $data = [])

Require the given file once.

Parameters

string $path
array $data

Return Value

mixed

Exceptions

FileNotFoundException

at line 178
LazyCollection lines(string $path)

Get the contents of a file one line at a time.

Parameters

string $path

Return Value

LazyCollection

Exceptions

FileNotFoundException

at line 203
string|false hash(string $path, string $algorithm = 'xxh128')

Get the hash of the file at the given path.

Defaults to xxh128 for fast app-internal file hashing. Pass a specific algorithm explicitly when an external protocol requires one.

Parameters

string $path
string $algorithm

Return Value

string|false

at line 213
int|false put(string $path, resource|string $contents, bool $lock = false)

Write the contents of a file.

Parameters

string $path
resource|string $contents
bool $lock

Return Value

int|false

at line 221
void replace(string $path, string $content, int|null $mode = null)

Write the contents of a file, replacing it atomically if it already exists.

Parameters

string $path
string $content
int|null $mode

Return Value

void

at line 257
void replaceInFile(array|string $search, array|string $replace, string $path)

Replace a given string within a given file.

Parameters

array|string $search
array|string $replace
string $path

Return Value

void

at line 275
int|false prepend(string $path, string $data)

Prepend to a file.

Parameters

string $path
string $data

Return Value

int|false

at line 287
int|false append(string $path, string $data, bool $lock = false)

Append to a file.

Parameters

string $path
string $data
bool $lock

Return Value

int|false

at line 295
string|bool chmod(string $path, int|null $mode = null)

Get or set UNIX mode of a file or directory.

Parameters

string $path
int|null $mode

Return Value

string|bool

at line 311
bool delete(array|string $paths)

Delete the file at a given path.

Parameters

array|string $paths

Return Value

bool

at line 335
bool move(string $path, string $target)

Move a file to a new location.

Parameters

string $path
string $target

Return Value

bool

at line 343
bool copy(string $path, string $target)

Copy a file to a new location.

Parameters

string $path
string $target

Return Value

bool

Create a symlink to the target file or directory. On Windows, a hard link is created if the target is a file.

Parameters

string $target
string $link

Return Value

bool|null

Create a relative symlink to the target file or directory.

Parameters

string $target
string $link

Return Value

void

Exceptions

RuntimeException

at line 392
string name(string $path)

Extract the file name from a file path.

Parameters

string $path

Return Value

string

at line 400
string basename(string $path)

Extract the trailing name component from a file path.

Parameters

string $path

Return Value

string

at line 408
string dirname(string $path)

Extract the parent directory from a file path.

Parameters

string $path

Return Value

string

at line 416
string extension(string $path)

Extract the file extension from a file path.

Parameters

string $path

Return Value

string

at line 426
string|null guessExtension(string $path)

Guess the file extension from the MIME type of a given file.

Parameters

string $path

Return Value

string|null

Exceptions

RuntimeException

at line 442
string|false type(string $path)

Get the file type of a given file.

Parameters

string $path

Return Value

string|false

at line 450
string|false mimeType(string $path)

Get the MIME type of a given file.

Parameters

string $path

Return Value

string|false

at line 460
int|false size(string $path)

Get the file size of a given file.

Parameters

string $path

Return Value

int|false

at line 468
int|false lastModified(string $path)

Get the file's last modification time.

Parameters

string $path

Return Value

int|false

at line 478
bool isDirectory(string $directory)

Determine if the given path is a directory.

Parameters

string $directory

Return Value

bool

at line 486
bool isEmptyDirectory(string $directory, bool $ignoreDotFiles = false)

Determine if the given path is a directory that does not contain any other files or directories.

Parameters

string $directory
bool $ignoreDotFiles

Return Value

bool

at line 494
bool isReadable(string $path)

Determine if the given path is readable.

Parameters

string $path

Return Value

bool

at line 502
bool isWritable(string $path)

Determine if the given path is writable.

Parameters

string $path

Return Value

bool

at line 510
bool hasSameHash(string $firstFile, string $secondFile)

Determine if two files are the same by comparing their hashes.

Parameters

string $firstFile
string $secondFile

Return Value

bool

at line 520
bool isFile(string $file)

Determine if the given path is a file.

Parameters

string $file

Return Value

bool

at line 528
array|false glob(string $pattern, int $flags = 0)

Find path names matching a given pattern.

Parameters

string $pattern
int $flags

Return Value

array|false

at line 538
array files(array|string $directory, bool $hidden = false, array|string|int $depth = 0)

Get an array of all files in a directory.

Parameters

array|string $directory
bool $hidden
array|string|int $depth

Return Value

array

at line 551
array allFiles(array|string $directory, bool $hidden = false)

Get all of the files from the given directory (recursive).

Parameters

array|string $directory
bool $hidden

Return Value

array

at line 559
array directories(array|string $directory, array|string|int $depth = 0)

Get all of the directories within a given directory.

Parameters

array|string $directory
array|string|int $depth

Return Value

array

at line 573
array allDirectories(array|string $directory)

Get all the directories within a given directory (recursive).

Parameters

array|string $directory

Return Value

array

at line 581
void ensureDirectoryExists(string $path, int $mode = 0755, bool $recursive = true)

Ensure a directory exists.

Parameters

string $path
int $mode
bool $recursive

Return Value

void

at line 595
bool makeDirectory(string $path, int $mode = 0755, bool $recursive = false, bool $force = false)

Create a directory.

Parameters

string $path
int $mode
bool $recursive
bool $force

Return Value

bool

at line 607
bool moveDirectory(string $from, string $to, bool $overwrite = false)

Move a directory.

Parameters

string $from
string $to
bool $overwrite

Return Value

bool

at line 619
bool copyDirectory(string $directory, string $destination, int|null $options = null)

Copy a directory from one location to another.

Parameters

string $directory
string $destination
int|null $options

Return Value

bool

at line 664
bool deleteDirectory(string $directory, bool $preserve = false)

Recursively delete a directory.

The directory itself may be optionally preserved.

Parameters

string $directory
bool $preserve

Return Value

bool

at line 705
bool deleteDirectories(string $directory)

Remove all of the directories within a given directory.

Parameters

string $directory

Return Value

bool

at line 727
bool cleanDirectory(string $directory)

Empty the specified directory of all files and folders.

Parameters

string $directory

Return Value

bool

at line 735
void clearStatCache(string $path)

Clear file status cache.

Parameters

string $path

Return Value

void

at line 743
static void flushState()

Flush all static state.

Return Value

void