Tools for handling configuration. Interfaces, implementation class, various readers and a factory.
Installation
Install with Composer;
composer require phrity/config
The ConfigurationInterface
interface
The Phrity\Config\ConfigurationInterface
extends
PSR-11 ContainerInterface and
JsonSerializable interfaces.
// ContainerInterface implementation
public function get(string $id): mixed;
public function has(string $id): bool;
// JsonSerializable implementation
public function jsonSerialize(): mixed;
// Additional methods
public function __construct(object|array $config);
public function merge(ConfigurationInterface $config): ConfigurationInterface;
The Configuration
class
The Phrity\Config\Configuration
class implements the ConfigurationInterface
.
use Phrity\Config\Configuration;
// Initiate with object or associative array
$config = new Configuration([
'a' => 23,
'b' => [
'bb' => 66,
],
]);
// Check and get (case insensitive) from configuration
$config->has('a'); // => true
$config->get('a'); // => 23
// Trying to get non-exising configuration will throw exception
$config->has('c'); // => false
$config->get('c'); // throws NotFoundException
Accessing by path
// It is possible to access by path
$config->has('b/bb'); // => true
$config->get('b/bb'); // => 66
Specifying default
// If default is specified, non-exising configuration will return that value instead of throwing exception
$config->get('a', default: 99); // => 23
$config->get('c', default: 99); // => 99
Type coercion
// Some types can be coerced into another type
$config->get('a', coerce: 'string'); // => "23"
- Target type
boolean
0
,0.0
,"0"
,"0.0"
,""
,"false"
andnull
will be coerced tofalse
1
,1.1
,"1"
,"1.0"
and"true"
will be coerced totrue
- Target types
integer
anddouble
- numeric
string
will be coerced tointeger
ordouble
null
andfalse
will be coerced to0
or0.0
true
will be coerced to1
or1.0
- numeric
- Target type
string
integer
anddouble
will be coerced to numericstring
null
will be coerced to"null"
false
will be coerced to"false"
true
will be coerced to"true"
- Target type
null
0
,0.0
,"0"
,"0.0"
,""
,"null"
andfalse
will be coerced tonull
Any coercion not specified above will cause a CoercionException
.
Merging configurations
// Configurations can be merged (immutable, new instance will be returned)
$additional = new Configuration(['c' => 12, 'b' => ['bc' => 13]]);
$merged = $config->merge($additional);
The Reader classes
A number of configuration readers are available.
- DataReader - Reader for PHP data input
- EnvReader and EnvFileReader - Readers for ENV input
- JsonReader and JsonFileReader - Readers for JSON input
- YamlReader and YamlFileReader - Readers for YAML input
The ConfigurationFactory
class
The Phrity\Config\ConfigurationFactory
provides shortcuts to create and merge configurations.
$factory = new Phrity\Config\ConfigurationFactory();
$configData = $factory->fromData(data: ['a' => 23]);
$configJson = $factory->fromJson(json: '{"a": 23}');
$configJsonFile = $factory->fromJsonFile(path: 'path/to/config.json');
$configYaml = $factory->fromYaml(yaml: 'n: 23');
$configYamlFile = $factory->fromYamlFile(path: 'path/to/config.yaml');
$configEnv = $factory->fromEnv();
$configEnvFile = $factory->fromEnvFile('.env');
$configMerged = $factory->merge(
$configData,
$configJson,
$configJsonFile,
$configYaml,
$configYamlFile,
$configEnv,
$configEnvlFile,
);
Versions
Version | PHP | |
---|---|---|
1.3 |
^8.1 |
Coerce option |
1.2 |
^8.1 |
Reader (data), all file-readers get optional option |
1.1 |
^8.1 |
Readers (yaml, env-file) |
1.0 |
^8.1 |
Interface, implementation, readers (json, json-file, yaml-file, env), factory |