-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsComponent.php
161 lines (139 loc) · 4.38 KB
/
SettingsComponent.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
<?php
namespace olessavluk\settings;
use Yii;
use yii\db\Query;
use yii\db\Connection;
use yii\caching\Cache;
use yii\base\Component;
use yii\web\Application;
class SettingsComponent extends Component
{
public $tableName = '{{%settings}}';
public $cacheTime = 0; //never expire
public $defaults = [];
/** @var Connection */
protected $db;
public $dbName = 'db';
/** @var Cache */
protected $cache;
public $cacheName = 'cache';
protected $items = [];
protected $toSave = [];
protected $toRemove = [];
public function init()
{
parent::init();
$this->db = $this->db ? $this->db : Yii::$app->get($this->dbName);
$this->cache = $this->cache ? $this->cache : Yii::$app->get($this->cacheName);;
$this->load();
$this->fillDefaults();
Yii::$app->on(Application::EVENT_AFTER_REQUEST, [$this, 'commit']);
}
/**
* Get settings entry
*
* @param $category
* @param string $key
* @param null $default
* @return mixed
*/
public function get($category, $key, $default = null)
{
return isset($this->items[$category][$key]) ? $this->items[$category][$key] : $default;
}
/**
* Set settings entry
*
* @param string $category
* @param string $key
* @param string $value
* @param bool|true $permanent
*/
public function set($category, $key, $value, $permanent = true)
{
$this->items[$category][$key] = $value;
if ($permanent) {
$this->toSave[$category][$key] = $value;
unset($this->toRemove[$category][$key]);
}
}
/**
* Delete settings entry
*
* @param $category
* @param $key
* @param bool|true $permanent
*/
public function delete($category, $key, $permanent = true)
{
unset($this->items[$category[$key]]);
if ($permanent) {
$this->toRemove[$category][$key] = true;
unset($this->toSave[$category][$key]);
}
}
protected function load()
{
$this->items = $this->cache->get('settings');
if (!$this->items) {
$this->items = [];
$rows = (new Query())
->from($this->tableName)
->all($this->db);
foreach ($rows as $row) {
$this->items[$row['category']][$row['key']] = @unserialize($row['value']);
}
$this->cache->set('settings', $this->items, $this->cacheTime);
}
}
protected function fillDefaults() {
foreach ($this->defaults as $category => $keyvalue) {
foreach ($keyvalue as $key => $value) {
if (!isset($this->items[$category][$key])) {
$this->items[$category][$key] = $value;
}
}
}
}
protected function commit() {
/* @fixme: implement batch operations */
foreach ($this->toRemove as $category => $keyvalue) {
foreach ($keyvalue as $key => $value) {
$this
->db
->createCommand()
->delete($this->tableName, [
'category' => $category,
'key' => $key
])->execute();
}
}
foreach ($this->toSave as $category => $keyvalue) {
foreach ($keyvalue as $key => $value) {
$obj = (new Query())
->from($this->tableName)
->where(['category' => $category, 'key' => $key])
->one($this->db);
$command = $this->db->createCommand();
if ($obj) {
$command = $command->update($this->tableName,
['value' => @serialize($value)],
['category' => $category, 'key' => $key]
);
} else {
$command = $command->insert($this->tableName, [
'category' => $category,
'key' => $key,
'value' => @serialize($value)
]);
}
$command->execute();
}
}
if (count($this->toRemove) + count($this->toSave) > 0) {
$this->cache->delete('settings');
}
$this->toRemove = [];
$this->toSave = [];
}
}