Configs::userTable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace toir427\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('toir427\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 \toir427\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 boolean If true then AccessControl only check if route are registered.
89
     */
90
    public $onlyRegisteredRoute = false;
91
92
    /**
93
     * @var boolean If false then AccessControl will check without Rule.
94
     */
95
    public $strict = true;
96
97
    /**
98
     * @var array
99
     */
100
    public $options;
101
102
    /**
103
     * @var array|false Used for multiple application
104
     * ```php
105
     * [
106
     *     'frontend' => [
107
     *         '@common/config/main.php',
108
     *         '@common/config/main-local.php',
109
     *         '@frontend/config/main.php',
110
     *         '@frontend/config/main-local.php',
111
     *     ],
112
     *     'backend' => [
113
     *         '@common/config/main.php',
114
     *         '@common/config/main-local.php',
115
     *         '@backend/config/main.php',
116
     *         '@backend/config/main-local.php',
117
     *     ],
118
     * ]
119
     * ```     *
120
     */
121
    public $advanced;
122
123
    /**
124
     * @var self Instance of self
125
     */
126
    private static $_instance;
127
    private static $_classes = [
128
        'db' => 'yii\db\Connection',
129
        'userDb' => 'yii\db\Connection',
130
        'cache' => 'yii\caching\Cache',
131
        'authManager' => 'yii\rbac\ManagerInterface',
132
    ];
133
134
    /**
135
     * @inheritdoc
136
     */
137
    public function init()
138
    {
139
        foreach (self::$_classes as $key => $class) {
140
            try {
141
                $this->{$key} = empty($this->{$key}) ? null : Instance::ensure($this->{$key}, $class);
142
            } catch (\Exception $exc) {
143
                $this->{$key} = null;
144
                Yii::error($exc->getMessage());
145
            }
146
        }
147
    }
148
149
    /**
150
     * Create instance of self
151
     * @return static
152
     */
153
    public static function instance()
154
    {
155
        if (self::$_instance === null) {
156
            $type = ArrayHelper::getValue(Yii::$app->params, 'mdm.admin.configs', []);
157
            if (is_array($type) && !isset($type['class'])) {
158
                $type['class'] = static::className();
159
            }
160
161
            return self::$_instance = Yii::createObject($type);
162
        }
163
164
        return self::$_instance;
165
    }
166
167
    public static function __callStatic($name, $arguments)
168
    {
169
        $instance = static::instance();
170
        if ($instance->hasProperty($name)) {
171
            return $instance->$name;
172
        } else {
173
            if (count($arguments)) {
174
                $instance->options[$name] = reset($arguments);
175
            } else {
176
                return array_key_exists($name, $instance->options) ? $instance->options[$name] : null;
177
            }
178
        }
179
    }
180
181
    /**
182
     * @return Connection
183
     */
184
    public static function db()
185
    {
186
        return static::instance()->db;
187
    }
188
189
    /**
190
     * @return Connection
191
     */
192
    public static function userDb()
193
    {
194
        return static::instance()->userDb;
195
    }
196
197
    /**
198
     * @return Cache
199
     */
200
    public static function cache()
201
    {
202
        return static::instance()->cache;
203
    }
204
205
    /**
206
     * @return ManagerInterface
207
     */
208
    public static function authManager()
209
    {
210
        return static::instance()->authManager;
211
    }
212
    /**
213
     * @return integer
214
     */
215
    public static function cacheDuration()
216
    {
217
        return static::instance()->cacheDuration;
218
    }
219
220
    /**
221
     * @return string
222
     */
223
    public static function menuTable()
224
    {
225
        return static::instance()->menuTable;
226
    }
227
228
    /**
229
     * @return string
230
     */
231
    public static function userTable()
232
    {
233
        return static::instance()->userTable;
234
    }
235
236
    /**
237
     * @return string
238
     */
239
    public static function defaultUserStatus()
240
    {
241
        return static::instance()->defaultUserStatus;
242
    }
243
244
    /**
245
     * @return boolean
246
     */
247
    public static function onlyRegisteredRoute()
248
    {
249
        return static::instance()->onlyRegisteredRoute;
250
    }
251
252
    /**
253
     * @return boolean
254
     */
255
    public static function strict()
256
    {
257
        return static::instance()->strict;
258
    }
259
}
260