API Reference

ConfigRepository

WPZylos\Framework\Config\ConfigRepository

Configuration repository with dot-notation access and typed accessors.


__construct(array $items = [])

Create a new configuration repository.

ParameterTypeDefaultDescription
$itemsarray[]Initial configuration items
$config = new ConfigRepository(['app' => ['name' => 'My Plugin']]);

get(string $key, mixed $default = null): mixed

Get a configuration value using dot notation.

ParameterTypeDefaultDescription
$keystring-Configuration key (e.g. 'app.debug')
$defaultmixednullDefault value if key not found

Returns: mixed - The configuration value or default.

$config->get('app.name');           // 'My Plugin'
$config->get('missing', 'default'); // 'default'

set(string $key, mixed $value): void

Set a configuration value using dot notation.

ParameterTypeDescription
$keystringConfiguration key
$valuemixedValue to set
$config->set('app.version', '2.0.0');

has(string $key): bool

Check if a configuration key exists.

ParameterTypeDescription
$keystringConfiguration key

Returns: bool

$config->has('app.name'); // true

all(): array

Get all configuration items.

Returns: array - All configuration items.

$items = $config->all();

string(string $key, string $default = ''): string

Get a value cast to string.

ParameterTypeDefaultDescription
$keystring-Configuration key
$defaultstring''Default value

Returns: string

$config->string('app.name');            // 'My Plugin'
$config->string('missing', 'fallback'); // 'fallback'

int(string $key, int $default = 0): int

Get a value cast to integer.

ParameterTypeDefaultDescription
$keystring-Configuration key
$defaultint0Default value

Returns: int

$config->int('database.port', 3306); // 3306

float(string $key, float $default = 0.0): float

Get a value cast to float.

ParameterTypeDefaultDescription
$keystring-Configuration key
$defaultfloat0.0Default value

Returns: float

$config->float('rate.tax', 0.15); // 0.15

bool(string $key, bool $default = false): bool

Get a value cast to boolean. Recognizes string values 'true', '1', 'yes', 'on' as true.

ParameterTypeDefaultDescription
$keystring-Configuration key
$defaultboolfalseDefault value

Returns: bool

$config->bool('app.debug');       // true
$config->bool('missing', false);  // false

array(string $key, array $default = []): array

Get a value as an array. Returns the default if the stored value is not an array.

ParameterTypeDefaultDescription
$keystring-Configuration key
$defaultarray[]Default value

Returns: array

$config->array('app.providers', []); // ['Provider1', 'Provider2']

loadDirectory(string $path): void

Load all PHP configuration files from a directory. Each file should return an array. The filename (without .php extension) becomes the top-level configuration key.

ParameterTypeDescription
$pathstringDirectory path
$config->loadDirectory(__DIR__ . '/config');
// config/app.php -> $config->get('app.*')

Silently returns if the directory does not exist.


merge(array $items): void

Deep-merge items into the repository using array_replace_recursive.

ParameterTypeDescription
$itemsarrayItems to merge
$config->merge(['app' => ['version' => '2.0']]);

EnvLoader

WPZylos\Framework\Config\EnvLoader

Parses .env files and optionally sets environment variables. Handles comments, quoted values, escape sequences, and special values.


__construct(bool $setEnv = true, bool $setPutenv = false)

Create a new environment loader.

ParameterTypeDefaultDescription
$setEnvbooltrueSet values in $_ENV
$setPutenvboolfalseSet values via putenv()
$env = new EnvLoader(setEnv: true, setPutenv: false);

load(string $path): bool

Load and parse a .env file. Does not throw if the file is missing.

ParameterTypeDescription
$pathstringPath to .env file

Returns: bool - true if loaded successfully, false if missing or unreadable.

$env->load(__DIR__ . '/.env');

get(string $key, ?string $default = null): ?string

Get a parsed environment value.

ParameterTypeDefaultDescription
$keystring-Variable name
$default?stringnullDefault value

Returns: ?string

$env->get('APP_DEBUG');            // 'true'
$env->get('MISSING', 'fallback'); // 'fallback'

has(string $key): bool

Check if a variable was parsed from the .env file.

ParameterTypeDescription
$keystringVariable name

Returns: bool

$env->has('APP_DEBUG'); // true

all(): array

Get all parsed environment values.

Returns: array

$values = $env->all();

static env(string $key, mixed $default = null): mixed

Static helper to get an environment value from any source. Checks $_ENV first, then getenv().

ParameterTypeDefaultDescription
$keystring-Variable name
$defaultmixednullDefault value

Returns: mixed

EnvLoader::env('APP_DEBUG', false);

ConfigServiceProvider

WPZylos\Framework\Config\ConfigServiceProvider extends WPZylos\Framework\Core\ServiceProvider

Registers the configuration system in the application container.


register(ApplicationInterface $app): void

Registers the config repository as a singleton. During registration:

  1. Creates a new ConfigRepository instance
  2. Loads .env from the plugin root via EnvLoader
  3. Loads config files from the @config path via loadDirectory()
  4. Binds a 'config' alias to the ConfigRepository singleton
ParameterTypeDescription
$appApplicationInterfaceApplication instance
// After registration:
$config = $app->make(ConfigRepository::class);
$config = $app->make('config');