Skip to content

ComposerFile

Helper for working with Drupal's Composer file.


Methods

__construct

File constructor.

public __construct(?string $filename = null, \Drupal\Core\Cache\CacheBackendInterface|\Drupal\Component\FileCache\FileCacheInterface|bool $cache = true): mixed

Parameters:

Parameter Type Description
$filename ?string
The filename/URI to the file.
$cache \Drupal\Core\Cache\CacheBackendInterface|\Drupal\Component\FileCache\FileCacheInterface|bool
An optional cache backend or FALSE to disable file contents caching.

exec

Runs a composer command.

public exec(string $command, mixed& $result_code = null): void

Note: USE THIS AT YOUR OWN RISK. YOU ARE RESPONSIBLE FOR ESCAPING THE INPUT TO THE $command PARAMETER.

Parameters:

Parameter Type Description
$command string
The composer command to run, without the "composer" part.
$result_code mixed
The result code from running the command, modified by reference.

Throws:

  • RuntimeException

Inherited methods

__construct

File constructor.

public __construct(string $filename, \Drupal\Core\Cache\CacheBackendInterface|bool $cache = true): mixed

Parameters:

Parameter Type Description
$filename string
The filename/URI to the file.
$cache \Drupal\Core\Cache\CacheBackendInterface|bool
An optional cache backend or FALSE to disable file contents caching.

getCache

Get the file cache data.

protected getCache(): mixed

Return Value:

The file data or NULL if no cached data was available.


setCache

Set the file cache data.

protected setCache(mixed $data): void

Parameters:

Parameter Type Description
$data mixed
The file data.

invalidateCache

Invalidate the file cache data.

protected invalidateCache(): void

readData

Gets contents of a file.

public readData(): mixed

Return Value:

The decoded file contents.

Throws:

  • IOException

decodeData

Decode a file's contents into data.

public decodeData(string $data): mixed

Parameters:

Parameter Type Description
$data string
The contents of a file.

Return Value:

The decoded file data.

Throws:

  • JsonException

encodeData

Encode data for file content.

public encodeData(mixed $data): string

Parameters:

Parameter Type Description
$data mixed
The data to save to a file.

Return Value:

The encoded data, ready to be saved to a file.

Throws:

  • JsonException

writeData

Writes contents to a file.

public writeData(mixed $data): void

Parameters:

Parameter Type Description
$data mixed
The contents to write to a file. If not a string, this will JSON encode
the data before writing to the file.

Throws:

  • IOException

validateWritable

Validates that the directory and file are both writable.

public validateWritable(): void

Throws:

  • IOException

validateReadable

Validates that the directory is and file are readable.

public validateReadable(): void

Throws:

  • IOException