1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace luya; |
4
|
|
|
|
5
|
|
|
use luya\helpers\ArrayHelper; |
6
|
|
|
|
7
|
|
|
use function Opis\Closure\serialize; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Configuration array Helper. |
11
|
|
|
* |
12
|
|
|
* The {{luya\Config}} allows you to create the configuration for different hosts and difference between web and console config. |
13
|
|
|
* |
14
|
|
|
* ```php |
15
|
|
|
* $config = new Config('myapp', dirname(__DIR__), [ |
16
|
|
|
* 'siteTitle' => 'My LUYA Project', |
17
|
|
|
* 'defaultRoute' => 'cms', |
18
|
|
|
* // other application level configurations |
19
|
|
|
* ]); |
20
|
|
|
* |
21
|
|
|
* // define global components which works either for console or web runtime |
22
|
|
|
* |
23
|
|
|
* $config->component('mail', [ |
24
|
|
|
* 'host' => 'xyz', |
25
|
|
|
* 'from' => '[email protected]', |
26
|
|
|
* ]); |
27
|
|
|
* |
28
|
|
|
* $config->component('db', [ |
29
|
|
|
* 'class' => 'yii\db\Connection', |
30
|
|
|
* 'dsn' => 'mysql:host=localhost;dbname=prod_db', |
31
|
|
|
* 'username' => 'foo', |
32
|
|
|
* 'password' => 'bar', |
33
|
|
|
* ])->env('martin'); |
34
|
|
|
* |
35
|
|
|
* |
36
|
|
|
* |
37
|
|
|
* // define components which are only for web or console runtime: |
38
|
|
|
* |
39
|
|
|
* $config->webComponent('request', [ |
40
|
|
|
* 'cookieValidationKey' => 'xyz', |
41
|
|
|
* ]); |
42
|
|
|
* |
43
|
|
|
* // which is equals to, but the above is better to read and structure in the config file |
44
|
|
|
* |
45
|
|
|
* $config->component('request', [ |
46
|
|
|
* 'cookieValidationKey' => 'xyz', |
47
|
|
|
* ])->webRuntime(); |
48
|
|
|
* |
49
|
|
|
* // adding modules |
50
|
|
|
* |
51
|
|
|
* $config->module('admin', [ |
52
|
|
|
* 'class' => 'luya\admin\Module', |
53
|
|
|
* 'secureLogin' => true, |
54
|
|
|
* ]); |
55
|
|
|
* |
56
|
|
|
* $config->module('cms', 'luya\cms\frontend\Module'); // which is equals to $config->module('cms', ['class' => 'luya\cms\frontend\Module']); |
57
|
|
|
* |
58
|
|
|
* // export and generate the config for a given enviroment or environment independent. |
59
|
|
|
* |
60
|
|
|
* return $config->toArray(); // returns the config not taking care of enviroment variables like prod, env |
61
|
|
|
* |
62
|
|
|
* return $config->toArray(Config::ENV_PROD); |
63
|
|
|
* ``` |
64
|
|
|
* |
65
|
|
|
* @author Basil Suter <[email protected]> |
66
|
|
|
* @since 1.0.10 |
67
|
|
|
*/ |
68
|
|
|
class Config |
69
|
|
|
{ |
70
|
|
|
const ENV_ALL = 'all'; |
71
|
|
|
|
72
|
|
|
const ENV_PROD = 'prod'; |
73
|
|
|
|
74
|
|
|
const ENV_PREP = 'prep'; |
75
|
|
|
|
76
|
|
|
const ENV_DEV = 'dev'; |
77
|
|
|
|
78
|
|
|
const ENV_LOCAL = 'local'; |
79
|
|
|
|
80
|
|
|
const RUNTIME_ALL = 0; |
81
|
|
|
|
82
|
|
|
const RUNTIME_CONSOLE = 1; |
83
|
|
|
|
84
|
|
|
const RUNTIME_WEB = 2; |
85
|
|
|
|
86
|
|
|
public function __construct($id, $basePath, array $applicationConfig = []) |
87
|
|
|
{ |
88
|
|
|
$applicationConfig['id'] = $id; |
89
|
|
|
$applicationConfig['basePath'] = $basePath; |
90
|
|
|
|
91
|
|
|
$this->application($applicationConfig); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Undocumented function |
96
|
|
|
* |
97
|
|
|
* @param [type] $config |
|
|
|
|
98
|
|
|
* @return ConfigDefinition |
99
|
|
|
*/ |
100
|
|
|
public function application($config) |
101
|
|
|
{ |
102
|
|
|
return $this->addDefinition(new ConfigDefinition(ConfigDefinition::GROUP_APPLICATIONS, md5(serialize($config)), $config)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Undocumented function |
107
|
|
|
* |
108
|
|
|
* @param [type] $id |
|
|
|
|
109
|
|
|
* @param [type] $config |
|
|
|
|
110
|
|
|
* @return ConfigDefinition |
111
|
|
|
*/ |
112
|
|
|
public function module($id, $config) |
113
|
|
|
{ |
114
|
|
|
return $this->addDefinition(new ConfigDefinition(ConfigDefinition::GROUP_MODULES, $id, $config)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Undocumented function |
119
|
|
|
* |
120
|
|
|
* @param [type] $id |
|
|
|
|
121
|
|
|
* @param [type] $config |
|
|
|
|
122
|
|
|
* @param [type] $runtime |
|
|
|
|
123
|
|
|
* @return ConfigDefinition |
124
|
|
|
*/ |
125
|
|
|
public function component($id, $config, $runtime = self::RUNTIME_ALL) |
126
|
|
|
{ |
127
|
|
|
return $this->addDefinition(new ConfigDefinition(ConfigDefinition::GROUP_COMPONENTS, $id, $config))->runtime($runtime); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Undocumented function |
132
|
|
|
* |
133
|
|
|
* @param [type] $id |
|
|
|
|
134
|
|
|
* @param [type] $config |
|
|
|
|
135
|
|
|
* @return ConfigDefinition |
136
|
|
|
*/ |
137
|
|
|
public function webComponent($id, $config) |
138
|
|
|
{ |
139
|
|
|
return $this->component($id, $config, self::RUNTIME_WEB); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Undocumented function |
144
|
|
|
* |
145
|
|
|
* @param [type] $id |
|
|
|
|
146
|
|
|
* @param [type] $config |
|
|
|
|
147
|
|
|
* @return ConfigDefinition |
148
|
|
|
*/ |
149
|
|
|
public function consoleComponent($id, $config) |
150
|
|
|
{ |
151
|
|
|
return $this->component($id, $config, self::RUNTIME_CONSOLE); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
private $_definitions = []; |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Undocumented function |
158
|
|
|
* |
159
|
|
|
* @param ConfigDefinition $definition |
160
|
|
|
* @return ConfigDefinition |
161
|
|
|
*/ |
162
|
|
|
private function addDefinition(ConfigDefinition $definition) |
163
|
|
|
{ |
164
|
|
|
$this->_definitions[] = $definition; |
165
|
|
|
|
166
|
|
|
return $definition; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
private $_isCliRuntime; |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Whether runtime is cli or not |
173
|
|
|
* |
174
|
|
|
* @return boolean |
175
|
|
|
*/ |
176
|
|
|
public function isCliRuntime() |
177
|
|
|
{ |
178
|
|
|
if ($this->_isCliRuntime === null) { |
179
|
|
|
$this->_isCliRuntime = strtolower(php_sapi_name()) === 'cli'; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this->_isCliRuntime; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function setCliRuntime($value) |
186
|
|
|
{ |
187
|
|
|
$this->_isCliRuntime = $value; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function toArray(array $envs = [self::ENV_ALL]) |
191
|
|
|
{ |
192
|
|
|
$config = []; |
193
|
|
|
|
194
|
|
|
foreach ($this->_definitions as $defintion) { |
195
|
|
|
/** @var ConfigDefinition $definition */ |
196
|
|
|
|
197
|
|
|
if (!$definition->validateEnvs($envs)) { |
|
|
|
|
198
|
|
|
continue; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
if ($definition->validateRuntime(self::RUNTIME_ALL)) { |
|
|
|
|
202
|
|
|
$this->appendConfig($config, $definition); |
|
|
|
|
203
|
|
|
} elseif ($this->isCliRuntime() && $definition->validateRuntime(self::RUNTIME_CONSOLE)) { |
|
|
|
|
204
|
|
|
$this->appendConfig($config, $definition); |
|
|
|
|
205
|
|
|
} elseif (!$this->isCliRuntime() && $definition->validateRuntime(self::RUNTIME_WEB)) { |
|
|
|
|
206
|
|
|
$this->appendConfig($config, $definition); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $config; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
private function appendConfig(&$config, ConfigDefinition $definition) |
214
|
|
|
{ |
215
|
|
|
switch ($definition->getGroup()) { |
216
|
|
|
case ConfigDefinition::GROUP_APPLICATIONS: |
217
|
|
|
foreach ($definition->getConfig() as $k => $v) { |
218
|
|
|
$config[$k] = $v; |
219
|
|
|
} |
220
|
|
|
break; |
221
|
|
View Code Duplication |
case ConfigDefinition::GROUP_COMPONENTS: |
222
|
|
|
if (!array_key_exists('components', $config)) { |
223
|
|
|
$config['components'] = []; |
224
|
|
|
} |
225
|
|
|
$config['components'][$definition->getKey()] = $definition->getConfig(); |
226
|
|
|
break; |
227
|
|
View Code Duplication |
case ConfigDefinition::GROUP_MODULES: |
228
|
|
|
if (!array_key_exists('modules', $config)) { |
229
|
|
|
$config['modules'] = []; |
230
|
|
|
} |
231
|
|
|
$config['modules'][$definition->getKey()] = $definition->getConfig(); |
232
|
|
|
break; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
} |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.