1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mdm\admin\components; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\caching\Cache; |
7
|
|
|
use yii\db\Connection; |
8
|
|
|
use yii\di\Instance; |
9
|
|
|
use yii\helpers\ArrayHelper; |
10
|
|
|
use yii\rbac\ManagerInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Configs |
14
|
|
|
* Used to configure some values. To set config you can use [[\yii\base\Application::$params]] |
15
|
|
|
* |
16
|
|
|
* ``` |
17
|
|
|
* return [ |
18
|
|
|
* |
19
|
|
|
* 'mdm.admin.configs' => [ |
20
|
|
|
* 'db' => 'customDb', |
21
|
|
|
* 'menuTable' => '{{%admin_menu}}', |
22
|
|
|
* 'cache' => [ |
23
|
|
|
* 'class' => 'yii\caching\DbCache', |
24
|
|
|
* 'db' => ['dsn' => 'sqlite:@runtime/admin-cache.db'], |
25
|
|
|
* ], |
26
|
|
|
* ] |
27
|
|
|
* ]; |
28
|
|
|
* ``` |
29
|
|
|
* |
30
|
|
|
* or use [[\Yii::$container]] |
31
|
|
|
* |
32
|
|
|
* ``` |
33
|
|
|
* Yii::$container->set('mdm\admin\components\Configs',[ |
34
|
|
|
* 'db' => 'customDb', |
35
|
|
|
* 'menuTable' => 'admin_menu', |
36
|
|
|
* ]); |
37
|
|
|
* ``` |
38
|
|
|
* |
39
|
|
|
* @author Misbahul D Munir <[email protected]> |
40
|
|
|
* @since 1.0 |
41
|
|
|
*/ |
42
|
|
|
|
43
|
|
|
class Configs extends \mdm\admin\BaseObject |
44
|
|
|
{ |
45
|
|
|
const CACHE_TAG = 'mdm.admin'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var ManagerInterface . |
49
|
|
|
*/ |
50
|
|
|
public $authManager = 'authManager'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Connection Database connection. |
54
|
|
|
*/ |
55
|
|
|
public $db = 'db'; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var Connection Database connection. |
59
|
|
|
*/ |
60
|
|
|
public $userDb = 'db'; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var Cache Cache component. |
64
|
|
|
*/ |
65
|
|
|
public $cache = 'cache'; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var integer Cache duration. Default to a hour. |
69
|
|
|
*/ |
70
|
|
|
public $cacheDuration = 3600; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string Menu table name. |
74
|
|
|
*/ |
75
|
|
|
public $menuTable = '{{%menu}}'; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var string Menu table name. |
79
|
|
|
*/ |
80
|
|
|
public $userTable = '{{%user}}'; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var integer Default status user signup. 10 mean active. |
84
|
|
|
*/ |
85
|
|
|
public $defaultUserStatus = 10; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var integer Number of user role. |
89
|
|
|
*/ |
90
|
|
|
public $userRolePageSize = 100; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var boolean If true then AccessControl only check if route are registered. |
94
|
|
|
*/ |
95
|
|
|
public $onlyRegisteredRoute = false; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var boolean If false then AccessControl will check without Rule. |
99
|
|
|
*/ |
100
|
|
|
public $strict = true; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @var array |
104
|
|
|
*/ |
105
|
|
|
public $options; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @var array|false Used for multiple application |
109
|
|
|
* ```php |
110
|
|
|
* [ |
111
|
|
|
* 'frontend' => [ |
112
|
|
|
* '@common/config/main.php', |
113
|
|
|
* '@common/config/main-local.php', |
114
|
|
|
* '@frontend/config/main.php', |
115
|
|
|
* '@frontend/config/main-local.php', |
116
|
|
|
* ], |
117
|
|
|
* 'backend' => [ |
118
|
|
|
* '@common/config/main.php', |
119
|
|
|
* '@common/config/main-local.php', |
120
|
|
|
* '@backend/config/main.php', |
121
|
|
|
* '@backend/config/main-local.php', |
122
|
|
|
* ], |
123
|
|
|
* ] |
124
|
|
|
* ``` * |
125
|
|
|
*/ |
126
|
|
|
public $advanced; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @var self Instance of self |
130
|
|
|
*/ |
131
|
|
|
private static $_instance; |
132
|
|
|
private static $_classes = [ |
133
|
|
|
'db' => 'yii\db\Connection', |
134
|
|
|
'userDb' => 'yii\db\Connection', |
135
|
|
|
'cache' => 'yii\caching\Cache', |
136
|
|
|
'authManager' => 'yii\rbac\ManagerInterface', |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @inheritdoc |
141
|
|
|
*/ |
142
|
|
|
public function init() |
143
|
|
|
{ |
144
|
|
|
foreach (self::$_classes as $key => $class) { |
145
|
|
|
try { |
146
|
|
|
$this->{$key} = empty($this->{$key}) ? null : Instance::ensure($this->{$key}, $class); |
147
|
|
|
} catch (\Exception $exc) { |
148
|
|
|
$this->{$key} = null; |
149
|
|
|
Yii::error($exc->getMessage()); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Create instance of self |
156
|
|
|
* @return static |
157
|
|
|
*/ |
158
|
|
|
public static function instance() |
159
|
|
|
{ |
160
|
|
|
if (self::$_instance === null) { |
161
|
|
|
$type = ArrayHelper::getValue(Yii::$app->params, 'mdm.admin.configs', []); |
162
|
|
|
if (is_array($type) && !isset($type['class'])) { |
163
|
|
|
$type['class'] = static::className(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return self::$_instance = Yii::createObject($type); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return self::$_instance; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public static function __callStatic($name, $arguments) |
173
|
|
|
{ |
174
|
|
|
$instance = static::instance(); |
175
|
|
|
if ($instance->hasProperty($name)) { |
176
|
|
|
return $instance->$name; |
177
|
|
|
} else { |
178
|
|
|
if (count($arguments)) { |
179
|
|
|
$instance->options[$name] = reset($arguments); |
180
|
|
|
} else { |
181
|
|
|
return array_key_exists($name, $instance->options) ? $instance->options[$name] : null; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return Connection |
188
|
|
|
*/ |
189
|
|
|
public static function db() |
190
|
|
|
{ |
191
|
|
|
return static::instance()->db; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return Connection |
196
|
|
|
*/ |
197
|
|
|
public static function userDb() |
198
|
|
|
{ |
199
|
|
|
return static::instance()->userDb; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return Cache |
204
|
|
|
*/ |
205
|
|
|
public static function cache() |
206
|
|
|
{ |
207
|
|
|
return static::instance()->cache; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return ManagerInterface |
212
|
|
|
*/ |
213
|
|
|
public static function authManager() |
214
|
|
|
{ |
215
|
|
|
return static::instance()->authManager; |
216
|
|
|
} |
217
|
|
|
/** |
218
|
|
|
* @return integer |
219
|
|
|
*/ |
220
|
|
|
public static function cacheDuration() |
221
|
|
|
{ |
222
|
|
|
return static::instance()->cacheDuration; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
|
|
public static function menuTable() |
229
|
|
|
{ |
230
|
|
|
return static::instance()->menuTable; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
public static function userTable() |
237
|
|
|
{ |
238
|
|
|
return static::instance()->userTable; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
|
|
public static function defaultUserStatus() |
245
|
|
|
{ |
246
|
|
|
return static::instance()->defaultUserStatus; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return boolean |
251
|
|
|
*/ |
252
|
|
|
public static function onlyRegisteredRoute() |
253
|
|
|
{ |
254
|
|
|
return static::instance()->onlyRegisteredRoute; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return boolean |
259
|
|
|
*/ |
260
|
|
|
public static function strict() |
261
|
|
|
{ |
262
|
|
|
return static::instance()->strict; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return int |
267
|
|
|
*/ |
268
|
|
|
public static function userRolePageSize() |
269
|
|
|
{ |
270
|
|
|
return static::instance()->userRolePageSize; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|