Skip to content

Commit

Permalink
ConfigBuilder class uploaded, README created
Browse files Browse the repository at this point in the history
  • Loading branch information
moicirk committed Feb 12, 2015
1 parent 6515714 commit 081ad19
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 0 deletions.
109 changes: 109 additions & 0 deletions ConfigBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace yii\configbuilder;

use Yii;
use \yii\base\Object;
use \yii\helpers\ArrayHelper;

/**
* Class ConfigBuilder combines different configuration
* files by scheme:
*
* base-common => base-{{web|console}} => {{env}}-common => {{env}}-{{web|console}}
* @see app\config file structure
* @author Dmitri Klimenko <[email protected]>
*/
class ConfigBuilder extends Object
{
/**
* Path alias to config folder
* @var string
*/
public $configPath = '@root/config';

/**
* Environment flag (constant)
* @var string
*/
public $environment = YII_ENV;

/**
* @inheritdoc
*/
public function init()
{
$this->configPath = Yii::getAlias($this->configPath);
}

/**
* Returns combined configuration for
* Yii2 web application
* @return array
*/
public function getWebConfig()
{
return ArrayHelper::merge(
$this->getBaseConfig(),
$this->getEnvConfig()
);
}

/**
* Returns combined configuration for
* Yii2 console application
* @return array
*/
public function getConsoleConfig()
{
return ArrayHelper::merge(
$this->getBaseConfig(true),
$this->getEnvConfig(true)
);
}

/**
* Returns combined common and web|console
* configuration set from base directory
* @param boolean $isConsole
* @return array
*/
protected function getBaseConfig($isConsole = false)
{
$primaryConfig = 'base/common.php';
$secondaryConfig = 'base/' . ($isConsole ? 'console' : 'web') . '.php';

return ArrayHelper::merge(
$this->loadConfigFile($primaryConfig),
$this->loadConfigFile($secondaryConfig)
);
}

/**
* Returns combined common and web|console
* configuration set from environment directory
* @param boolean $isConsole
* @return array
*/
protected function getEnvConfig($isConsole = false)
{
$primaryConfig = $this->environment . '/common.php';
$secondaryConfig = $this->environment . '/' . ($isConsole ? 'console' : 'web') . '.php';

return ArrayHelper::merge(
$this->loadConfigFile($primaryConfig),
$this->loadConfigFile($secondaryConfig)
);
}

/**
* Reads configuration file content
*
* @param string $filePath
* @return mixed
*/
protected function loadConfigFile($filePath)
{
return require($this->configPath . '/' . ltrim($filePath, '/'));
}
}
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# yii-configbuilder

Config builder class proposes a special configuration files structure to make it easier to configure Your Yii2 application for different environments.

## Installation

## Usage
Prepare config folder files structure to look like this:
```
- base
--- common
--- web
--- console
- prod
--- common
--- web
--- console
... any other environments
```

The workflow of ConfigBuilder is following:
- takes common file from base directory
- takes (web|console) file from base directory (depends on web or cli execution)
- takes YII_ENV constant value to know what environment is used
- takes common file from (prod|dev|test) directory
- takes (web|console) file from env directory

## Initialization
To initialize ConfigBuilder change your application entry points like that:
```
// Before
$config = require(__DIR__ . '/../config/web.php');
(new yii\web\Application($config))->run();
// After
require($rootPath . '/config/ConfigBuilder.php');
Yii::setAlias('@root', $rootPath); //root alias is required for builder to work
$configBuilder = new \app\config\ConfigBuilder();
(new yii\web\Application($configBuilder->getWebConfig()))->run();
```
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "daprime-com/yii-configbuilder",
"description": "Configuration builder extension for applications based on Yii2",
"keywords": ["yii2", "daprime"],
"type": "yii2-extension",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Dmitri Klimenko",
"email": "[email protected]"
},
{
"name": "Igor Murujev",
"email": "[email protected]"
}
],
"require": {
"yiisoft/yii2": "*"
},
"autoload": {
"psr-4": { "yii\\configbuilder\\": ""}
}
}

0 comments on commit 081ad19

Please sign in to comment.