\Omega\Filesystem Filesystem

Class Filesystem.

The Filesystem class provides a comprehensive interface for managing files and directories within a storage system. It facilitates storing, retrieving, renaming, and deleting files, as well as performing various operations like checking for existence, calculating sizes, checksums, and MIME types. The class is built around an adapter pattern, allowing compatibility with different storage backends through the FilesystemAdapterInterface.

This class maintains a registry of File objects created during its operation, which allows for efficient access and manipulation of files without repeatedly creating new instances. It also provides utility methods to manage file existence and validate keys, ensuring robust error handling and improved maintainability of the filesystem operations.

Summary

Methods
Properties
Constants
__construct
getAdapter
has
rename
get
write
read
delete
keys
listKeys
mtime
checksum
size
createStream
createFile
mimeType
clearFileRegister
removeFromRegister
isDirectory
No public properties found
No public constants found
No protected methods found
fileRegister
adapter
No protected constants found
assertHasFile
isFileInRegister
assertValidKey
No private properties found
No private constants found

Properties

$fileRegister

$fileRegister : array

Type

array<string|int, mixed> — An associative array that stores `File` objects created with the `createFile()` method. The key is the file key, and the value is the corresponding `File` instance.

Methods

__construct()

__construct(\Omega\Filesystem\Adapter\FilesystemAdapterInterface  $adapter) : mixed

Parameters

\Omega\Filesystem\Adapter\FilesystemAdapterInterface $adapter

A configured Adapter instance that implements the required methods to interact with the storage backend.

Returns

mixed —

getAdapter()

getAdapter() : \Omega\Filesystem\Adapter\FilesystemAdapterInterface

Returns the adapter instance associated with the filesystem.

This method allows access to the underlying adapter, enabling direct interaction with the storage backend if needed.

Returns

\Omega\Filesystem\Adapter\FilesystemAdapterInterface —

The adapter used by the filesystem.

has()

has(string  $key) : bool

Check if a file exists in the filesystem.

Parameters

string $key

The key (path) of the file.

Returns

bool —

TRUE if the file exists, FALSE otherwise.

rename()

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

Rename a file in the filesystem.

Parameters

string $sourceKey

The current key (path) of the file.

string $targetKey

The new key (path) for the file.

Returns

bool —

TRUE if the rename was successful.

get()

get(string  $key, bool  $create = false) : \Omega\Filesystem\File

Retrieve a file from the filesystem.

Parameters

string $key

The key (path) of the file.

bool $create

Whether to create the file if it does not exist.

Returns

\Omega\Filesystem\File —

The requested file object.

write()

write(string  $key, string  $content, bool  $overwrite = false) : int

Write content to a file in the filesystem.

Parameters

string $key

The key (path) of the file.

string $content

The content to write to the file.

bool $overwrite

Whether to overwrite the file if it already exists.

Returns

int —

The number of bytes written to the file.

read()

read(string  $key) : string

Read content from a file in the filesystem.

Parameters

string $key

The key (path) of the file.

Returns

string —

The content of the file.

delete()

delete(string  $key) : bool

Delete a file from the filesystem.

Parameters

string $key

The key (path) of the file to delete.

Returns

bool —

TRUE on success, FALSE otherwise.

keys()

keys() : array

Retrieve an array of all file keys in the filesystem.

Returns

array —

An array of file keys.

listKeys()

listKeys(string  $prefix = '') : array

List file keys that begin with a specified prefix.

Parameters

string $prefix

The prefix to filter keys.

Returns

array —

An array of file keys matching the prefix.

mtime()

mtime(string  $key) : int

Retrieve the last modified time of a specified file.

Parameters

string $key

The key (path) of the file.

Returns

int —

The last modified time as a Unix timestamp.

checksum()

checksum(string  $key) : string

Calculate the checksum of a specified file's content.

Parameters

string $key

The key (path) of the file.

Returns

string —

An MD5 hash representing the file's content checksum.

size()

size(string  $key) : int

Retrieve the size of a specified file's content.

Parameters

string $key

The key (path) of the file.

Returns

int —

The size of the file in bytes.

createStream()

createStream(string  $key) : \Omega\Filesystem\Stream\StreamInterface|\Omega\Filesystem\Stream\InMemoryBuffer

Create a new stream instance for a specified file.

Parameters

string $key

The key (path) of the file.

Returns

\Omega\Filesystem\Stream\StreamInterface|\Omega\Filesystem\Stream\InMemoryBuffer —

A stream interface for the file.

createFile()

createFile(string  $key) : \Omega\Filesystem\File

Create a new file in the filesystem.

Parameters

string $key

The key (path) for the new file.

Returns

\Omega\Filesystem\File —

The newly created file object.

mimeType()

mimeType(string  $key) : string

Get the mime type of a specified file.

Parameters

string $key

The key (path) of the file.

Returns

string —

The MIME type of the file.

clearFileRegister()

clearFileRegister() : void

Clears the file register, removing all registered files.

Returns

void —

removeFromRegister()

removeFromRegister(string  $key) : void

Removes a file from the register.

Parameters

string $key

The key of the file to remove from the register.

Returns

void —

isDirectory()

isDirectory(string  $key) : bool

Check if the specified key represents a directory.

Parameters

string $key

The key (path) to check.

Returns

bool —

TRUE if the key represents a directory, FALSE otherwise.

assertHasFile()

assertHasFile(string  $key) : void

Checks if a file exists in the filesystem.

Throws a FileNotFoundException if the file does not exist.

Parameters

string $key

The key of the file to check.

Throws

\Omega\Filesystem\Exception\FileNotFoundException

if the file does not exist.

Returns

void —

isFileInRegister()

isFileInRegister(string  $key) : bool

Checks if a File object is registered.

Parameters

string $key

The key of the file.

Returns

bool —

True if the file is registered, false otherwise.

assertValidKey()

assertValidKey(string  $key) : void

Validates the given key to ensure it is not empty.

Parameters

string $key

The key to validate.

Throws

\InvalidArgumentException

if the key is empty.

Returns

void —