Skip to content
forked from hassankhan/config

Config is a lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

Notifications You must be signed in to change notification settings

igarciaes/config

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

config

Build Status

config is a file configuration loader that supports PHP, INI, XML, JSON, and YAML files. Files are parsed and loaded depending on the file extension.

Some examples of valid configuration files are below

api

The Config object can be statically created or instantiated:

$conf = Config::load('config.json');
$conf = new Config('config.json');

Use get() to retrieve values:

// Get value using key
$debug  = $config->get('debug');

// Get value using nested key
$secret = $config->get('security.secret');

// Get a value with a fallback
$ttl    = $config->get('app.timeout', 3000);

Use set() to set values (doh!):

$conf = Config::load('config.json');
$conf = new Config('config.json');

examples

Here's an example JSON file that we'll call config.json.

{
    "app": {
        "host": "localhost",
        "port": 80,
        "base": "/my/app"
    },
    "security": {
        "secret": "s3cr3t-c0d3"
    },
    "debug": false
}

Here's the same config file in PHP format:

<?php
return array(
    'app' => array(
        'host' => 'localhost',
        'port' => 80,
        'base' => '/my/app'
    ),
    'security' => array(
        'secret' => 's3cr3t-c0d3'
    ),
    'debug' => false
);

Or in a PHP file that returns a function that creates your config:

return function () {
    // Normal callable function, returns array
    return array(
    'app' => array(
        'host' => 'localhost',
        'port' => 80,
        'base' => '/my/app'
    ),
    'security' => array(
        'secret' => 's3cr3t-c0d3'
    ),
    'debug' => false
    );
};

Or in an INI format:

debug = false

[app]
host = localhost
port = 80
base = /my/app

[security]
secret = s3cr3t-c0d3

Or in an XML format:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <app>
        <host>localhost</host>
        <port>80</port>
        <base>/my/app</base>
    </app>
    <security>
        <secret>s3cr3t-c0d3</secret>
    </security>
    <debug>false</debug>
</config>

Or in a YAML format:

app:
    host: localhost
    port: 80
    base: /my/app
security:
    secret: s3cr3t-c0d3
debug: false

license

MIT: http://noodlehaus.mit-license.org

About

Config is a lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%