forked from yii2tech/config
/
Manager.php
371 lines (349 loc) · 10.9 KB
/
Manager.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
<?php
/**
* @link https://github.com/yii2tech
* @copyright Copyright (c) 2015 Yii2tech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace yii2tech\config;
use Yii;
use yii\base\BootstrapInterface;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\caching\Cache;
use yii\di\Instance;
use yii\helpers\ArrayHelper;
/**
* Manager allows management of the dynamic application configuration parameters.
* Configuration parameters are set up via [[items]].
* Parameters can be saved inside the persistent storage determined by [[storage]].
*
* Manager implements [[BootstrapInterface]], so if you want to apply it to application
* you should place it at 'bootstrap' configuration section.
*
* Application configuration example:
*
* ```php
* [
* 'bootstrap' => [
* 'configManager',
* // ...
* ],
* 'components' => [
* 'configManager' => [
* 'class' => 'yii2tech\config\Manager',
* 'items' => [
* 'appName' => [
* 'path' => 'name',
* 'label' => 'Application Name',
* 'rules' => [
* ['required']
* ],
* ],
* 'nullDisplay' => [
* 'path' => 'components.formatter.nullDisplay',
* 'label' => 'HTML representing not set value',
* 'rules' => [
* ['required']
* ],
* ],
* ],
* ],
* ...
* ],
* ];
* ```
*
* Each configuration item is a model and so can be used to compose web form.
*
* Configuration apply example:
*
* ```php
* $configManager = Yii::$app->get('configManager');
* $configManager->configure(Yii::$app);
* ```
*
* @see Item
* @see Storage
*
* @property array[]|Item[]|string $items public alias of {@link _items}.
* @property Storage|array $storage public alias of {@link _storage}.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 1.0
*/
class Manager extends Component implements BootstrapInterface
{
/**
* @var array[]|Item[]|string config items in format: id => configuration.
* This filed can be setup as PHP file name, which returns the array of items.
*/
private $_items = [];
/**
* @var Storage|array config storage.
* It should be [[Storage]] instance or its array configuration.
*/
private $_storage = [
'class' => 'yii2tech\config\StorageDb'
];
/**
* @var Cache|array|string id of the cache object or the application component ID of the DB connection.
* After the Manager object is created, if you want to change this property, you should only assign it
* with a Cache object.
*/
public $cache = 'cache';
/**
* @var string id, which will be used to stored composed application configuration
* in the cache.
*/
public $cacheId = __CLASS__;
/**
* @var integer duration of cache for models in seconds.
* '0' means never expire.
* Set this parameter to a negative integer to aviod caching.
*/
public $cacheDuration = 0;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->cache = Instance::ensure($this->cache, Cache::className());
}
/**
* @inheritdoc
*/
public function bootstrap($app)
{
try {
$this->configure($app);
} catch (\Exception $exception) {
// fetching config from storage like database may fail at initial point, like before DB migrations applied
Yii::warning($exception->getMessage(), __METHOD__);
}
}
/**
* @param Storage|array|string $storage storage instance or configuration.
*/
public function setStorage($storage)
{
$this->_storage = $storage;
}
/**
* @return Storage storage instance.
*/
public function getStorage()
{
if (!is_object($this->_storage)) {
$this->_storage = Instance::ensure($this->_storage, Storage::className());
}
return $this->_storage;
}
/**
* @param array|string $items items list or configuration file name.
*/
public function setItems($items)
{
$this->_items = $items;
}
/**
* @return Item[] config items list.
*/
public function getItems()
{
$this->normalizeItems();
$items = [];
foreach ($this->_items as $id => $item) {
$items[] = $this->getItem($id);
}
return $items;
}
/**
* @param mixed $id item id
* @return Item config item instance.
* @throws InvalidParamException on failure.
*/
public function getItem($id)
{
$this->normalizeItems();
if (!array_key_exists($id, $this->_items)) {
throw new InvalidParamException("Unknown config item '{$id}'.");
}
if (!is_object($this->_items[$id])) {
$this->_items[$id] = $this->createItem($id, $this->_items[$id]);
}
return $this->_items[$id];
}
/**
* Creates config item by given configuration.
* @param mixed $id item id.
* @param array $config item configuration.
* @return Item config item instance
*/
protected function createItem($id, array $config)
{
if (empty($config['class'])) {
$config['class'] = Item::className();
}
$config['id'] = $id;
return Yii::createObject($config);
}
/**
* Normalizes [[items]] value, ensuring it is array.
* @throws InvalidConfigException on failure
*/
protected function normalizeItems()
{
if (!is_array($this->_items)) {
if (is_string($this->_items)) {
$fileName = Yii::getAlias($this->_items);
if (file_exists($fileName)) {
$this->_items = require($fileName);
if (!is_array($this->_items)) {
throw new InvalidConfigException('File "' . $fileName . '" should return an array.');
}
} else {
throw new InvalidConfigException('File "' . $this->_items . '" does not exist.');
}
} else {
throw new InvalidConfigException('"' . get_class($this) . '::items" should be array or file name containing it.');
}
}
}
/**
* @param array $itemValues config item values.
* @return Manager self reference.
*/
public function setItemValues(array $itemValues)
{
foreach ($itemValues as $id => $value) {
$item = $this->getItem($id);
$item->value = $value;
}
return $this;
}
/**
* @return array config item values
*/
public function getItemValues()
{
$itemValues = [];
foreach ($this->getItems() as $item) {
$itemValues[$item->id] = $item->value;
}
return $itemValues;
}
/**
* Composes application configuration array from config items.
* @return array application configuration.
*/
public function composeConfig()
{
$itemConfigs = [];
foreach ($this->getItems() as $item) {
$itemConfigs[] = $item->composeConfig();
}
return call_user_func_array(['yii\helpers\ArrayHelper', 'merge'], $itemConfigs);
}
/**
* Saves the current config item values into the persistent storage.
* @return boolean success.
*/
public function saveValues()
{
$result = $this->getStorage()->save($this->getItemValues());
if ($result) {
$this->cache->delete($this->cacheId);
}
return $result;
}
/**
* Restores config item values from the persistent storage.
* @return Manager self reference.
*/
public function restoreValues()
{
return $this->setItemValues($this->getStorage()->get());
}
/**
* Clears config item values saved in the persistent storage.
* @return boolean success.
*/
public function clearValues()
{
$result = $this->getStorage()->clear();
if ($result) {
$this->cache->delete($this->cacheId);
}
return $result;
}
/**
* Composes the application configuration using config item values
* from the persistent storage.
* This method caches its result for the better performance.
* @return array application configuration.
*/
public function fetchConfig()
{
$config = $this->cache->get($this->cacheId);
if ($config === false) {
$this->restoreValues();
$config = $this->composeConfig();
$this->cache->set($this->cacheId, $config, $this->cacheDuration);
}
return $config;
}
/**
* Performs the validation for all config item models at once.
* @return boolean whether the validation is successful without any error.
*/
public function validate()
{
$result = true;
foreach ($this->getItems() as $item) {
$isItemValid = $item->validate();
$result = $result && $isItemValid;
}
return $result;
}
/**
* Configures given module with provided configuration array.
* @param \yii\base\Module $module module to be configured.
* @param array $config configuration array.
*/
public function configure($module, $config = null)
{
if ($config === null) {
$config = $this->fetchConfig();
}
foreach ($config as $key => $value) {
switch ($key) {
case 'components':
$components = array_merge($module->getComponents(true), $module->getComponents(false));
$components = ArrayHelper::merge($components, $value);
$module->setComponents($components);
break;
case 'modules':
$nestedModules = $module->getModules(false);
foreach ($nestedModules as $id => $nestedModule) {
if (!isset($value[$id])) {
continue;
}
if (is_object($nestedModule)) {
$this->configure($nestedModule, $value[$id]);
} else {
$nestedModules[$id] = ArrayHelper::merge($nestedModule, $value[$id]);
}
}
$module->setModules($nestedModules);
break;
case 'params':
$module->params = ArrayHelper::merge($module->params, $value);
break;
default:
$module->$key = $value;
}
}
}
}