Env
in package
implements
ISettingSource
environment variable based settings.
Table of Contents
Interfaces
- ISettingSource
- Access to setting based services.
Properties
Methods
- __construct() : mixed
- get() : mixed
- This method is used to get the value of a setting stored in an environment variable.
- getSection() : array<string|int, mixed>|null
- Get the entire section as an array.
- getSectionNames() : array<string|int, mixed>
- This method is used to get the section names.
- getSectionSettingNames() : array<string|int, mixed>
- This method is used to get the setting names for a section.
- save() : bool
- This method is used to save the settings to the environment variables.
- set() : ISettingSource
- This method is used to set the value of a setting stored in an environment variable.
- parseValue() : mixed
- Parse environment variable value, auto-detecting arrays and deserializing special types.
Properties
$env
private
Env
$env
Methods
__construct()
public
__construct(Env $env) : mixed
Parameters
- $env : Env
get()
This method is used to get the value of a setting stored in an environment variable.
public
get(string $sectionName, string $name) : mixed
The section and name will be concatenated with an underscore and the value will be uppercased. e.g. get( 'test', 'name' ) will look for the environment variable TEST_NAME.
Supports automatic array parsing:
- JSON format: '["value1","value2"]' returns array
- Comma-separated: 'value1,value2,value3' returns array
- Plain string: 'value' returns string (backward compatible)
Parameters
- $sectionName : string
- $name : string
getSection()
Get the entire section as an array.
public
getSection(string $sectionName) : array<string|int, mixed>|null
For environment variables we scan matching keys and return an associative array of name => value. Returns null if a section not present. Values are automatically parsed (arrays from JSON/CSV are returned as arrays).
Parameters
- $sectionName : string
Return values
array<string|int, mixed>|nullgetSectionNames()
This method is used to get the section names.
public
getSectionNames() : array<string|int, mixed>
For environment variables we can infer section names by scanning environment keys that contain an underscore and returning the prefix before the underscore.
Return values
array<string|int, mixed>getSectionSettingNames()
This method is used to get the setting names for a section.
public
getSectionSettingNames(string $section) : array<string|int, mixed>
We scan environment variable keys for those starting with SECTIONNAME_ and return the suffix names in lowercase (matching other sources' expectations).
Parameters
- $section : string
Return values
array<string|int, mixed>save()
This method is used to save the settings to the environment variables.
public
save() : bool
This is not possible, so this method will always return false.
Return values
boolset()
This method is used to set the value of a setting stored in an environment variable.
public
set(string $sectionName, string $name, mixed $value) : ISettingSource
The section and name will be concatenated with an underscore and the value will be uppercased. e.g. set( 'test', 'name', 'value' ) will set the environment variable TEST_NAME=value.
Non-scalar values (arrays, objects) are automatically serialized to JSON format to prevent data corruption. The get() method will automatically deserialize them.
Parameters
- $sectionName : string
- $name : string
- $value : mixed
Return values
ISettingSourceparseValue()
Parse environment variable value, auto-detecting arrays and deserializing special types.
private
parseValue(string $value) : mixed
Parameters
- $value : string