-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ConfigBuilder class uploaded, README created
- Loading branch information
Showing
3 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, '/')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\\": ""} | ||
} | ||
} |