class FileCache implements FileCache

The file cache.

Properties

protected array $config

File cache configuration.

protected Filesystem $files

The Filesytem instance to use

protected FilesystemManager $storage

File FilesystemManager instance to use

Methods

__construct(array $config = [], Filesystem $files = null, FilesystemManager $storage = null)

Create an instance.

bool
exists(File $file)

Check if a file exists.

mixed
get(File $file, callable $callback)

Perform a callback with the path of a cached file. This takes care of shared locks on the cached file file so it is not corrupted due to concurrent write operations.

mixed
getOnce(File $file, callable $callback)

Like get but deletes the cached file afterwards (if it is not used somewhere else).

resource
getStream(File $file)

Get a stream resource for an file. If the file is cached, the resource points to the cached file instead. This will not cache uncached files. Make sure to close the streams!

mixed
batch(array $files, callable $callback)

Perform a callback with the paths of many cached files. Use this to prevent pruning of the files while they are processed.

mixed
batchOnce(array $files, callable $callback)

Like batch but deletes the cached files afterwards (if they are not used somewhere else).

prune()

Remove cached files that are too old or exceed the maximum cache size.

clear()

Delete all unused cached files.

bool
existsRemote(File $file)

Check for existence of a remte file.

bool
existsDisk(File $file)

Check for existence of a file from a storage disk.

bool
delete(SplFileInfo $file)

Delete a cached file it it is not used.

array
retrieve(File $file)

Cache a remote or cloud storage file if it is not cached and get the path to the cached file. If the file is local, nothing will be done and the path to the local file will be returned.

array
retrieveExistingFile(string $cachedPath, resource $handle)

Get path and handle for a file that exists in the cache.

array
retrieveNewFile(File $file, string $cachedPath, resource $handle)

Get path and handle for a file that does not yet exist in the cache.

string
getRemoteFile(File $file, resource $target)

Cache a remote file and get the path to the cached file.

string
getDiskFile(File $file, resource $target)

Cache an file from a storage disk and get the path to the cached file. Files from local disks are not cached.

string
cacheFromResource(File $file, resource $source, resource $target)

Store the file from the given resource to a cached file.

ensurePathExists()

Creates the cache directory if it doesn't exist yet.

string
getCachedPath(File $file)

Get the path to the cached file file.

resource
getFileStream(string $url, resource|null $context = null)

Get the stream resource for an file.

bool
isRemote(File $file)

Determine if an file is remote, i.e. served by a public webserver.

Filesystem
getDisk(File $file)

Get the storage disk on which a file is stored.

string
encodeUrl(string $url)

Escape special characters (e.g. spaces) that may occur in parts of a HTTP URL.

Details

at line 47
__construct(array $config = [], Filesystem $files = null, FilesystemManager $storage = null)

Create an instance.

Parameters

array $config

Optional custom configuration.

Filesystem $files
FilesystemManager $storage

at line 57
bool exists(File $file)

Check if a file exists.

Parameters

File $file

Return Value

bool

Whether the file exists or not.

at line 69
mixed get(File $file, callable $callback)

Perform a callback with the path of a cached file. This takes care of shared locks on the cached file file so it is not corrupted due to concurrent write operations.

Parameters

File $file
callable $callback

Gets the file object and the path to the cached file file as arguments.

Return Value

mixed

Result of the callback.

at line 79
mixed getOnce(File $file, callable $callback)

Like get but deletes the cached file afterwards (if it is not used somewhere else).

Parameters

File $file
callable $callback

Gets the file object and the path to the cached file file as arguments.

Return Value

mixed

Result of the callback.

at line 89
resource getStream(File $file)

Get a stream resource for an file. If the file is cached, the resource points to the cached file instead. This will not cache uncached files. Make sure to close the streams!

Parameters

File $file

Return Value

resource

Exceptions

Exception

at line 121
mixed batch(array $files, callable $callback)

Perform a callback with the paths of many cached files. Use this to prevent pruning of the files while they are processed.

Parameters

array $files
callable $callback

Gets the array of file objects and the array of paths to the cached file files (in the same ordering) as arguments.

Return Value

mixed

Result of the callback.

at line 145
mixed batchOnce(array $files, callable $callback)

Like batch but deletes the cached files afterwards (if they are not used somewhere else).

Parameters

array $files
callable $callback

Gets the array of file objects and the array of paths to the cached file files (in the same ordering) as arguments.

Return Value

mixed

Result of the callback.

at line 179
prune()

Remove cached files that are too old or exceed the maximum cache size.

at line 230
clear()

Delete all unused cached files.

at line 254
protected bool existsRemote(File $file)

Check for existence of a remte file.

Parameters

File $file

Return Value

bool

at line 291
protected bool existsDisk(File $file)

Check for existence of a file from a storage disk.

Parameters

File $file

Return Value

bool

at line 326
protected bool delete(SplFileInfo $file)

Delete a cached file it it is not used.

Parameters

SplFileInfo $file

Return Value

bool

If the file has been deleted.

at line 355
protected array retrieve(File $file)

Cache a remote or cloud storage file if it is not cached and get the path to the cached file. If the file is local, nothing will be done and the path to the local file will be returned.

Parameters

File $file

File to get the path for

Return Value

array

Containing the 'path' to the file and the file 'handle'. Close the handle when finished.

Exceptions

Exception

at line 408
protected array retrieveExistingFile(string $cachedPath, resource $handle)

Get path and handle for a file that exists in the cache.

Parameters

string $cachedPath
resource $handle

Return Value

array

at line 429
protected array retrieveNewFile(File $file, string $cachedPath, resource $handle)

Get path and handle for a file that does not yet exist in the cache.

Parameters

File $file
string $cachedPath
resource $handle

Return Value

array

at line 467
protected string getRemoteFile(File $file, resource $target)

Cache a remote file and get the path to the cached file.

Parameters

File $file

Remote file

resource $target

Target file resource

Return Value

string

Exceptions

Exception

at line 491
protected string getDiskFile(File $file, resource $target)

Cache an file from a storage disk and get the path to the cached file. Files from local disks are not cached.

Parameters

File $file

Cloud storage file

resource $target

Target file resource

Return Value

string

Exceptions

Exception

at line 519
protected string cacheFromResource(File $file, resource $source, resource $target)

Store the file from the given resource to a cached file.

Parameters

File $file
resource $source
resource $target

Return Value

string

Path to the cached file

Exceptions

Exception

at line 549
protected ensurePathExists()

Creates the cache directory if it doesn't exist yet.

at line 563
protected string getCachedPath(File $file)

Get the path to the cached file file.

Parameters

File $file

Return Value

string

at line 578
protected resource getFileStream(string $url, resource|null $context = null)

Get the stream resource for an file.

Parameters

string $url
resource|null $context

Stream context

Return Value

resource

at line 601
protected bool isRemote(File $file)

Determine if an file is remote, i.e. served by a public webserver.

Parameters

File $file

Return Value

bool

at line 613
protected Filesystem getDisk(File $file)

Get the storage disk on which a file is stored.

Parameters

File $file

Return Value

Filesystem

at line 630
protected string encodeUrl(string $url)

Escape special characters (e.g. spaces) that may occur in parts of a HTTP URL.

We do not use urlencode or rawurlencode because they encode some characters (e.g. "+") that should not be changed in the URL.

Parameters

string $url

Return Value

string