Completed
Push — master ( df46a8...ddc7c0 )
by Misbahul D
03:24
created

Configs::userDb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace mdm\admin\components;
4
5
use Yii;
6
use yii\db\Connection;
7
use yii\caching\Cache;
8
use yii\rbac\ManagerInterface;
9
use yii\helpers\ArrayHelper;
10
use yii\di\Instance;
11
12
/**
13
 * Configs
14
 * Used for configure some value. 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
class Configs extends \yii\base\Object
43
{
44
    const CACHE_TAG = 'mdm.admin';
45
46
    /**
47
     * @var ManagerInterface .
48
     */
49
    public $authManager = 'authManager';
50
51
    /**
52
     * @var Connection Database connection.
53
     */
54
    public $db = 'db';
55
56
    /**
57
     * @var Connection Database connection.
58
     */
59
    public $userDb = 'db';
60
61
    /**
62
     * @var Cache Cache component.
63
     */
64
    public $cache = 'cache';
65
66
    /**
67
     * @var integer Cache duration. Default to a hour.
68
     */
69
    public $cacheDuration = 3600;
70
71
    /**
72
     * @var string Menu table name.
73
     */
74
    public $menuTable = '{{%menu}}';
75
76
    /**
77
     * @var string Menu table name.
78
     */
79
    public $userTable = '{{%user}}';
80
81
    /**
82
     * @var integer Default status user signup. 10 mean active.
83
     */
84
    public $defaultUserStatus = 10;
85
86
    /**
87
     * @var boolean If true then AccessControl only check if route are registered.
88
     */
89
    public $onlyRegisteredRoute = false;
90
91
    /**
92
     * @var boolean If false then AccessControl will check without Rule.
93
     */
94
    public $strict = true;
95
96
    /**
97
     * @var array 
98
     */
99
    public $options;
100
101
    /**
102
     * @var self Instance of self
103
     */
104
    private static $_instance;
105
    private static $_classes = [
106
        'db' => 'yii\db\Connection',
107
        'userDb' => 'yii\db\Connection',
108
        'cache' => 'yii\caching\Cache',
109
        'authManager' => 'yii\rbac\ManagerInterface'
110
    ];
111
112
    /**
113
     * @inheritdoc
114
     */
115
    public function init()
116
    {
117
        foreach (self::$_classes as $key => $class) {
118
            try {
119
                $this->{$key} = empty($this->{$key}) ? null : Instance::ensure($this->{$key}, $class);
120
            } catch (\Exception $exc) {
121
                $this->{$key} = null;
122
                Yii::error($exc->getMessage());
123
            }
124
        }
125
    }
126
127
    /**
128
     * Create instance of self
129
     * @return static
130
     */
131
    public static function instance()
132
    {
133
        if (self::$_instance === null) {
134
            $type = ArrayHelper::getValue(Yii::$app->params, 'mdm.admin.configs', []);
135
            if (is_array($type) && !isset($type['class'])) {
136
                $type['class'] = static::className();
137
            }
138
139
            return self::$_instance = Yii::createObject($type);
140
        }
141
142
        return self::$_instance;
143
    }
144
145
    public static function __callStatic($name, $arguments)
146
    {
147
        $instance = static::instance();
148
        if ($instance->hasProperty($name)) {
149
            return $instance->$name;
150
        } else {
151
            if (count($arguments)) {
152
                $instance->options[$name] = reset($arguments);
153
            } else {
154
                return array_key_exists($name, $instance->options) ? $instance->options[$name] : null;
155
            }
156
        }
157
    }
158
159
    /**
160
     * @return Connection
161
     */
162
    public static function db()
163
    {
164
        return static::instance()->db;
165
    }
166
167
    /**
168
     * @return Connection
169
     */
170
    public static function userDb()
171
    {
172
        return static::instance()->userDb;
173
    }
174
175
    /**
176
     * @return Cache
177
     */
178
    public static function cache()
179
    {
180
        return static::instance()->cache;
181
    }
182
183
    /**
184
     * @return ManagerInterface
185
     */
186
    public static function authManager()
187
    {
188
        return static::instance()->authManager;
189
    }
190
    /**
191
     * @return integer
192
     */
193
    public static function cacheDuration()
194
    {
195
        return static::instance()->cacheDuration;
196
    }
197
198
    /**
199
     * @return string
200
     */
201
    public static function menuTable()
202
    {
203
        return static::instance()->menuTable;
204
    }
205
206
    /**
207
     * @return string
208
     */
209
    public static function userTable()
210
    {
211
        return static::instance()->userTable;
212
    }
213
214
    /**
215
     * @return string
216
     */
217
    public static function defaultUserStatus()
218
    {
219
        return static::instance()->defaultUserStatus;
220
    }
221
222
    /**
223
     * @return boolean
224
     */
225
    public static function onlyRegisteredRoute()
226
    {
227
        return static::instance()->onlyRegisteredRoute;
228
    }
229
230
    /**
231
     * @return boolean
232
     */
233
    public static function strict()
234
    {
235
        return static::instance()->strict;
236
    }
237
}
238