Skip to content
PHP Ecosystem
Explore

Filesystem

API reference › Scaffold › Filesystem

final classPHPForge\Scaffold\Filesystem
Sourcesrc/Filesystem.php

Runs native filesystem calls and turns the PHP warning of a failed call into a RuntimeException.

MethodDescriptionDefined by
copy() staticCopies a file, overwriting the destination.Filesystem
copyPermissions() staticCopies the permission bits of a file, masked by the current umask, onto another file.Filesystem
hash() staticReturns the hexadecimal SHA-256 hash of a file.Filesystem
isEmptyDirectory() staticReturns whether a directory holds no entry.Filesystem
mkdir() staticCreates a directory and its missing parents.Filesystem
read() staticReturns the content of a file.Filesystem
readLink() staticReturns the target of a symbolic link.Filesystem
remove() staticDeletes a file.Filesystem
removeDirectory() staticRemoves an empty directory.Filesystem
write() staticWrites content to a file, creating or truncating it.Filesystem
writeAtomic() staticWrites content to a temporary file next to $path and renames it over $path, removing the temporary file when the rename fails.Filesystem
public static function copy(string $source, string $destination, string $failure): void

Copies a file, overwriting the destination.

Parameters

ParameterTypeDescription
$sourcestringAbsolute path to the source file.
$destinationstringAbsolute path to the destination file.
$failurestringMessage of the exception thrown on failure, before the PHP warning.

Throws

public static function copyPermissions(string $source, string $destination, string $failure): void

Copies the permission bits of a file, masked by the current umask, onto another file.

Parameters

ParameterTypeDescription
$sourcestringAbsolute path to the file whose permissions are copied.
$destinationstringAbsolute path to the file that receives the permissions.
$failurestringMessage of the exception thrown on failure, before the PHP warning.

Throws

public static function hash(string $path, string $failure): string

Returns the hexadecimal SHA-256 hash of a file.

Parameters

ParameterTypeDescription
$pathstringAbsolute path to the file.
$failurestringMessage of the exception thrown on failure, before the PHP warning.

Returns

string: Hexadecimal SHA-256 hash of the file content.

Throws

public static function isEmptyDirectory(string $directory, string $failure): bool

Returns whether a directory holds no entry.

Parameters

ParameterTypeDescription
$directorystringAbsolute path to the directory.
$failurestringMessage of the exception thrown on failure, before the PHP warning.

Returns

bool: true when the directory has no entry besides . and .., false otherwise.

Throws

public static function mkdir(string $directory, string $failure): void

Creates a directory and its missing parents.

Parameters

ParameterTypeDescription
$directorystringAbsolute path to the directory.
$failurestringMessage of the exception thrown on failure, before the PHP warning.

Throws

public static function read(string $path, string $failure): string

Returns the content of a file.

Parameters

ParameterTypeDescription
$pathstringAbsolute path to the file.
$failurestringMessage of the exception thrown on failure, before the PHP warning.

Returns

string: Content of the file.

Throws

public static function readLink(string $path, string $failure): string

Returns the target of a symbolic link.

Parameters

ParameterTypeDescription
$pathstringAbsolute path to the symbolic link.
$failurestringMessage of the exception thrown on failure, before the PHP warning.

Returns

string: Target of the link, as stored in the link.

Throws

public static function remove(string $path, string $failure): void

Deletes a file.

Parameters

ParameterTypeDescription
$pathstringAbsolute path to the file.
$failurestringMessage of the exception thrown on failure, before the PHP warning.

Throws

public static function removeDirectory(string $directory, string $failure): void

Removes an empty directory.

Parameters

ParameterTypeDescription
$directorystringAbsolute path to the directory.
$failurestringMessage of the exception thrown on failure, before the PHP warning.

Throws

public static function write(string $path, string $content, string $failure): void

Writes content to a file, creating or truncating it.

Parameters

ParameterTypeDescription
$pathstringAbsolute path to the file.
$contentstringContent to write.
$failurestringMessage of the exception thrown on failure, before the PHP warning.

Throws

public static function writeAtomic(string $path, string $content, string $failure): void

Writes content to a temporary file next to $path and renames it over $path, removing the temporary file when the rename fails.

Parameters

ParameterTypeDescription
$pathstringAbsolute path to the file.
$contentstringContent to write.
$failurestringMessage of the exception thrown on failure, before the PHP warning.

Throws

  • RuntimeException: when the temporary file cannot be written, renamed or removed.