Iconfig – Instant config manager for PHP
Iconfig
(Instant config) is a stand alone component/package for any PHP
application, which is built to easily manage configurations for any PHP
application, available on Packagist.
How To Use :
At first, install it using composer, if you don’t know how to use composer
then learn to use it and add following to require
section in your composer.json
file like :
{ "require": { "sheikhheera/iconfig": "dev-master" } }
This is also available on gitHub page, you can directly download the package from there.
How It Works ?
Basically, PHP
applications use array
for configurations. An MVC
framework or an application without any framework must have some common settings and user can configure those according to his/her need and most often all configuration files reside in a single folder, commonly, the config name is used. So, keeping that on mind, this dynamic configuration manager (or whatever you say) has been built, which loads all files from a given path.
For example, this is a sample of database.php
file used for database configurations, which is located in config
folder as app/config/database.php
in one of my MVC
application
return array( 'default' => 'mysql', 'connections' => array( 'sqlite' => array( 'driver' => 'sqlite', 'database' => 'public/caliber.sqlite', 'prefix' => 'cb_', ), 'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'caliber', 'username' => 'root', 'password' => 'bossboss', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => 'cb_', ) ), );
There are also other configuration files, such as, app/config/session.php
for session, app/config/cache.php
for cache and so on. On the boot-up process, these files being loaded to set the application’s environment and kept in the memory using an array
to use those settings further, on the run time of application. So, to make this process easy, this package/library could be used as a stand alone component with any PHP
project and to use it you can initialize it as :
$config = new Iconfig\Config('config'); // or '../app/config/'
Above code will load all files from the config folder (it expects arrays inside files) and will put everything in an array (groups using file name). Now, you can set/get any item from the array. For example, if you want to get the default item from the array, then you can use
$default = $config->getDatabase('default'); // mysql
Now, what is getDatabase()
? Actually, in this example, I’ve used the file name database.php
for this array so I can use getDatabase() and setDatabase() to get or set an item. If I have a file named with session.php then I can use getSession() and setSession() to get or set any settings for session management. Which means, when you will pass the path (where you all configuration files are saved) to the constructor it’l load all ‘php’ files from that path/folder. So If, for example, in a folder named settings you have three files inside that folder as database.php, session.php and for example chache.php and if you initialize it using
$settings = new Iconfig\Config('settings');
Then, it’ll load all three php files from the folder and it’ll create one associative array using three groups like :
Array( 'database' => array( 'default' => 'mysql', 'connections' => array( 'sqlite' => array( 'driver' => 'sqlite', 'database' => 'public/caliber.sqlite', 'prefix' => 'cb_', ) ), 'session' => array( 'driver' => 'native', 'lifetime' 120, 'files' => '/sessions' ), 'chache' =>array( 'path' => 'c:/web/app/storage' ) );
Now, you can use this array to retrieve a setting or you can also set/change any predefined settings using dynamic methods. You can use setDatabase()
and getDatabase
to set/get database’ configurations and setSession() and getSession for session and so on. These dynamic methods will be available to you after initialization, using php’s overloading
technic.
// set/get database settings $settings->setDatabase('default', 'sqlite'); $settings->getDatabase('default'); // sqlite // set/get session settings $settings->setSession('lifetime', 240); $settings->getSession('lifetime'); // 240
If you want, you can set an alias
and can use methods statically like this :
new Iconfig\Config('../myApp/config', 'Config'); // Config as Alias, you can use any name if(Config::isExist('session')) { Config::setSession('driver', 'database'); $sessionArray = Config::getSession(); // full array will be returned when called without argument }
Also you can use a default value like this :
// if path doesn't exist then "/web" will be returned $chache = getChache('path', '/web');
If you have three database connections and all have a driver key then you can specify which driver key you want like this (get item from nested array) :
Config::getDatabase('connections.sqlite.driver'); // get the driver from sqlite Config::getDatabase('connections.pgsql.driver'); // get the driver from pgsql
You can use closure as a callback as getMethod($key, $callback)
, for example, you can use :
$connections = Config::getDatabase('connections', function($data){ if(is_array($data) && array_key_exists('sqlite', $data)) { Config::setDatabase('connections.sqlite.driver', 'myNewSqliteDriver'); return Config::getDatabase('connections'); // this will return connections array with new value } });
Same thing could be used when setting a value as setDatabase('connections.pgsql.driver', 'pgsql')
. You can also use find()
method to search an item if you don’t know where the item is in the array using :
// if it exists, you'll get the value Config::find('sqlite'); // it'll look sqlite in to the connections Config::find('connections.sqlite'); // it'll look driver in to the connections.sqlite array Config::find('connections.sqlite.driver');
Also you can use getAll()
Method to retrieve all the configurations from the array like :
$all = Config::getAll();
var_dump($all); // all configurations will be returned
[/code]
If you want, you can use load()
method to load more files as ;
Config::load('filePath'); // new items will be added.
Well, that’s all for now. Feel free to use or modify it to improve it’s functionality or if you find any bug, please inform me. Hope, I’ll be able to add more features in future In-Sha-Allah (on God’s will). Thanks!