Git Product home page Git Product logo

config's Introduction

davidlienhard/config

๐Ÿ˜ php library to get configuration data from json/yaml 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 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 exceptions
    • Conversion - Errors that happen during type conversion. eg trying to convert a string to an array
    • Mismatch - Trying to access configuration data that is not available
      • FileMismatch - Trying to access a file that does not exist
        • Parser - 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.

config's People

Contributors

dependabot[bot] avatar davidlienhard avatar

Watchers

James Cloos avatar  avatar

config's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.