config
Tools for handling configuration. Interfaces, implementation classes, various readers, writers and a factory.
Installation
Install with Composer;
composer require phrity/config
Configuration
Implements PSR-11 ContainerInterface, providing get() and has() methods.
$config = new Configuration(['opt1' => 11, 'opt2' => 22]);
$config->has('opt1'); // -> true
$config->get('opt1'); // -> 11
Support path access, default value and type coercion.
$config->has('a/b/c');
$config->get('a/b/c', default: 99, coerce: 'string');
Support merging.
$merged = $config->merge(new Configuration($data));
Read more on Configuration.
Writable configuration
Identical to Configuration but also support writing values on the configuration.
$config = new WritableConfiguration(['opt1' => 11, 'opt2' => 22]);
$config->set('opt1', 123);
$config->with('opt1', 123);
Read more on WritableConfiguration.
Reader classes
Classes that create configuration instance from various sources.
$reader = new JsonFileReader();
$config = $reader->createConfiguration(path: 'path/to/file.json');
List of available readers:
- DataReader - Reader for PHP data input
- DirectoryReader - Reads and merges all supported configuration files in directory
- EnvReader and EnvFileReader - Readers for ENV input (file reader requires
symfony/dotenv) - JsonReader and JsonFileReader - Readers for JSON input
- NeonReader and NeonFileReader - Reader for NEON input (requires
nette/neon) - YamlReader and YamlFileReader - Readers for YAML input (requires
symfony/yaml)
Writer classes
Classes that write configuration to file.
$reader = new JsonFileWriter();
$config = $reader->writeConfiguration($config, 'path/to/file.json');
List of available writers:
- JsonFileWriter - Writer for JSON output
Factory
A factory that provides shortcuts to create and merge configurations.
$factory = new ConfigurationFactory();
$config = $factory->fromJsonFile(path: 'path/to/config.json');
Read more on ConfigurationFactory.