selner / Config

PHP library for simple configuration management

Home Page:https://packagist.org/packages/phlak/config

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Config

Latest Stable Version Total Downloads Author License Build Status

PHP library for simple configuration management -- by, Chris Kankiewicz

Introduction

Config is a simple PHP configuration management library supporting multiple, configuration file formats and an expressive API.

Supported file formats:

  • PHP
  • INI
  • JSON
  • YAML
  • XML

Like this project? Keep me caffeinated by making a donation.

Requirements

Install with Composer

composer require phlak/config

Initializing the Client

First, import Config:

use PHLAK\Config;

Then instantiate the class:

$config = new Config\Config($context);

Where $context is a path to a supported file type, a directory containing one or more supported file types or an array of configuration options.

Configuration File Formats

PHP

A PHP configuration file must have the .php file extension, be a valid PHP file and and return a valid PHP array.

<?php

return [
    'driver' => 'mysql',
    'drivers' => [
        'sqlite' => [
            'database' => 'database.sqlite',
            'prefix' => ''
        ],
        'mysql' => [
            'host' => 'localhost',
            'database' => 'blog',
            'username' => 'blogger',
            'password' => 'hunter2',
            'charset' => 'utf8',
            'prefix' => ''
        ]
    ]
];

INI

An INI configuration file must have the .ini file extension and be a valid INI file.

driver = mysql

[drivers]

sqlite[database] =  database.sqlite
sqlite[prefix] =

mysql[host] = localhost
mysql[database] = blog
mysql[username] = blogger
mysql[password] = hunter2
mysql[charset] = utf8
mysql[prefix] =

JSON

A JSON configuration file must have the .json file extension and contain a valid JSON object.

{
    "driver": "mysql",
    "drivers": {
        "sqlite": {
            "database": "database.sqlite",
            "prefix": ""
        },
        "mysql": {
            "host": "localhost",
            "database": "blog",
            "username": "blogger",
            "password": "hunter2",
            "charset": "utf8",
            "prefix": ""
        }
    }
}

YAML

A YAML configuration file must have the .yaml file extension, be a valid YAML file.

driver: mysql

drivers:

  sqlite:
    database: database.sqlite
    prefix:

  mysql:
    host: localhost
    database: blog
    username: blogger
    password: hunter2
    charset: utf8
    prefix:

XML

A XML configuration file must have the .xml file extension and contain valid XML.

<?xml version='1.0'?>

<database>
    <driver>mysql</driver>
    <drivers>
        <sqlite>
            <database>database.sqlite</database>
            <prefix></prefix>
        </sqlite>
        <mysql>
            <host>localhost</host>
            <database>blog</database>
            <username>blogger</username>
            <password>hunter2</password>
            <charset>utf8</charset>
            <prefix></prefix>
        </mysql>
    </drivers>
</database>

Usage

Set a configuration option:

$config->set($key, $value);

Retrieve a configuration option:

$config->get($key, $default = null);

Check if a configuration option exists:

$config->has($key, $override = false);

Load an additional configuration file:

$conifg->load($pathToConfig, $override = true);

Merge two Config objects into one:

$config = new Config\Config(['foo' => 'foo', 'baz' => 'baz']);
$gifnoc = new Config\Config(['bar' => 'rab', 'baz' =>'zab']);

$config->merge($gifnoc);

$config->get('foo'); // Returns 'foo'
$config->get('bar'); // Returns 'rab'
$config->get('baz'); // Returns 'zab'

Split a sub-array of options into it's own object:

$config = new Config\Config([
    'foo' => 'foo',
    'bar' => [
        'baz' => 'barbaz'
    ]
]);

$bar = $config->split('bar');

$bar->get('baz'); // Returns 'barbaz'

Troubleshooting

Please report bugs to the GitHub Issue Tracker.

Copyright

This project is liscensed under the MIT License.

About

PHP library for simple configuration management

https://packagist.org/packages/phlak/config

License:MIT License


Languages

Language:PHP 100.0%