davidlienhard / config
🐘 php library to get configuration data from json files
Installs: 5 703
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 1
Type:libary
Requires
- php: ^8.0
- ext-json: ^8.0
- davidlienhard/functioncaller: ^1
- league/flysystem: ^3
- nette/neon: ^3
- symfony/yaml: ^6
Requires (Dev)
README
🐘 php library to get configuration data from json/yaml files
Setup
You can install through composer
with:
composer require davidlienhard/config:^2
Note: davidlienhard/config requires PHP 8.0
Examples
Setup
<?php declare(strict_types=1); use DavidLienhard\Config\Config; try { $config = new Config("path/to/config"); } catch (\Throwable $t) { echo "unable to setup config"; exit(1); }
Read Data
Example Config File: system.json
{ "name": "test", "list1": { "key1": "value1", "key2": "value2", "key3": "value3", "key4": "value4" }, "list2": [ "value1", "value2", "value3", "value4" ] }
get single value
<?php declare(strict_types=1); echo $config->get("system", "name"); /* test */ echo $config->get("system", "list1", "key1"); /* value1 */
get single value with a specific type
<?php declare(strict_types=1); echo $config->getAsString("system", "name"); /* if the value exists and is not an array, it will return a string in any case if the value is an array, this will throw an exception if the value does not exist this will return null */
the following methods do exists
getAsString()
getAsInt()
getAsFloat()
getAsBool()
getAsArray()
get associative array
<?php declare(strict_types=1); print_r($config->get("system", "list1")); /* Array ( [key1] => value1 [key2] => value2 [key3] => value3 [key4] => value4 ) */
get numeric array
<?php declare(strict_types=1); print_r($config->get("system", "list2")); /* Array ( [0] => value1 [1] => value2 [2] => value3 [3] => value4 ) */
get not existing value
<?php declare(strict_types=1); var_dump($config->get("system", "doesnotexist")); /* NULL */
get data from not existing file
<?php declare(strict_types=1); var_dump($config->get("doesnotexist")); /* throws \Exception */
Parsers / Supported Filetypes
By default, this library contains two parsers. One for Json & one for Yaml/Yml files.
If required it is possible to add a customer parser for othe filetypes, i.e. XML or INI.
The custom parser must extend the class ParserAbstract
and implement the Interface ParserInterface
.
A parser can be registered as follows:
<?php declare(strict_types=1); try { $config->registerParser(\your\custom\parser::class); } catch (ConfigException $e) { die("unable to register custom parser"); }
Exceptions
The library currently contains the following exceptions
Config
- Main Exception that is parent of all other exceptionsConversion
- Errors that happen during type conversion. eg trying to convert a string to an arrayMismatch
- Trying to access configuration data that is not availableFileMismatch
- Trying to access a file that does not existParser
- errors that happen when parsing files. usually through invalid files
KeyMismatch
- Trying to access a key that does not exists, while the file is present
License
The MIT License (MIT). Please see LICENSE for more information.