1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soluto\Settings; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\Component; |
7
|
|
|
use yii\db\Connection; |
8
|
|
|
use yii\db\Query; |
9
|
|
|
use yii\base\Event; |
10
|
|
|
|
11
|
|
|
class Settings extends Component |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @event SettingsEvent an event that is triggered before execute command. |
15
|
|
|
*/ |
16
|
|
|
const EVENT_BEFORE_EXECUTE = 'beforeExecute'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string Name of the table where configurations will be stored |
20
|
|
|
*/ |
21
|
|
|
public $tableName = '{{%setting}}'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string Name of column where keys will be stored |
25
|
|
|
*/ |
26
|
|
|
public $keyColumnName = 'key'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string Name of column where values will be stored |
30
|
|
|
*/ |
31
|
|
|
public $valueColumnName = 'value'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return Connection the DB connection instance |
35
|
|
|
*/ |
36
|
2 |
|
protected function getDb() |
37
|
|
|
{ |
38
|
2 |
|
return Yii::$app->getDb(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Whether the configuration exists in the database |
43
|
|
|
* @param string $name configuration name |
44
|
|
|
* @param integer $tenantId The tenant id value |
|
|
|
|
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
2 |
|
protected function exists($name) |
48
|
|
|
{ |
49
|
2 |
|
$query = $this->createQuery($name); |
50
|
2 |
|
return $query->exists(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns configuration value from database |
55
|
|
|
* @param string $name configuration name |
56
|
|
|
* @return string value stored in database |
57
|
|
|
*/ |
58
|
1 |
|
public function get($name, $defaultValue = null) |
59
|
|
|
{ |
60
|
1 |
|
$value = null; |
|
|
|
|
61
|
1 |
|
$query = $this->createQuery($name); |
62
|
1 |
|
$row = $query->one($this->getDb()); |
63
|
|
|
|
64
|
1 |
|
$value = ($row) ? $row[$this->valueColumnName] : null; |
65
|
|
|
|
66
|
1 |
|
if (is_string($value) && trim($value) == '') { |
67
|
|
|
$value = null; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return ($value !== null) ? $value : $defaultValue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Store configuration value to database |
75
|
|
|
* @param string $name |
76
|
|
|
* @param mixed $value |
77
|
|
|
*/ |
78
|
2 |
|
public function set($name, $value) |
79
|
|
|
{ |
80
|
2 |
|
$db = $this->getDb(); |
81
|
2 |
|
$values = [$this->valueColumnName => $value]; |
82
|
2 |
|
$where = [$this->keyColumnName => $name]; |
83
|
|
|
|
84
|
2 |
|
$event = $this->beforeExecute(); |
85
|
2 |
|
if ($event->data) { |
86
|
1 |
|
$values = array_merge($event->data, $values); |
87
|
1 |
|
$where = array_merge($event->data, $where); |
88
|
1 |
|
} |
89
|
|
|
|
90
|
2 |
|
if ($this->exists($name)) { |
91
|
1 |
|
$db->createCommand() |
92
|
1 |
|
->update($this->tableName, $values, $where) |
93
|
1 |
|
->execute(); |
94
|
1 |
|
} else { |
95
|
2 |
|
$values = array_merge($values, $where); |
96
|
|
|
|
97
|
2 |
|
$db->createCommand() |
98
|
2 |
|
->insert($this->tableName, $values) |
99
|
2 |
|
->execute(); |
100
|
|
|
} |
101
|
2 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Retrieves all configurations stored in database |
105
|
|
|
* @param integer $tenantId |
|
|
|
|
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
1 |
|
public function all() |
109
|
|
|
{ |
110
|
1 |
|
$result = []; |
111
|
|
|
|
112
|
1 |
|
$query = $this->createQuery() |
113
|
1 |
|
->addSelect($this->keyColumnName); |
114
|
|
|
|
115
|
1 |
|
$rows = $query->all($this->getDb()); |
116
|
|
|
|
117
|
1 |
|
foreach ($rows as $row) { |
118
|
1 |
|
$result[$row[$this->keyColumnName]] = $row[$this->valueColumnName]; |
119
|
1 |
|
} |
120
|
|
|
|
121
|
1 |
|
return $result; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Store all configuration in database |
126
|
|
|
* @param array $names |
127
|
|
|
*/ |
128
|
1 |
|
public function save($names) |
129
|
|
|
{ |
130
|
1 |
|
foreach ($names as $key => $value) { |
131
|
1 |
|
$this->set($key, $value) ; |
132
|
1 |
|
} |
133
|
1 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Deletes specified configurations (or all if none specified) from the parameters table |
137
|
|
|
* @param array|string $names |
138
|
|
|
* @param integer $tenantId |
|
|
|
|
139
|
|
|
*/ |
140
|
1 |
|
public function remove($names = []) |
141
|
|
|
{ |
142
|
1 |
|
if (is_array($names)) { |
143
|
1 |
|
$where = ['IN', $this->keyColumnName, $names]; |
144
|
1 |
|
} else { |
145
|
1 |
|
$where = [$this->keyColumnName => $names]; |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
$event = $this->beforeExecute(); |
149
|
1 |
|
if ($event->data) { |
150
|
|
|
$where = array_merge($event->data, $where); |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
$this->getDb() |
154
|
1 |
|
->createCommand() |
155
|
1 |
|
->delete($this->tableName, $where) |
156
|
1 |
|
->execute(); |
157
|
1 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Creates query to find settings value |
161
|
|
|
* @param string $name |
162
|
|
|
* @return \yii\db\Query |
163
|
|
|
*/ |
164
|
2 |
|
protected function createQuery($name = null) |
165
|
|
|
{ |
166
|
2 |
|
$query = (new Query()) |
167
|
2 |
|
->select([$this->valueColumnName]) |
168
|
2 |
|
->from($this->tableName); |
169
|
|
|
|
170
|
2 |
|
$event = $this->beforeExecute(); |
171
|
|
|
|
172
|
2 |
|
if ($event->data) { |
173
|
1 |
|
$query->andWhere($event->data); |
174
|
1 |
|
} |
175
|
|
|
|
176
|
2 |
|
if ($name) { |
|
|
|
|
177
|
2 |
|
$query->andWhere([$this->keyColumnName => $name]); |
178
|
2 |
|
} |
179
|
|
|
|
180
|
2 |
|
return $query; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* This method is called at the before execute db command |
185
|
|
|
* @return yii\base\Event |
186
|
|
|
*/ |
187
|
2 |
|
protected function beforeExecute() |
188
|
|
|
{ |
189
|
2 |
|
$event = new Event(); |
190
|
2 |
|
$this->trigger(self::EVENT_BEFORE_EXECUTE, $event); |
191
|
2 |
|
return $event; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.