final class FileSystem

File system tool.

Methods

static void
createDir(string $dir, int $mode = 0777)

Creates a directory if it does not exist, including parent directories.

static void
copy(string $origin, string $target, bool $overwrite = true)

Copies a file or an entire directory. Overwrites existing files and directories by default.

static resource
open(string $path, string $mode)

Opens file and returns resource.

static void
delete(string $path)

Deletes a file or an entire directory if exists. If the directory is not empty, it deletes its contents first.

static void
rename(string $origin, string $target, bool $overwrite = true)

Renames or moves a file or a directory. Overwrites existing files and directories by default.

static string
read(string $file)

Reads the content of a file.

static Generator
readLines(string $file, bool $stripNewLines = true)

Reads the file content line by line. Because it reads continuously as we iterate over the lines, it is possible to read files larger than the available memory.

static void
write(string $file, string $content, int|null $mode = 0666)

Writes the string to a file.

static void
makeWritable(string $path, int $dirMode = 0777, int $fileMode = 0666)

Sets file permissions to $fileMode or directory permissions to $dirMode.

static bool
isAbsolute(string $path)

Determines if the path is absolute.

static string
normalizePath(string $path)

Normalizes .. and . and directory separators in path.

static string
joinPaths(string ...$paths)

Joins all segments of the path and normalizes the result.

static string
resolvePath(string $basePath, string $path)

Resolves a path against a base path. If the path is absolute, returns it directly, if it's relative, joins it with the base path.

static string
unixSlashes(string $path)

Converts backslashes to slashes.

static string
platformSlashes(string $path)

Converts slashes to platform-specific directory separators.

Details

at line 26
static void createDir(string $dir, int $mode = 0777)

Creates a directory if it does not exist, including parent directories.

Parameters

string $dir
int $mode

Return Value

void

Exceptions

IOException

at line 44
static void copy(string $origin, string $target, bool $overwrite = true)

Copies a file or an entire directory. Overwrites existing files and directories by default.

Parameters

string $origin
string $target
bool $overwrite

Return Value

void

Exceptions

IOException
InvalidStateException

at line 84
static resource open(string $path, string $mode)

Opens file and returns resource.

Parameters

string $path
string $mode

Return Value

resource

Exceptions

IOException

at line 102
static void delete(string $path)

Deletes a file or an entire directory if exists. If the directory is not empty, it deletes its contents first.

Parameters

string $path

Return Value

void

Exceptions

IOException

at line 134
static void rename(string $origin, string $target, bool $overwrite = true)

Renames or moves a file or a directory. Overwrites existing files and directories by default.

Parameters

string $origin
string $target
bool $overwrite

Return Value

void

Exceptions

IOException
InvalidStateException

at line 164
static string read(string $file)

Reads the content of a file.

Parameters

string $file

Return Value

string

Exceptions

IOException

at line 185
static Generator readLines(string $file, bool $stripNewLines = true)

Reads the file content line by line. Because it reads continuously as we iterate over the lines, it is possible to read files larger than the available memory.

Parameters

string $file
bool $stripNewLines

Return Value

Generator

Exceptions

IOException

at line 214
static void write(string $file, string $content, int|null $mode = 0666)

Writes the string to a file.

Parameters

string $file
string $content
int|null $mode

Return Value

void

Exceptions

IOException

at line 241
static void makeWritable(string $path, int $dirMode = 0777, int $fileMode = 0666)

Sets file permissions to $fileMode or directory permissions to $dirMode.

Recursively traverses and sets permissions on the entire contents of the directory as well.

Parameters

string $path
int $dirMode
int $fileMode

Return Value

void

Exceptions

IOException

at line 274
static bool isAbsolute(string $path)

Determines if the path is absolute.

Parameters

string $path

Return Value

bool

at line 283
static string normalizePath(string $path)

Normalizes .. and . and directory separators in path.

Parameters

string $path

Return Value

string

at line 304
static string joinPaths(string ...$paths)

Joins all segments of the path and normalizes the result.

Parameters

string ...$paths

Return Value

string

at line 313
static string resolvePath(string $basePath, string $path)

Resolves a path against a base path. If the path is absolute, returns it directly, if it's relative, joins it with the base path.

Parameters

string $basePath
string $path

Return Value

string

at line 326
static string unixSlashes(string $path)

Converts backslashes to slashes.

Parameters

string $path

Return Value

string

at line 335
static string platformSlashes(string $path)

Converts slashes to platform-specific directory separators.

Parameters

string $path

Return Value

string