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 = '{{%settings}}'; |
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 = new Event(); |
85
|
2 |
|
$this->trigger(self::EVENT_BEFORE_EXECUTE, $event); |
86
|
|
|
|
87
|
2 |
|
if ($event->data) { |
88
|
1 |
|
$values = array_merge($event->data, $values); |
89
|
1 |
|
$where = array_merge($event->data, $where); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
if ($this->exists($name)) { |
93
|
1 |
|
$db->createCommand() |
94
|
1 |
|
->update($this->tableName, $values, $where) |
95
|
1 |
|
->execute(); |
96
|
|
|
} else { |
97
|
2 |
|
$values = array_merge($values, $where); |
98
|
|
|
|
99
|
2 |
|
$db->createCommand() |
100
|
2 |
|
->insert($this->tableName, $values) |
101
|
2 |
|
->execute(); |
102
|
|
|
} |
103
|
2 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Retrieves all configurations stored in database |
107
|
|
|
* @param integer $tenantId |
|
|
|
|
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
1 |
|
public function all() |
111
|
|
|
{ |
112
|
1 |
|
$result = []; |
113
|
|
|
|
114
|
1 |
|
$query = $this->createQuery() |
115
|
1 |
|
->addSelect($this->keyColumnName); |
116
|
|
|
|
117
|
1 |
|
$rows = $query->all($this->getDb()); |
118
|
|
|
|
119
|
1 |
|
foreach ($rows as $row) { |
120
|
1 |
|
$result[$row[$this->keyColumnName]] = $row[$this->valueColumnName]; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
return $result; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Store all configuration in database |
128
|
|
|
* @param array $names |
129
|
|
|
*/ |
130
|
1 |
|
public function save($names) |
131
|
|
|
{ |
132
|
1 |
|
foreach ($names as $key => $value) { |
133
|
1 |
|
$this->set($key, $value) ; |
134
|
|
|
} |
135
|
1 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Deletes specified configurations (or all if none specified) from the parameters table |
139
|
|
|
* @param array|string $names |
140
|
|
|
* @param integer $tenantId |
|
|
|
|
141
|
|
|
*/ |
142
|
1 |
|
public function delete($names = []) |
143
|
|
|
{ |
144
|
1 |
|
if (is_array($names)) { |
145
|
1 |
|
$where = ['IN', $this->keyColumnName, $names]; |
146
|
|
|
} else { |
147
|
1 |
|
$where = [$this->keyColumnName => $names]; |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
$event = new Event(); |
151
|
1 |
|
$this->trigger(self::EVENT_BEFORE_EXECUTE, $event); |
152
|
|
|
|
153
|
1 |
|
if ($event->data) { |
154
|
|
|
$where = array_merge($event->data, $where); |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
$this->getDb() |
158
|
1 |
|
->createCommand() |
159
|
1 |
|
->delete($this->tableName, $where) |
160
|
1 |
|
->execute(); |
161
|
1 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Creates query to find settings value |
165
|
|
|
* @param string $name |
166
|
|
|
* @return \yii\db\Query |
167
|
|
|
*/ |
168
|
2 |
|
protected function createQuery($name = null) |
169
|
|
|
{ |
170
|
2 |
|
$query = (new Query()) |
171
|
2 |
|
->select([$this->valueColumnName]) |
172
|
2 |
|
->from($this->tableName); |
173
|
|
|
|
174
|
2 |
|
$event = new Event(); |
175
|
2 |
|
$this->trigger(self::EVENT_BEFORE_EXECUTE, $event); |
176
|
|
|
|
177
|
2 |
|
if ($event->data) { |
178
|
1 |
|
$query->andWhere($event->data); |
179
|
|
|
} |
180
|
|
|
|
181
|
2 |
|
if ($name) { |
|
|
|
|
182
|
2 |
|
$query->andWhere([$this->keyColumnName => $name]); |
183
|
|
|
} |
184
|
|
|
|
185
|
2 |
|
return $query; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
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.