phrity / config
Configuration interface, class, and factories
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-04-19 11:15:07 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 // It is possible to access by path $config->has('b/bb'); // => true $config->get('b/bb'); // => 66 // 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 // 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.
The DataReader
class
The Phrity\Config\DataReader
takes input as associative array, object, or null.
$reader = new Phrity\Config\DataReader(); $config = $reader->createConfiguration(data: ['a' => 23]);
The JsonReader
class
The Phrity\Config\JsonReader
parses provided JSON string.
$reader = new Phrity\Config\JsonReader(); $config = $reader->createConfiguration(json: '{"a": 23}');
The JsonFileReader
class
The Phrity\Config\JsonFileReader
parses a file containing JSON.
$reader = new Phrity\Config\JsonFileReader(); $config = $reader->createConfiguration(path: 'path/to/config.json');
The YamlReader
class
The Phrity\Config\YamlReader
parses provided YAML string.
The symfony/yaml
library must be required to use this reader.
$reader = new Phrity\Config\YamlReader(); $config = $reader->createConfiguration(yaml: 'a: 23');
The YamlFileReader
class
The Phrity\Config\YamlFileReader
parses a file containing YAML.
The symfony/yaml
library must be required to use this reader.
$reader = new Phrity\Config\YamlFileReader(); $config = $reader->createConfiguration(path: 'path/to/config.yaml');
The EnvReader
class
The Phrity\Config\EnvReader
parses environment variables.
$reader = new Phrity\Config\EnvReader(); $config = $reader->createConfiguration();
The EnvFileReader
class
The Phrity\Config\EnvFileReader
parses a file containing env data.
The symfony/dotenv
library must be required to use this reader.
$reader = new Phrity\Config\EnvFileReader(); $config = $reader->createConfiguration(path: 'path/to/.env');
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.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 |