Skip to documentation

PhpFileConfig

Class
Final: Yes

Class PhpFileConfig.

Description

Provides a configuration source backed by a single PHP file returning an array. This class MUST load the configuration from the specified PHP file. It SHALL lazily initialize and retrieve configuration data upon invocation. If configured as persistent, mutations made via set and remove SHALL be persisted back to the file.

Table of Contents

Interfaces

ConfigInterface

Interface ConfigInterface.

Properties

 : string|null
 : array<string|int, mixed>|ConfigInterface|null
 : string
 : bool

Methods

__construct()

Constructs a PhpFileConfig instance.

 : mixed
__invoke()

Invokes the configuration and returns the configuration data.

 : ConfigInterface
get()

Retrieves a configuration value by key.

 : mixed
getIterator()

Retrieves an iterator for traversing the configuration data.

 : Traversable
has()

Checks for existence of a configuration key.

 : bool
remove()

Removes a configuration key and its associated value.

 : void
set()

Sets configuration data.

 : void
toArray()

Exports the entire configuration to an array.

 : array<string|int, mixed>
absolutePath()

Resolves relative paths against their containing directory without using include_path.

 : string
dump()

Publishes configuration atomically after revalidating the locked target.

 : void
 : array{path: string, suffix: string, linked: bool}
getConfig()

Retrieves or initializes the configuration instance.

 : ConfigInterface
isStreamPath()

Recognizes the registered protocols selected by PHP's native stream lookup.

 : bool
loadConfig()

Loads the configuration from the file or default configuration.

 : ConfigInterface
mutate()

Mutates the resolved target under an exclusive process lock.

 : void
 : array<string|int, int>|null
resolveTargetFile()

Anchors the input location on first use and resolves its current symlink target.

 : string
Properties

$absoluteFile

Private
private string|null $absoluteFile = null

$persistent

Private Read-only
private bool $persistent = false
Methods

__construct()

Public

Constructs a PhpFileConfig instance.

public __construct( string  $file[, bool  $persistent = false][, array<string, mixed>|ConfigInterface|null  $defaultConfig = null]) : mixed
Parameters
$file : string

Description

the path to the PHP configuration file

$persistent : bool = false

Description

whether changes should be saved back to the file

$defaultConfig : array<string, mixed>|ConfigInterface|null = null

Description

optional default configuration when the file does not exist

__invoke()

Public

Invokes the configuration and returns the configuration data.

public __invoke() : ConfigInterface

Description

If the file exists, it MUST return a valid array. If the file does not exist but a default configuration is provided, the default configuration SHALL be used.

Tags
throws

Description

if the file is unreadable or does not return an array

Return values

Description

a ConfigInterface implementation containing the configuration data

get()

Public

Retrieves a configuration value by key.

public get( string  $key[, mixed|null  $default = null]) : mixed
Parameters
$key : string

Description

the configuration key to retrieve

$default : mixed|null = null

Description

the default value if the key is not found

Return values

Description

the value of the configuration key or the default

getIterator()

Public

Retrieves an iterator for traversing the configuration data.

public getIterator() : Traversable
Return values
Traversable

Description

an iterator over the configuration

has()

Public

Checks for existence of a configuration key.

public has( string  $key) : bool
Parameters
$key : string

Description

the configuration key to check

Return values
bool

Description

TRUE if the key exists, FALSE otherwise

remove()

Public

Removes a configuration key and its associated value.

public remove( string  $key) : void

Description

This method MUST update the configuration data in the file if the persistent flag is set to true.

Parameters
$key : string

Description

the configuration key to remove

Tags
throws

Description

if the file cannot be written

set()

Public

Sets configuration data.

public set( array<string, mixed>|ConfigInterface|string  $key[, mixed  $value = null]) : void

Description

This method MUST update the configuration data in the file if the persistent flag is set to true.

Parameters
$key : array<string, mixed>|ConfigInterface|string

Description

the configuration key or an array of key-value pairs to set

$value : mixed = null

Description

the value to set for the specified key

Tags
throws

Description

if the key is invalid or file cannot be written

toArray()

Public

Exports the entire configuration to an array.

public toArray() : array<string|int, mixed>
Return values
array<string|int, mixed>

Description

the configuration as an associative array

absolutePath()

Private

Resolves relative paths against their containing directory without using include_path.

private absolutePath( string  $path[, string|null  $directory = null]) : string
Parameters
$path : string

Description

the file or symlink target path

$directory : string|null = null

Description

the directory containing a relative symlink target

Return values
string

anchorWindowsDriveRelativePath()

Private
private anchorWindowsDriveRelativePath( string  $path) : string
Parameters
$path : string
Return values
string

anchorWindowsRootedPath()

Private
private anchorWindowsRootedPath( string  $path, string|null  $directory) : string
Parameters
$path : string
$directory : string|null
Return values
string

applyTemporaryMetadata()

Private
private applyTemporaryMetadata( string  $tmpFile, array<string|int, int>|null  $existingMetadata) : void
Parameters
$tmpFile : string
$existingMetadata : array<string|int, int>|null

dump()

Private

Publishes configuration atomically after revalidating the locked target.

private dump( array<string|int, mixed>  $data, string  $targetFile) : void
Parameters
$data : array<string|int, mixed>
$targetFile : string

exportContent()

Private
private exportContent( array<string|int, mixed>  $data) : string
Parameters
$data : array<string|int, mixed>
Return values
string

findSymlinkComponent()

Private
private findSymlinkComponent( string  $file) : array{path: string, suffix: string, linked: bool}
Parameters
$file : string
Return values
array{path: string, suffix: string, linked: bool}
Private
private followSymlinks( string  $file) : string
Parameters
$file : string
Return values
string

isStreamPath()

Private

Recognizes the registered protocols selected by PHP's native stream lookup.

private isStreamPath( string  $path) : bool
Parameters
$path : string
Return values
bool

loadConfig()

Private

Loads the configuration from the file or default configuration.

private loadConfig([ string|null  $targetFile = null]) : ConfigInterface
Parameters
$targetFile : string|null = null

Description

resolved target shared with a persistent mutation

Tags
throws

Description

if the file is unreadable or does not return an array

Return values

mutateUnderLock()

Private
private mutateUnderLock( resource  $lockFp, string  $targetFile, callable(ConfigInterface): void  $operation) : void
Parameters
$lockFp : resource
$targetFile : string
$operation : callable(ConfigInterface): void

prepareWritableTarget()

Private
private prepareWritableTarget( string  $targetFile) : string
Parameters
$targetFile : string
Return values
string

readExistingMetadata()

Private
private readExistingMetadata( string  $targetFile) : array<string|int, int>|null
Parameters
$targetFile : string
Return values
array<string|int, int>|null

resolveTargetFile()

Private

Anchors the input location on first use and resolves its current symlink target.

private resolveTargetFile() : string
Return values
string
Search results