Skip to documentation

DirectoryConfig

Class
implements ConfigInterface
uses GlobTrait LazyLoadConfigTrait

Class DirectoryConfig.

Description

Loads and aggregates configuration from a specified directory containing PHP files. This class MUST validate the target directory and match file names using Laminas glob semantics. It MAY cache the aggregated result if a cached config file path is provided.

Tags
@extends

Description

LaminasConfigAggregatorConfig

Table of Contents

Interfaces

ConfigInterface

Interface ConfigInterface.

Constants

 = '{,*}.php'
 = false

Properties

Methods

__construct()

Constructs a DirectoryConfig instance.

 : mixed
__invoke()

Implementing class MUST define the __invoke() method to return a ConfigInterface instance.

 : 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.

 : void
set()

Sets configuration data.

 : void
toArray()

Exports the entire configuration to an array.

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

Selects visible child directories when recursive discovery is enabled.

 : array<int, string>
discoverDirectories()

Yields directory paths and their canonical identities in depth-then-path order.

 : Generator<string, string>
discoverFiles()

Discovers files in depth-then-path order without following directory symlinks.

 : array<int, string>
escapeDirectory()

Escapes only the directory, leaving the library's filename pattern intact.

 : string
getConfig()

Retrieves or initializes the configuration instance.

 : ConfigInterface
isWithinDirectory()

Checks a candidate's canonical parent against the selected literal directory.

 : bool
matchFiles()

Matches the configured pattern inside a literal directory and resolves each file.

 : Generator<int, string>
openDirectory()

Skips children that become inaccessible between discovery and opening.

 : DirectoryIterator|null
provideConfigs()

Includes discovered PHP files only when the aggregator resolves this provider.

 : Generator
Constants

Constants

PATTERN

Protected
protected string PATTERN = '{,*}.php'

Description

file matching pattern for PHP configuration files

RECURSIVE

Protected
protected bool RECURSIVE = false

Description

whether child directories should be discovered

Properties

$cachedConfigFile

Private Read-only
private string|null $cachedConfigFile = null
Methods

__construct()

Public

Constructs a DirectoryConfig instance.

public __construct( string  $directory[, string|null  $cachedConfigFile = null]) : mixed

Description

This constructor SHALL validate the specified directory, and initialize the Laminas config aggregator with a lazy file provider using the defined pattern. An existing cache file SHALL be reused when provided. Generating a cache file additionally requires a truthy config_cache_enabled entry.

Parameters
$directory : string

Description

the directory path from which to load configuration files

$cachedConfigFile : string|null = null

Description

optional path to a cache file for the aggregated configuration

Tags
throws

Description

if the directory is not valid or not readable

__invoke()

Public Abstract

Implementing class MUST define the __invoke() method to return a ConfigInterface instance.

public abstract __invoke() : ConfigInterface
Return values

Description

the actual configuration instance

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.

public remove( string  $key) : void
Parameters
$key : string

Description

the configuration key to remove

set()

Public

Sets configuration data.

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

Description

the key or set of keys/values to set

$value : mixed|null = null

Description

the value to set if a single key is provided

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

discoverChildDirectories()

Private

Selects visible child directories when recursive discovery is enabled.

private discoverChildDirectories( string  $directory) : array<int, string>
Parameters
$directory : string

Description

the parent directory path

Return values
array<int, string>

discoverDirectories()

Private

Yields directory paths and their canonical identities in depth-then-path order.

private discoverDirectories() : Generator<string, string>
Return values
Generator<string, string>

discoverFiles()

Private

Discovers files in depth-then-path order without following directory symlinks.

private discoverFiles() : array<int, string>
Return values
array<int, string>

escapeDirectory()

Private

Escapes only the directory, leaving the library's filename pattern intact.

private escapeDirectory( string  $directory) : string
Parameters
$directory : string

Description

the literal directory path

Return values
string

isWithinDirectory()

Private

Checks a candidate's canonical parent against the selected literal directory.

private isWithinDirectory( string  $file, string  $directory) : bool
Parameters
$file : string

Description

the candidate path

$directory : string

Description

the canonical selected directory

Return values
bool

matchFiles()

Private

Matches the configured pattern inside a literal directory and resolves each file.

private matchFiles( string  $directory, string  $realDirectory) : Generator<int, string>
Parameters
$directory : string

Description

the discovered directory path

$realDirectory : string

Description

its canonical identity

Return values
Generator<int, string>

openDirectory()

Private

Skips children that become inaccessible between discovery and opening.

private openDirectory( string  $directory) : DirectoryIterator|null
Parameters
$directory : string

Description

the discovered directory path

Tags
throws
UnexpectedValueException

Description

for root failures or unrelated directory-opening errors

Return values
DirectoryIterator|null

provideConfigs()

Private

Includes discovered PHP files only when the aggregator resolves this provider.

private provideConfigs() : Generator
Return values
Generator
Search results