-
Notifications
You must be signed in to change notification settings - Fork 9
/
Config.php
429 lines (388 loc) · 12.9 KB
/
Config.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php
namespace luya;
use luya\helpers\ArrayHelper;
/**
* Configuration array Helper.
*
* The {{luya\Config}} allows you to create the configuration for different hosts and difference between web and console config.
*
* ```php
* $config = new Config('myapp', dirname(__DIR__), [
* 'siteTitle' => 'My LUYA Project',
* 'defaultRoute' => 'cms',
* // other application level configurations
* ]);
*
* // define global components which works either for console or web runtime
*
* $config->component('mail', [
* 'host' => 'xyz',
* 'from' => '[email protected]',
* ]);
*
* $config->component('db', [
* 'class' => 'yii\db\Connection',
* 'dsn' => 'mysql:host=localhost;dbname=prod_db',
* 'username' => 'foo',
* 'password' => 'bar',
* ]);
*
* // define components which are only for web or console runtime:
*
* $config->webComponent('request', [
* 'cookieValidationKey' => 'xyz',
* ]);
*
* // which is equals to, but the above is better to read and structure in the config file
*
* $config->component('request', [
* 'cookieValidationKey' => 'xyz',
* ])->webRuntime();
*
* // adding modules
*
* $config->module('admin', [
* 'class' => 'luya\admin\Module',
* 'secureLogin' => true,
* ]);
*
* $config->module('cms', 'luya\cms\frontend\Module'); // which is equals to $config->module('cms', ['class' => 'luya\cms\frontend\Module']);
*
* // export and generate the config for a given enviroment or environment independent.
*
* return $config->toArray(); // returns the config not taking care of enviroment variables like prod, env
*
* return $config->toArray([Config::ENV_PROD]);
* ```
*
* ## Runtime
*
* Each method returns an {{luya\ConfigDefinition}} object and can therefore be configured for different runtimes (console/web). The given example will add a console command
* only for console applications:
*
* ```php
* $config->application([
* 'controllerMap' => [
* 's3' => 'luya\aws\commands\S3Command',
* ]
* ])->consoleRuntime();
* ```
*
* ## Envs
*
* Switching between envs can be usefull if certain configurations should only apply on a certain environment. Therefore you can add `env()` behind componenets, applications and modules.
*
* ```php
* $config->component('db', [
* 'class' => 'yii\db\Connection',
* 'dsn' => 'mysql:host=localhost;dbname=local_db',
* 'username' => 'foo',
* 'password' => 'bar',
* ])->env(Config::ENV_LOCAL);
*
* $config->component('db', [
* 'class' => 'yii\db\Connection',
* 'dsn' => 'mysql:host=localhost;dbname=dev_db',
* 'username' => 'foo',
* 'password' => 'bar',
* ])->env(Config::ENV_DEV);
*
* $config->component('db', [
* 'class' => 'yii\db\Connection',
* 'dsn' => 'mysql:host=localhost;dbname=prod_db',
* 'username' => 'foo',
* 'password' => 'bar',
* ])->env(Config::ENV_PROD);
*
* return $config->toArray(Config::ENV_PROD); // would only return the prod env db component
* ```
*
* > When mergin varaibles, the later will always override the former. If arrays are involved the values will be added, not replaced!
* > Example: `'foo' => 'bar', 'values' => [1]` and `'foo' => 'baz', 'values' => [2]` will be merged to: `'foo' => 'baz', 'values' => [1,2]`.
*
* @author Basil Suter <[email protected]>
* @since 1.0.21
*/
class Config
{
public const ENV_ALL = 'all';
/**
* @var string Predefined constant for production
*/
public const ENV_PROD = 'prod';
/**
* @var string Predefined constant for preproduction
*/
public const ENV_PREP = 'prep';
/**
* @var string Predefined constant for development mode
*/
public const ENV_DEV = 'dev';
/**
* @var string Predefined constant for local development
*/
public const ENV_LOCAL = 'local';
/**
* @var string Predefined constant for ci enviroments
*/
public const ENV_CI = 'ci';
public const RUNTIME_ALL = 0;
public const RUNTIME_CONSOLE = 1;
public const RUNTIME_WEB = 2;
/**
* Constructor
*
* @param string $id
* @param string $basePath
* @param array $applicationConfig
*/
public function __construct($id, $basePath, array $applicationConfig = [])
{
$applicationConfig['id'] = $id;
$applicationConfig['basePath'] = $basePath;
$this->application($applicationConfig);
}
private $_env;
/**
* Assign the env to each component, module or application that defined inside the callback.
*
* Callback function has one parameter with the current {{luya\Config}} object.
*
* An example using env to wrap multiple configuration lines into a single environment:
*
* ```php
* $config->env(Config::ENV_LOCAL, function($config) {
*
* $config->callback(function() {
* define('YII_DEBUG', true);
* define('YII_ENV', 'local');
* });
*
* $config->component('db', [
* 'dsn' => 'mysql:host=luya_db;dbname=luya_kickstarter',
* 'username' => 'luya',
* 'password' => 'luya',
* 'enableSchemaCache' => false,
* ]);
*
* $config->module('debug', [
* 'class' => 'yii\debug\Module',
* 'allowedIPs' => ['*'],
* ]);
*
* $config->bootstrap(['debug']);
* });
* ```
*
* @param string $env The environment to assigne inside the callback.
* @param callable $callback function(\luya\Config $config)
* @return $this
*/
public function env($env, callable $callback)
{
$this->_env = $env;
try {
call_user_func($callback, $this);
} finally {
$this->_env = null;
}
return $this;
}
/**
* register application level config
*
* @param array $config The array to configure
* @return ConfigDefinition
*/
public function application(array $config)
{
return $this->addDefinition(new ConfigDefinition(ConfigDefinition::GROUP_APPLICATIONS, 'application', $config));
}
/**
* Register one or more bootstrap entries into the bootstrap section.
*
* @param array $config An array with bootstrap entries, its common to use the module name
* @return ConfigDefinition
*/
public function bootstrap(array $config)
{
return $this->addDefinition(new ConfigDefinition(ConfigDefinition::GROUP_BOOTSTRAPS, 'bootstrap', $config));
}
/**
* Register a module.
*
* @param string $id The module identifier.
* @param string|array $config The configuration for the given module. If a string is given this will be taken as `class` property.
* @return ConfigDefinition
*/
public function module($id, $config)
{
return $this->addDefinition(new ConfigDefinition(ConfigDefinition::GROUP_MODULES, $id, $config));
}
/**
* Run a callable functions for the defined env when toArray() is called.
*
* @param callable $fn The function to run, the first argument of the closure is the {{luya\Config}} object.
* @return ConfigDefinition
* @since 1.0.23
*/
public function callback(callable $fn)
{
return $this->addDefinition(new ConfigDefinition(ConfigDefinition::GROUP_CALLABLE, false, $fn));
}
/**
* Register a component
*
* @param string $id The id of the component
* @param string|array $config The configuration for the given module. If a string is given this will be taken as `class` property.
* @param string $runtime The runtime for the component: all, web or console
* @return ConfigDefinition
*/
public function component($id, $config, $runtime = self::RUNTIME_ALL)
{
return $this->addDefinition(new ConfigDefinition(ConfigDefinition::GROUP_COMPONENTS, $id, $config))->runtime($runtime);
}
/**
* Register a web runtime component.
*
* @param string $id The id of the component
* @param string|array $config The configuration for the given module. If a string is given this will be taken as `class` property.
* @return ConfigDefinition
*/
public function webComponent($id, $config)
{
return $this->component($id, $config, self::RUNTIME_WEB);
}
/**
* Register a console runtime component.
*
* @param string $id The id of the component
* @param string|array $config The configuration for the given module. If a string is given this will be taken as `class` property.
* @return ConfigDefinition
*/
public function consoleComponent($id, $config)
{
return $this->component($id, $config, self::RUNTIME_CONSOLE);
}
private $_definitions = [];
/**
* Add a definition into the definitions bag.
*
* @param ConfigDefinition $definition
* @return ConfigDefinition
*/
private function addDefinition(ConfigDefinition $definition)
{
if ($this->_env !== null) {
$definition->env($this->_env);
}
$this->_definitions[] = $definition;
return $definition;
}
private $_isCliRuntime;
/**
* Whether runtime is cli or not
*
* @return boolean
*/
public function isCliRuntime()
{
if ($this->_isCliRuntime === null) {
$this->_isCliRuntime = strtolower(php_sapi_name()) === 'cli';
}
return $this->_isCliRuntime;
}
/**
* Setter method for runtime.
*
* > This method is mainly used for unit testing.
*
* @param boolean $value
*/
public function setCliRuntime($value)
{
$this->_isCliRuntime = $value;
}
/**
* Export the given configuration as array for certain envs.
*
* @param array|string $envs A list of environments to export. if nothing is given all enviroments will be returned. A string will be threated as array with 1 entry.
* @return array The configuration array
*/
public function toArray($envs = [])
{
$config = [];
$envs = (array) $envs;
$envs = array_merge($envs, [self::ENV_ALL]);
foreach ($this->_definitions as $definition) { /** @var ConfigDefinition $definition */
// validate if current export env is in the list of envs
if (!$definition->validateEnvs($envs)) {
continue;
}
// validate runtime circumstances
if ($definition->validateRuntime(self::RUNTIME_ALL)) {
$this->appendConfig($config, $definition);
} elseif ($this->isCliRuntime() && $definition->validateRuntime(self::RUNTIME_CONSOLE)) {
$this->appendConfig($config, $definition);
} elseif (!$this->isCliRuntime() && $definition->validateRuntime(self::RUNTIME_WEB)) {
$this->appendConfig($config, $definition);
}
}
return $config;
}
/**
* Append a given definition int othe config
*
* @param array $config
* @param ConfigDefinition $definition
*/
private function appendConfig(&$config, ConfigDefinition $definition)
{
switch ($definition->getGroup()) {
case ConfigDefinition::GROUP_APPLICATIONS:
foreach ($definition->getConfig() as $k => $v) {
$config[$k] = $v;
}
break;
case ConfigDefinition::GROUP_COMPONENTS:
$this->handleKeyBaseMerge($config, $definition, 'components');
break;
case ConfigDefinition::GROUP_MODULES:
$this->handleKeyBaseMerge($config, $definition, 'modules');
break;
case ConfigDefinition::GROUP_BOOTSTRAPS:
if (!array_key_exists('bootstrap', $config)) {
$config['bootstrap'] = [];
}
foreach ($definition->getConfig() as $v) {
$config['bootstrap'][] = $v;
}
break;
case ConfigDefinition::GROUP_CALLABLE:
call_user_func($definition->getConfig(), $this);
break;
}
}
/**
* Add a array key based component definition.
*
* @param array $config
* @param ConfigDefinition $definition
* @param string $section
*/
private function handleKeyBaseMerge(&$config, ConfigDefinition $definition, $section)
{
// ass missing section key
if (!array_key_exists($section, $config)) {
$config[$section] = [];
}
// array key from definition in order to merge with existing values
$key = $definition->getKey();
// if key exists, merge otherwise create key
if (isset($config[$section][$key])) {
$config[$section][$key] = ArrayHelper::merge($config[$section][$key], $definition->getConfig());
} else {
$config[$section][$key] = $definition->getConfig();
}
}
}