-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bootstrap.php
50 lines (38 loc) · 1.1 KB
/
Bootstrap.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
<?php
/**
* @link http://www.matacms.com/
* @copyright Copyright (c) 2015 Qi Interactive Limited
* @license http://www.matacms.com/license/
*/
namespace matacms\settings;
use yii\base\BootstrapInterface;
use yii\base\Event;
use yii\db\BaseActiveRecord;
use mata\keyvalue\models\KeyValue;
use matacms\settings\models\Setting;
class Bootstrap implements BootstrapInterface {
public function bootstrap($app) {
if ($this->canRun($app) == false)
return;
Event::on(KeyValue::className(), KeyValue::EVENT_KEY_NOT_FOUND, function($event) {
$this->addKeyToSettings($event->message);
});
}
private function addKeyToSettings($key) {
$setting = Setting::findByKey($key);
if ($setting == null)
$this->addNewSetting($key);
}
private function addNewSetting($key) {
$setting = new Setting();
$setting->attributes = [
"Key" => $key,
"FormInputField" => Setting::DEFAULT_FORM_TYPE
];
if ($setting->save() == false)
throw new \yii\web\ServerErrorHttpException($setting->getTopError());
}
private function canRun($app) {
return is_a($app, "yii\console\Application") == false;
}
}