🐘 php library to get configuration data from json files

2.1.0 2021-07-21 15:07 UTC

This package is auto-updated.

Last update: 2021-09-13 05:30:20 UTC


README

🐘 php library to get configuration data from json files

Latest Stable Version Source Code Software License Minimum PHP Version CI Status

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 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 */

License

The MIT License (MIT). Please see LICENSE for more information.