Primordyx Framework Documentation

Config
in package

Config - Application Configuration Management

A static facade for managing application configuration via INI files. Provides single-load performance by reading the INI file once and caching in memory for the application lifecycle.

Usage:

  Config::initialize('/path/to/config.ini');         // Uses 'default' section by default
  Config::initialize('/path/to/config.ini', 'prod'); // Uses 'prod' section by default
  $dbHost = Config::get('host', 'database');         // [database] section
  $apiKey = Config::get('api_key');                  // Uses default section
  $enabled = Config::getBool('enabled', 'features'); // Type-safe boolean
  $port = Config::getInt('port', 'database', 3306);  // Type-safe integer

Table of Contents

Properties

$config  : Ini|null
The Ini configuration object

Methods

all()  : array<string|int, mixed>
Get all configuration data (alias for dump).
dump()  : array<string|int, mixed>
Get all configuration sections and their values.
get()  : mixed
Get a configuration value.
getArray()  : array<int, string>
Get a configuration value as an array (comma-separated).
getBool()  : bool
Get a configuration value as a boolean.
getEnum()  : string
Get a configuration value that must match one of the allowed options.
getFloat()  : float
Get a configuration value as a float.
getIni()  : Ini
Get the underlying Ini configuration object.
getInt()  : int
Get a configuration value as an integer.
getJsonArray()  : array<string|int, mixed>
Get a configuration value as a JSON-decoded array.
getSection()  : array<string|int, mixed>
Get all key-value pairs from a section.
has()  : bool
Check if a configuration key exists.
initialize()  : void
Initialize the application configuration.
isInitialized()  : bool
Check if the configuration has been initialized.
reload()  : void
Re-initialize with new settings.
reset()  : void
Reset the configuration (useful for testing).
ensureInitialized()  : void
Ensure configuration is initialized, throw exception if not.

Properties

$config

The Ini configuration object

private static Ini|null $config = null

Methods

all()

Get all configuration data (alias for dump).

public static all() : array<string|int, mixed>
Tags
throws
RuntimeException

if configuration is not initialized

Return values
array<string|int, mixed>

All configuration data

dump()

Get all configuration sections and their values.

public static dump() : array<string|int, mixed>
Tags
throws
RuntimeException

if configuration is not initialized

Return values
array<string|int, mixed>

All configuration data

get()

Get a configuration value.

public static get(string $key[, string $section = '' ][, mixed $default = null ]) : mixed

When no section is specified, uses the default section that was set during initialize().

Examples:

  Config::initialize('/path/config.ini');           // Uses 'default' as default section
  Config::get('api_key')              // Looks in default section (usually [default])
  Config::get('host', 'database')     // Looks in [database] section
  Config::get('debug', 'app', false)  // Looks in [app] section with fallback
Parameters
$key : string

The configuration key to retrieve

$section : string = ''

Section name (uses default section if empty)

$default : mixed = null

Default value if key not found

Tags
throws
RuntimeException

if configuration is not initialized

Return values
mixed

The configuration value

getArray()

Get a configuration value as an array (comma-separated).

public static getArray(string $key[, string $section = '' ][, string $separator = ',' ]) : array<int, string>
Parameters
$key : string

The configuration key to retrieve

$section : string = ''

Section name (uses default section if empty)

$separator : string = ','

Delimiter to split on (default: comma)

Tags
throws
RuntimeException

if configuration is not initialized

Return values
array<int, string>

The configuration value as array

getBool()

Get a configuration value as a boolean.

public static getBool(string $key[, string $section = '' ][, bool $default = false ]) : bool

Supports native booleans or common truthy/falsy strings like "true", "false", "on", "off", etc.

Parameters
$key : string

The configuration key to retrieve

$section : string = ''

Section name (uses default section if empty)

$default : bool = false

Default value if key not found

Tags
throws
RuntimeException

if configuration is not initialized

Return values
bool

The configuration value as boolean

getEnum()

Get a configuration value that must match one of the allowed options.

public static getEnum(string $key[, string $section = '' ][, array<string|int, mixed> $allowed = [] ][, string $default = '' ]) : string
Parameters
$key : string

The configuration key to retrieve

$section : string = ''

Section name (uses default section if empty)

$allowed : array<string|int, mixed> = []

List of allowed values

$default : string = ''

Default value if key not found or invalid

Tags
throws
RuntimeException

if configuration is not initialized

Return values
string

The configuration value if valid, default otherwise

getFloat()

Get a configuration value as a float.

public static getFloat(string $key[, string $section = '' ][, float $default = 0.0 ]) : float
Parameters
$key : string

The configuration key to retrieve

$section : string = ''

Section name (uses default section if empty)

$default : float = 0.0

Default value if key not found

Tags
throws
RuntimeException

if configuration is not initialized

Return values
float

The configuration value as float

getIni()

Get the underlying Ini configuration object.

public static getIni() : Ini

Useful for advanced operations that aren't covered by the static facade.

Tags
throws
RuntimeException

if configuration is not initialized

Return values
Ini

The configuration object

getInt()

Get a configuration value as an integer.

public static getInt(string $key[, string $section = '' ][, int $default = 0 ]) : int
Parameters
$key : string

The configuration key to retrieve

$section : string = ''

Section name (uses default section if empty)

$default : int = 0

Default value if key not found

Tags
throws
RuntimeException

if configuration is not initialized

Return values
int

The configuration value as integer

getJsonArray()

Get a configuration value as a JSON-decoded array.

public static getJsonArray(string $key[, string $section = '' ][, array<string|int, mixed> $default = [] ]) : array<string|int, mixed>
Parameters
$key : string

The configuration key to retrieve

$section : string = ''

Section name (uses default section if empty)

$default : array<string|int, mixed> = []

Default value if key not found or invalid JSON

Tags
throws
RuntimeException

if configuration is not initialized

Return values
array<string|int, mixed>

The configuration value as array from JSON

getSection()

Get all key-value pairs from a section.

public static getSection([string $section = '' ]) : array<string|int, mixed>
Parameters
$section : string = ''

Section name (uses default section if empty)

Tags
throws
RuntimeException

if configuration is not initialized

Return values
array<string|int, mixed>

All configuration values from the section

has()

Check if a configuration key exists.

public static has(string $key[, string $section = '' ]) : bool
Parameters
$key : string

The configuration key to check

$section : string = ''

Section name (uses default section if empty)

Tags
throws
RuntimeException

if configuration is not initialized

Return values
bool

True if key exists, false otherwise

initialize()

Initialize the application configuration.

public static initialize(string $filename[, string $defaultSection = 'default' ]) : void
Parameters
$filename : string

Path to the INI configuration file

$defaultSection : string = 'default'

Default section to use when none specified (defaults to 'default')

Tags
throws
RuntimeException

if the INI file cannot be loaded

isInitialized()

Check if the configuration has been initialized.

public static isInitialized() : bool
Return values
bool

True if initialized, false otherwise

reload()

Re-initialize with new settings.

public static reload(string $filename[, string $defaultSection = 'default' ]) : void
Parameters
$filename : string

Path to the INI configuration file

$defaultSection : string = 'default'

Default section to use when none specified (defaults to 'default')

reset()

Reset the configuration (useful for testing).

public static reset() : void

ensureInitialized()

Ensure configuration is initialized, throw exception if not.

private static ensureInitialized() : void
Tags
throws
RuntimeException

if configuration is not initialized


        
On this page

Search results