Yii 1.1: flexibleconfig

Flexible configuration for Yii application.
4 followers

The extension is designed for flexible application configuration. Represents the behavior for the СApplication class. Adds an object with one method - loadConfigure().

Features

  • Easy connection configuration file;
  • Inheritance of configuration files;
  • Automatic load config from *_local.php;

Settings

  • string FConfig::configDir - path to the configuration file, defaults to 'application.config';
  • array FConfig::configs - array with a list of configurations, key - the name of the configuration, value - an array of configuration settings;
  • string FConfig::currentConfig - the name of the current configuration;

Each configuration has a property 'parent' - is a string with the name of the parent configuration.

Example 1

I need to have two different application configuration mode for the development and production server. Most of the settings they have the same, and some do not. To do this, I have three files in the folder config: main.php - contains general settings for all configurations; dev.php - contains specific design-time configuration; production.php - contains settings specific to the production.

index.php file I have contains the following code:

<?php
 
    $yii = 'yii/framework/yii.php';
 
    // remove the following lines when in production mode
    defined('YII_DEBUG') or define('YII_DEBUG', true);
    // specify how many levels of call stack should be shown in each log message
    defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 3);
 
    require_once($yii);
    Yii::createWebApplication(array(
        'behaviors' => array(
            'fconfig' => array(
                'class' => 'ext.FlexibleConfig.FConfig',
                'currentConfig' => 'dev',
                'configs' => array(
                    'dev' => array(
                        'parent' => 'main',
                    ),
                    'production' => array(
                        'parent' => 'main',
                    ),
                ),
            ),
        ),
    ))->loadConfigure()->run();

In addition to the config folder is a file dev_local.php, which is automatically loaded after dev.php and overrides the necessary local settings.

Example 2

index.php

<?php
 
    $yii='yii/framework/yii.php';
    $config=include(dirname(__FILE__).'/protected/config/main.php');
 
    require_once($yii);
    Yii::createWebApplication($config)->loadConfigure()->run();

config/main.php

<?php
 
if (strpos($_SERVER['HTTP_HOST'], 'localhost') !== false) {
    defined('YII_DEBUG') or define('YII_DEBUG',true);
    defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
    define('YII_ENV', 'dev');
} else {
    defined('YII_DEBUG') or define('YII_DEBUG',false);
    defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',1);
    define('YII_ENV', 'production');
}
 
return array(
    'name'=>'My Web Application',
 
    'behaviors' => array(
        'fconfig' => array(
            'class' => 'ext.FlexibleConfig.FConfig',
            'currentConfig' =>  YII_ENV,
            'configs' => array(
                'dev' => array(),
                'production' => array(),
                'test' => array(),
            ),
        ),
    ),
 
    'params'=>array(
        'adminEmail'=>'ekaragodin@gmail.com',
    ),
);

Resources

Change Log

January 26, 2011

  • Added multiple inheritance

January 23, 2011

  • First version

Total 8 comments

#2621 report it
Gustavo at 2011/01/23 10:20pm
easy to complain about something ...

instead, do your part and help the extension get better

#2614 report it
karagodin at 2011/01/23 08:20am
example 2

I added a second example to show the use case without changing the index.php file.

#2613 report it
François Gannaz at 2011/01/23 07:55am
A better config system is needed in Yii...

Thanks to the author, it's a step in the good direction. A better config system is really needed in Yii, but IMHO this extension is still far from it.
Here are several cons:

  • The debug mode is not a part of the config file, so handling dev and prod instances is still a pain.
  • One needs to modify "index.php" to switch the config profile.
  • The inheriting system seems bad to me. A list of config files would be more easier and more powerful.
  • Some config files should be marked as "optionnal" whereas others are "required".
  • Ability to load files from subdirectories of the config file.

Having to modify "index.php" in almost each new instance of a standard Yii applications is a real PITA.

#2612 report it
karagodin at 2011/01/23 06:34am
dynamic define

Certainly, inheritance can be as much as long.

Not to change a file index.php, it is possible a array transferred in Yii::createWebApplication() too to take out in an external file or to define a current config dynamically:

'currentConfig' => strpos($_SERVER['HTTP_HOST'], 'localhost') !== false ? 'dev' : 'production',
#2611 report it
Say_Ten at 2011/01/23 05:36am
Definately be using this.

This has been the one thing that we've been hacking about with a lot but never having the time to write a proper solution. I'm assuming you can have parents with parents?

What we have been doing though is making use of an environment variables APPLICATION_ENV to control which config file we load. We followed the Zend structure for this so we could deploy projects in either framework, so it's either development, testing or production. Of course this can be passed into current config and you're covered if you need to overwrite the index file.

#2610 report it
Gustavo at 2011/01/23 04:49am
looks usefull

thanks for sharing!

#2608 report it
karagodin at 2011/01/23 04:09am
autoload *_local.php

If the current configuration of the "dev", then after loading the file "dev.php" automatically loaded "dev_local.php", if it exists.

The result is CMap::mergeArray(require('dev.php'), require('dev_local.php'));

#2607 report it
Pentium10 at 2011/01/23 03:53am
further clarification needed

I don't understand

In addition to the config folder is a file dev_local.php, which is automatically loaded after dev.php and overrides the necessary local settings.

Could you please elaborate?

Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: New BSD License
  • Developed by: karagodin
  • Category: Others
  • Votes: +4
  • Downloaded: 456 times
  • Created on: Jan 23, 2011
  • Last updated: Jan 25, 2011