class FileStorage implements PhpStorageInterface (View source)

Stores the code as regular PHP files.

Properties

protected string $directory

The directory where the files should be stored.

Methods

__construct(array $configuration)

Constructs this FileStorage object.

bool
exists(string $name)

Checks whether the PHP code exists in storage.

load(string $name)

Loads PHP code from storage.

bool
save(string $name, string $code)

Saves PHP code to storage.

static string
htaccessLines(bool $private = TRUE) deprecated

Returns the standard .htaccess lines that Drupal writes to file directories.

ensureDirectory(string $directory, int $mode = 0777)

Ensures the directory exists, has the right permissions, and a .htaccess.

bool
createDirectory(string $directory, int $mode = 0777)

Ensures the requested directory exists and has the right permissions.

bool
delete(string $name)

Deletes PHP code from storage.

string|false
getFullPath(string $name)

Gets the full file path.

bool
writeable()

Whether this is a writable storage.

deleteAll()

Removes all files in this bin.

bool
unlink(string $path)

Deletes files and/or directories in the specified path.

array
listAll()

Lists all the files in the storage.

garbageCollection()

Performs garbage collection on the storage.

Details

__construct(array $configuration)

Constructs this FileStorage object.

Parameters

array $configuration

An associative array, containing at least these two keys:

  • directory: The directory where the files should be stored.
  • bin: The storage bin. Multiple storage objects can be instantiated with the same configuration, but for different bins..

bool exists(string $name)

Checks whether the PHP code exists in storage.

Parameters

string $name

The virtual file name. Can be a relative path.

Return Value

bool

TRUE if the virtual file exists, FALSE otherwise.

load(string $name)

Loads PHP code from storage.

Depending on storage implementation, exists() checks can be expensive, so this function may be called for a file that doesn't exist, and that should not result in errors. This function does not return anything, so it is up to the caller to determine if any code was loaded (for example, check class_exists() or function_exists() for what was expected in the code).

Parameters

string $name

The virtual file name. Can be a relative path.

bool save(string $name, string $code)

Saves PHP code to storage.

Parameters

string $name

The virtual file name. Can be a relative path.

string $code

The PHP code to be saved.

Return Value

bool

TRUE if the save succeeded, FALSE if it failed.

static string htaccessLines(bool $private = TRUE) deprecated

deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Instead use \Drupal\Component\FileSecurity\FileSecurity.

Returns the standard .htaccess lines that Drupal writes to file directories.

Parameters

bool $private

(optional) Set to FALSE to return the .htaccess lines for an open and public directory. The default is TRUE, which returns the .htaccess lines for a private and protected directory.

Return Value

string

The desired contents of the .htaccess file.

See also

https://www.drupal.org/node/3075098
file_save_htaccess()

protected ensureDirectory(string $directory, int $mode = 0777)

Ensures the directory exists, has the right permissions, and a .htaccess.

For compatibility with open_basedir, the requested directory is created using a recursion logic that is based on the relative directory path/tree: It works from the end of the path recursively back towards the root directory, until an existing parent directory is found. From there, the subdirectories are created.

Parameters

string $directory

The directory path.

int $mode

The mode, permissions, the directory should have.

protected bool createDirectory(string $directory, int $mode = 0777)

Ensures the requested directory exists and has the right permissions.

For compatibility with open_basedir, the requested directory is created using a recursion logic that is based on the relative directory path/tree: It works from the end of the path recursively back towards the root directory, until an existing parent directory is found. From there, the subdirectories are created.

Parameters

string $directory

The directory path.

int $mode

The mode, permissions, the directory should have.

Return Value

bool

TRUE if the directory exists or has been created, FALSE otherwise.

bool delete(string $name)

Deletes PHP code from storage.

Parameters

string $name

The virtual file name. Can be a relative path.

Return Value

bool

TRUE if the delete succeeded, FALSE if it failed.

string|false getFullPath(string $name)

Gets the full file path.

Parameters

string $name

The virtual file name. Can be a relative path.

Return Value

string|false

The full file path for the provided name. Return FALSE if the implementation needs to prevent access to the file.

bool writeable()

Whether this is a writable storage.

Return Value

bool

deleteAll()

Removes all files in this bin.

Deletes files and/or directories in the specified path.

If the specified path is a directory the method will call itself recursively to process the contents. Once the contents have been removed the directory will also be removed.

Parameters

string $path

A string containing either a file or directory path.

Return Value

bool

TRUE for success or if path does not exist, FALSE in the event of an error.

array listAll()

Lists all the files in the storage.

Return Value

array

Array of filenames.

garbageCollection()

Performs garbage collection on the storage.

The storage may choose to delete expired or invalidated items.