-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConfigBuilder.php
109 lines (97 loc) · 2.23 KB
/
ConfigBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace yii\configbuilder;
use Yii;
use \yii\base\BaseObject;
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 BaseObject
{
/**
* 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, '/'));
}
}