\Omega\Filesystem\Adapter\Local Local

Adapter for the local filesystem.

This class provides an interface to interact with the local filesystem. It allows reading, writing, deleting, and renaming files and directories within a specified base directory. It also supports stream creation, checksum calculation, size retrieval, and MIME type detection for files.

Summary

Methods
Properties
Constants
__construct
read
write
rename
exists
keys
mtime
delete
isDirectory
createStream
checksum
size
mimeType
computeKey
No public properties found
No public constants found
computePath
normalizePath
ensureDirectoryExists
createDirectory
directory
No protected constants found
deleteDirectory
create
mode
No private constants found

Properties

$directory

$directory : string

The base directory where the filesystem operations are performed.

Type

string — Holds the base directory where the fiesystem operation are performed,

$create

$create : bool

Type

bool

$mode

$mode : int

Type

int

Methods

__construct()

__construct(string  $directory, bool  $create = false, int  $mode = 0777) : void

Constructs a new Local filesystem adapter instance.

Parameters

string $directory

Directory where the filesystem is located.

bool $create

Whether to create the directory if it does not exist (default FALSE).

int $mode

Mode for mkdir.

Throws

\RuntimeException

if the specified directory does not exist and could not be created.

Returns

void —

read()

read(string  $key) : string|bool

Reads the content of the file.

Parameters

string $key

The key or path of the file to read.

Throws

\OutOfBoundsException

If the computed path is out of the directory

\InvalidArgumentException

if the directory already exists

\RuntimeException

if the directory could not be created

Returns

string|bool —

The content of the file or false if it cannot be read.

write()

write(string  $key, string  $content) : int|bool

Writes the given content into the file.

Parameters

string $key

The key or path of the file to write.

string $content

The content to write into the file.

Throws

\OutOfBoundsException

If the computed path is out of the directory

\InvalidArgumentException

if the directory already exists

\RuntimeException

if the directory could not be created

Returns

int|bool —

The number of bytes written or false on failure.

rename()

rename(string  $sourceKey, string  $targetKey) : bool

Renames a file.

Parameters

string $sourceKey

The current key or path of the file.

string $targetKey

The new key or path for the file.

Throws

\OutOfBoundsException

If the computed path is out of the directory

\InvalidArgumentException

if the directory already exists

\RuntimeException

if the directory could not be created

Returns

bool —

True on success, false on failure.

exists()

exists(string  $key) : bool

Indicates whether the file exists.

Parameters

string $key

The key or path of the file to check.

Returns

bool —

True if the file exists, false otherwise.

keys()

keys() : array

Returns an array of all keys (files and directories).

Throws

\OutOfBoundsException

If the computed path is out of the directory

\InvalidArgumentException

if the directory already exists

\RuntimeException

if the directory could not be created

Returns

array —

The list of keys.

mtime()

mtime(string  $key) : int|bool

Returns the last modified time.

Parameters

string $key

The key or path of the file.

Throws

\OutOfBoundsException

If the computed path is out of the directory

\InvalidArgumentException

if the directory already exists

\RuntimeException

if the directory could not be created

Returns

int|bool —

The last modified time as a UNIX timestamp, or false on failure.

delete()

delete(string  $key) : bool

Deletes the file.

Parameters

string $key

The key or path of the file to delete.

Returns

bool —

True on success, false on failure.

isDirectory()

isDirectory(string  $key) : bool

Check if the key represents a directory.

Parameters

string $key

The key or path to check.

Throws

\OutOfBoundsException

If the computed path is out of the directory

\InvalidArgumentException

if the directory already exists

\RuntimeException

if the directory could not be created

Returns

bool —

True if the key is a directory, false otherwise.

createStream()

createStream(mixed  $key) : \Omega\Filesystem\Stream\StreamInterface

Creates a new stream instance for the specified file.

Parameters

mixed $key

The key or path of the file for which the stream is created.

Throws

\OutOfBoundsException

If the computed path is out of the directory

\InvalidArgumentException

if the directory already exists

\RuntimeException

if the directory could not be created

Returns

\Omega\Filesystem\Stream\StreamInterface —

The stream instance associated with the specified file.

checksum()

checksum(string  $key) : string

Returns the checksum of the specified file.

Parameters

string $key

The identifier of the file for which to calculate the checksum.

Throws

\OutOfBoundsException

If the computed path is out of the directory

\InvalidArgumentException

if the directory already exists

\RuntimeException

if the directory could not be created

Returns

string —

The calculated checksum as a string.

size()

size(string  $key) : int|false

Retrieves the size of the specified file (key).

Parameters

string $key

The file key for which the size is being retrieved.

Throws

\OutOfBoundsException

If the computed path is out of the directory

\InvalidArgumentException

if the directory already exists

\RuntimeException

if the directory could not be created

Returns

int|false —

The size of the file in bytes, or false if it cannot be determined.

mimeType()

mimeType(string  $key) : string|false

Retrieves the MIME type of the specified file (key).

Parameters

string $key

The file key for which the MIME type is being retrieved.

Throws

\OutOfBoundsException

If the computed path is out of the directory

\InvalidArgumentException

if the directory already exists

\RuntimeException

if the directory could not be created

Returns

string|false —

The MIME type of the file, or false if it cannot be determined.

computeKey()

computeKey(string  $path) : string

Computes the key from the specified path.

Parameters

string $path

The path to compute the key from.

Returns

string —

The computed key.

computePath()

computePath(string  $key) : string

Computes the full path from the specified key.

Parameters

string $key

The key to compute the path from.

Throws

\OutOfBoundsException

If the computed path is out of the directory.

Returns

string —

The computed path.

normalizePath()

normalizePath(string  $path) : string

Normalizes the given path.

Parameters

string $path

Throws

\OutOfBoundsException

If the computed path is out of the directory

Returns

string —

ensureDirectoryExists()

ensureDirectoryExists(string  $directory, bool  $create = false) : void

Ensures that the specified directory exists.

Parameters

string $directory

The directory path to check.

bool $create

Whether to create the directory if it does not exist.

Throws

\RuntimeException

if the directory could not be created.

Returns

void —

createDirectory()

createDirectory(string  $directory) : void

Creates the specified directory and its parents.

Parameters

string $directory

Path of the directory to create

Throws

\InvalidArgumentException

if the directory already exists

\RuntimeException

if the directory could not be created

Returns

void —

deleteDirectory()

deleteDirectory(string  $directory) : bool

Deletes a directory and its contents recursively.

Parameters

string $directory

The directory to delete.

Returns

bool —

True if the deletion was successful, false otherwise.