phrity / config
Configuration interface, class, and factories
Installs: 1 060
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- ext-json: *
- phrity/util-accessor: ^1.0
- psr/container: ^1.0 | ^2.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^10.0 | ^11.0
- squizlabs/php_codesniffer: ^3.0
- symfony/dotenv: ^6.0 | ^7.0
- symfony/yaml: ^6.0 | ^7.0
Suggests
- symfony/dotenv: Required for loading .env files
- symfony/yaml: Required for loading YAML configuration
This package is auto-updated.
Last update: 2024-12-18 13:49:47 UTC
README
Introduction
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, );