getsky/phalcon-config-loader

Component to load configuration from various files

Installs: 412

Dependents: 2

Stars: 4

Watchers: 1

Forks: 2

Language: PHP

v1.1.3 2014-10-31 12:28 UTC

README

ConfigLoader - it's manager configuration files for Phalcon. It allows you to create a configuration of various formats (ini, yaml, JSON, PHP arrays or any other, for which you will add adapter) via a single method.

$configYml = $configLoader->create('config.yml');
$configIni = $configLoader->create('config.ini');
$configPhp = $configLoader->create('config.php');


// or use string
$string = 'foo = bar'
$configFromText = $configLoader->fromText($string, 'ini');

// or use arrays
$config = ['foo' => 'bar'];
$configFromArray = $configLoader->fromArray($config);

ConfigLoader is able to track %environment% in configuration files and replace it on our environment.

// Create ConfigLoader and specify the environment of our application
$configLoader = new ConfigLoader('prod');

// config.yml : test: %environment%
$configYml = $configLoader->create('config.yml');

echo $configYml->test;
// print: prod 

To add your adapter, you must call add () with transfer expansion and adapter class, which must inherit a class Phalcon\Config:

$config = $configLoader->add('xml', 'MyNamespace/XmlConfig');

Moreover, you can merge configuration files:

#config.ini
[test]
test = true
%res% = import.ini
exp = %res:import.ini
%class% = Test/Class::SERVICES
import-class =  %class:Test/Class::SERVICES
#import.ini
import = "test"
namespace Test;

class Class {
  const SERVICES = '/const.ini';
}
#const.ini
class = "class"

The result loading configuration from config.ini:

[                               
    'test' => [                 
        'test' => true,                             
        'import' => true,       
        'env' => 'dev',
        'exp' => [
            'import' => true,
            'env' => 'dev'
        ],
        'class' => "class",
        'impot-class' => [
            'class' => "class"
        ]
    ]                           
]                               

Declared variables in the parent file will not be replaced by variables from the child (only %res% or %class%):

# /app/config/config.ini
%res% = include.ini
[foo]
test = config-test
# /app/config/include.ini
[foo]
test = test
bar = bar
# result
[foo]
test = config-test
bar = bar

If you do not want to import resources (loading of the other configuration files in this configuration), the second parameter must pass a boolean false:

$config = $configLoader->create('config.ini', false);