\Omega\Filesystem File

Class File.

This class represents a file within a filesystem. It provides methods to manage the file's content, metadata, and existence in the filesystem. The file content is lazily loaded, meaning it will not be retrieved from the filesystem until it is specifically requested. This optimizes performance by avoiding unnecessary file reads.

Summary

Methods
Properties
Constants
__construct
getKey
getContent
getName
getSize
getMtime
setSize
setContent
setName
exists
delete
createStream
rename
No public properties found
No public constants found
setMetadata
content
metadata
name
size
mtime
key
filesystem
No protected constants found
supportsMetadata
No private properties found
No private constants found

Properties

$content

$content : mixed|null

The content of the file. It is lazy-loaded and will be retrieved from the filesystem on first request.

Type

mixed|null —

$metadata

$metadata : array|null

Metadata associated with the file, stored as an associative array.

This is only applicable for adapters that support metadata.

Type

array<string|int, mixed>|null —

$name

$name : string|null

The human-readable name of the file, usually derived from the end of the key.

Type

string|null —

$size

$size : int

The size of the file in bytes.

Type

int —

$mtime

$mtime : int|null

The last modified time of the file as a Unix timestamp.

Type

int|null —

$key

$key : string

Type

string

Methods

__construct()

__construct(string  $key, \Omega\Filesystem\FilesystemInterface  $filesystem) : mixed

Constructor to initialize the File object.

Parameters

string $key

The key (path) of the file in the filesystem.

\Omega\Filesystem\FilesystemInterface $filesystem

The filesystem interface to interact with the filesystem.

Returns

mixed —

getKey()

getKey() : string

Get the key (path) of the file.

Returns

string —

The key of the file.

getContent()

getContent(array  $metadata = []) : string

Retrieve the content of the file. The content is loaded lazily on the first call.

Parameters

array $metadata

Optional metadata to be set when reading.

Throws

\Omega\Filesystem\Exception\FileNotFoundException

If the file cannot be found in the filesystem.

Returns

string —

The content of the file.

getName()

getName() : string

Get the name of the file.

Returns

string —

The name of the file.

getSize()

getSize() : int

Get the size of the file in bytes.

Returns

int —

The size of the file.

getMtime()

getMtime() : int

Get the last modified time of the file.

Returns

int —

The last modified time as a Unix timestamp.

setSize()

setSize(int  $size) : void

Set the size of the file.

Parameters

int $size

The size of the file in bytes.

Returns

void —

setContent()

setContent(string  $content, array  $metadata = []) : int

Set the content of the file. This will also update the size and metadata.

Parameters

string $content

The content to be written to the file.

array $metadata

Optional metadata to be sent when writing.

Returns

int —

The number of bytes written to the file, or FALSE on failure.

setName()

setName(string  $name) : void

Set the name of the file.

Parameters

string $name

The new name of the file.

Returns

void —

exists()

exists() : bool

Check if the file exists in the filesystem.

Returns

bool —

TRUE if the file exists, FALSE otherwise.

delete()

delete(array  $metadata = []) : bool

Delete the file from the filesystem.

Parameters

array $metadata

Optional metadata to be sent when deleting.

Throws

\Omega\Filesystem\Exception\FileNotFoundException

If the file cannot be found.

\RuntimeException

If the file cannot be deleted.

Returns

bool —

TRUE on success, FALSE on failure.

createStream()

createStream() : \Omega\Filesystem\Stream\StreamInterface

Create a new file stream instance.

Returns

\Omega\Filesystem\Stream\StreamInterface —

A stream interface for the file.

rename()

rename(string  $newKey) : void

Rename the file and move it to a new location.

Parameters

string $newKey

The new key (path) for the file.

Returns

void —

setMetadata()

setMetadata(array  $metadata) : bool

Set the metadata for the file if the filesystem adapter supports it.

Parameters

array $metadata

The metadata to be set.

Returns

bool —

TRUE if metadata was set, FALSE otherwise.

supportsMetadata()

supportsMetadata() : bool

Check if the filesystem adapter supports metadata.

Returns

bool —

TRUE if metadata is supported, FALSE otherwise.