Config
extends EnvironmentVariables
in package
Class Config
Table of Contents
Constants
- PATH_CACHED_CONFIG : string = DIRECTORY_SEPARATOR . 'runtime' . DIRECTORY_SEP...
- The path to the cached configuration files.
Properties
- $configFileName : array<string|int, mixed>
- An array of all possible config file names.
- $environmentEnsuredKeys : array<string|int, mixed>
- An array of environment keys that are ensured to be set.
- $from : array<string, string>
- An array of configuration file names to load from.
Methods
- __construct() : mixed
- Constructs a new instance of the Config class.
- autoLoad() : bool
- Automatically loads the configuration files.
- get() : mixed
- Retrieves the value of the specified config key.
- getEnvironmentVariable() : mixed
- Gets an environment variable.
- checkEnvironmentFileExists() : bool
- Checks if the environment file exists and is readable.
- ensureRequiredEnvironmentVariables() : void
- Ensure required environment variables are set.
- load() : void
- Loads the environment variables from the specified path.
- normalizeEnvValue() : mixed
- Normalize environment variable values.
- setEnvironmentPath() : string
- Sets the environment path by trimming the path and adding the '.env' extension if needed.
- checkConfigCacheIsAvailable() : bool
- Checks if the config cache is available.
- getConfigFileNames() : array<string|int, mixed>
- Gets an array of all possible config file names.
Constants
PATH_CACHED_CONFIG
The path to the cached configuration files.
protected
string
PATH_CACHED_CONFIG
= DIRECTORY_SEPARATOR . 'runtime' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR
This path is used to store cached configuration files.
Properties
$configFileName
An array of all possible config file names.
protected
static array<string|int, mixed>
$configFileName
= [
// The application configuration file.
'app',
// The database configuration file.
'database',
// The cache configuration file.
'cache',
// The session configuration file.
'session',
// The mail configuration file.
'mail',
// The authentication configuration file.
'auth',
// The filesystem configuration file.
'filesystem',
// The module configuration file.
'modul',
// The logger configuration file.
'logger',
]
$environmentEnsuredKeys
An array of environment keys that are ensured to be set.
protected
static array<string|int, mixed>
$environmentEnsuredKeys
= [
// 'app' => ['name', 'env', 'debug', 'url', 'timezone', 'locale', 'fallback_locale', 'key', 'cipher'],
'database' => ['DB_HOST', 'DB_PORT', 'DB_DATABASE', 'DB_USERNAME', 'DB_PASSWORD'],
]
The keys in this array are the names of the configs that are ensured to be set. The values of this array are arrays of environment variables that are ensured to be set for each config.
Example: [ 'app' => ['name', 'env', 'debug', 'url', 'timezone', 'locale', 'fallback_locale', 'key', 'cipher'], 'database' => ['DB_HOST', 'DB_PORT', 'DB_DATABASE', 'DB_USERNAME', 'DB_PASSWORD'], ]
$from
An array of configuration file names to load from.
protected
static array<string, string>
$from
= []
This array is used to determine which configuration files to load. The keys of this array are the names of the configuration files to load, and the values of this array are the paths to the configuration files.
Methods
__construct()
Constructs a new instance of the Config class.
public
__construct(string $appPath[, bool $autoLoadConfig = true ][, bool $autoLoadConfigFromCache = true ][, string|null $environmentPath = null ]) : mixed
Parameters
- $appPath : string
-
The path to the application root directory.
- $autoLoadConfig : bool = true
-
If true, the configuration files will be loaded automatically.
- $autoLoadConfigFromCache : bool = true
-
If true, the configuration files will be loaded from cache if available.
- $environmentPath : string|null = null
-
The path to the environment file. If null, the default path will be used.
autoLoad()
Automatically loads the configuration files.
public
static autoLoad(string $appPath[, bool $autoLoadConfigFromCacheIfAvailable = true ][, string|null $environmentPath = null ]) : bool
Parameters
- $appPath : string
-
The path to the application root directory.
- $autoLoadConfigFromCacheIfAvailable : bool = true
-
If true, the configuration files will be loaded from cache if available.
- $environmentPath : string|null = null
-
The path to the environment file. If null, the default path will be used.
Tags
Return values
bool —True if the configuration files were successfully loaded, false otherwise.
get()
Retrieves the value of the specified config key.
public
static get(string $key[, bool $useDefaultChoice = true ]) : mixed
Parameters
- $key : string
-
The config key.
- $useDefaultChoice : bool = true
-
If true, the default choice of the config key will be used.
Tags
Return values
mixed —The value of the config key.
getEnvironmentVariable()
Gets an environment variable.
public
static getEnvironmentVariable(string $key[, mixed $default = null ]) : mixed
Gets an environment variable from the global $_ENV array. If the environment variable is not set, the default value will be returned.
Parameters
- $key : string
-
The key of the environment variable.
- $default : mixed = null
-
The default value to return if the environment variable is not set.
Return values
mixed —The value of the environment variable or the default value.
checkEnvironmentFileExists()
Checks if the environment file exists and is readable.
protected
static checkEnvironmentFileExists(string $path) : bool
Parameters
- $path : string
-
The path to the environment file.
Return values
bool —True if the file exists and is readable, false otherwise.
ensureRequiredEnvironmentVariables()
Ensure required environment variables are set.
protected
static ensureRequiredEnvironmentVariables() : void
Iterate over the list of required environment variables and throw an exception if any of them are not set.
Tags
load()
Loads the environment variables from the specified path.
protected
static load(string $path) : void
Parameters
- $path : string
-
The path to the environment file.
normalizeEnvValue()
Normalize environment variable values.
protected
static normalizeEnvValue(mixed $value) : mixed
Normalize environment variable values from their string representation to their actual values. This includes values like "true", "(true)", "false", "(false)", "empty", "(empty)", "null" and "(null)".
Parameters
- $value : mixed
-
The value to normalize.
Return values
mixed —The normalized value.
setEnvironmentPath()
Sets the environment path by trimming the path and adding the '.env' extension if needed.
protected
static setEnvironmentPath(string|null $path, string $appPath) : string
Parameters
- $path : string|null
-
The path to be set. If empty, the default path will be used.
- $appPath : string
-
The path to the application.
Return values
string —The set environment path.
checkConfigCacheIsAvailable()
Checks if the config cache is available.
private
static checkConfigCacheIsAvailable(string $appPath) : bool
Parameters
- $appPath : string
-
The path to the application.
Return values
bool —True if the config cache is available, false otherwise.
getConfigFileNames()
Gets an array of all possible config file names.
private
static getConfigFileNames() : array<string|int, mixed>
Return values
array<string|int, mixed> —An array of all possible config file names.