Skip to content

Instantly share code, notes, and snippets.

@crisu83
Last active December 13, 2015 23:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crisu83/4989508 to your computer and use it in GitHub Desktop.
Save crisu83/4989508 to your computer and use it in GitHub Desktop.
Helper for building Yii application configurations.
<?php
/**
* ConfigBuilder class file.
* @author Christoffer Niska <christoffer.niska@gmail.com>
* @copyright Copyright &copy; Christoffer Niska 2013-
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
/**
* Helper for building application configurations.
*/
class ConfigBuilder
{
/**
* Builds a configuration from the given array.
* @param array $array the configuration parts.
* @return array the configuration.
*/
public static function build($array)
{
$result = array();
if (!is_array($array))
$array = array($array);
foreach ($array as $config)
{
if (is_string($config))
{
if (!file_exists($config))
continue;
$config = require($config);
}
$result = CMap::mergeArray($result, $config);
}
return $result;
}
}
@crisu83
Copy link
Author

crisu83 commented Feb 19, 2013

Example usage in an entry script:

$yii = __DIR__ . '/../app/vendor/yiisoft/yii/framework/yii.php';
$builder = __DIR__ . '/../app/helpers/ConfigBuilder.php';

require_once($yii);
require_once($builder);

$config = ConfigBuilder::build(array(
    __DIR__ . '/../app/config/common.php',
    __DIR__ . '/../app/config/web.php',
    __DIR__ . '/../app/config/dev.php',
    __DIR__ . '/../app/config/local.php',
));

Yii::createWebApplication($config)->run();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment