Completed
Push — 6.0 ( 800870...267f59 )
by yun
03:41 queued 17s
created

Db::parseConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think;
14
15
use InvalidArgumentException;
16
use think\db\Connection;
17
use think\db\Query;
18
use think\db\Raw;
19
20
class Db
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class Db
Loading history...
21
{
22
    /**
23
     * 当前数据库连接对象
24
     * @var Connection
25
     */
26
    protected $connection;
27
28
    /**
29
     * 数据库连接实例
30
     * @var array
31
     */
32
    protected $instance = [];
33
34
    /**
35
     * 配置对象
36
     * @var Config
37
     */
38
    protected $config;
39
40
    /**
41
     * Event对象
42
     * @var Event
43
     */
44
    protected $event;
45
46
    /**
47
     * 数据库配置
48
     * @var array
49
     */
50
    protected $option = [];
51
52
    /**
53
     * 读取主库
54
     * @var array
55
     */
56
    protected $readMaster = [];
57
58
    /**
59
     * 查询次数
60
     * @var int
61
     */
62
    protected $queryTimes = 0;
63
64
    /**
65
     * 架构函数
66
     * @param array $config 连接配置
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
67
     * @access public
68
     */
69
    public function __construct(array $config = [])
70
    {
71
        if (empty($config['query'])) {
72
            $config['query'] = Query::class;
73
        }
74
75
        $this->option = $config;
76
77
        $this->connect($config);
78
    }
79
80
    public static function __make(Event $event, Config $config)
2 ignored issues
show
Coding Style introduced by
Missing doc comment for function __make()
Loading history...
Coding Style introduced by
Method name "Db::__make" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
Coding Style introduced by
Public method name "Db::__make" must not be prefixed with an underscore
Loading history...
81
    {
82
        $db = new static($config->get('database'));
0 ignored issues
show
Bug introduced by
It seems like $config->get('database') can also be of type null; however, parameter $config of think\Db::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        $db = new static(/** @scrutinizer ignore-type */ $config->get('database'));
Loading history...
83
84
        $db->event  = $event;
85
        $db->config = $config;
86
87
        return $db;
88
    }
89
90
    /**
91
     * 切换数据库连接
92
     * @access public
93
     * @param mixed       $config 连接配置
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
94
     * @param bool|string $name   连接标识 true 强制重新连接
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
95
     * @return $this
96
     */
97
    public function connect($config = [], $name = false)
98
    {
99
        $this->connection = $this->instance($this->parseConfig($config), $name);
100
        return $this;
101
    }
102
103
    /**
104
     * 取得数据库连接类实例
105
     * @access public
106
     * @param array       $config 连接配置
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
107
     * @param bool|string $name   连接标识 true 强制重新连接
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
108
     * @return Connection
109
     */
110
    public function instance(array $config = [], $name = false)
111
    {
112
        if (false === $name) {
113
            $name = md5(serialize($config));
114
        }
115
116
        if (true === $name || !isset($this->instance[$name])) {
117
118
            if (empty($config['type'])) {
119
                throw new InvalidArgumentException('Undefined db type');
120
            }
121
122
            if (true === $name) {
123
                $name = md5(serialize($config));
124
            }
125
126
            $this->instance[$name] = App::factory($config['type'], '\\think\\db\\connector\\', $config);
127
        }
128
129
        return $this->instance[$name];
130
    }
131
132
    /**
133
     * 设置从主库读取数据
134
     * @access public
135
     * @param string $table 数据表
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
136
     * @return $this
137
     */
138
    public function readMaster(string $table = '*')
139
    {
140
        $this->readMaster[$table] = true;
141
142
        return $this;
143
    }
144
145
    /**
146
     * 是否从主库读取数据
147
     * @access public
148
     * @param string $table 数据表
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
149
     * @return bool
150
     */
151
    public function isReadMaster(string $table): bool
152
    {
153
        return isset($this->readMaster['*']) || isset($this->readMaster[$table]);
154
    }
155
156
    /**
157
     * 使用表达式设置数据
158
     * @access public
159
     * @param string $value 表达式
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
160
     * @return Raw
161
     */
162
    public function raw(string $value): Raw
163
    {
164
        return new Raw($value);
165
    }
166
167
    /**
168
     * 更新查询次数
169
     * @access public
170
     * @return void
171
     */
172
    public function updateQueryTimes(): void
173
    {
174
        $this->queryTimes++;
175
    }
176
177
    /**
178
     * 获得查询次数
179
     * @access public
180
     * @return integer
181
     */
182
    public function getQueryTimes(): int
183
    {
184
        return $this->queryTimes;
185
    }
186
187
    /**
188
     * 数据库连接参数解析
189
     * @access private
190
     * @param mixed $config
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
191
     * @return array
192
     */
193
    private function parseConfig($config): array
0 ignored issues
show
Coding Style introduced by
Private method name "Db::parseConfig" must be prefixed with an underscore
Loading history...
194
    {
195
        if (empty($config)) {
196
            $config = $this->option;
197
        } elseif (is_string($config)) {
198
            // 支持读取配置参数
199
            $config = $this->option[$config] ?? null;
200
        }
201
202
        return $config;
203
    }
204
205
    /**
206
     * 获取数据库的配置参数
207
     * @access public
208
     * @param string $name 参数名称
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
209
     * @return mixed
210
     */
211
    public function getConfig(string $name = '')
212
    {
213
        return $name ? ($this->option[$name] ?? null) : $this->option;
214
    }
215
216
    /**
217
     * 创建一个新的查询对象
218
     * @access public
219
     * @param string       $query      查询对象类名
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
220
     * @param string|array $connection 连接配置信息
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
221
     * @return mixed
222
     */
223
    public function buildQuery(string $query, $connection = [])
224
    {
225
        return $this->connect($connection)->newQuery($query);
226
    }
227
228
    /**
229
     * 注册回调方法
230
     * @access public
231
     * @param string   $event    事件名
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
232
     * @param callable $callback 回调方法
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
233
     * @return void
234
     */
235
    public function event(string $event, callable $callback): void
236
    {
237
        $this->event->listen('db.' . $event, $callback);
238
    }
239
240
    /**
241
     * 触发事件
242
     * @access public
243
     * @param string $event  事件名
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
244
     * @param mixed  $params 传入参数
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
245
     * @param bool   $once
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
246
     * @return mixed
247
     */
248
    public function trigger(string $event, $params = null, bool $once = false)
249
    {
250
        return $this->event->trigger('db.' . $event, $params, $once);
251
    }
252
253
    /**
254
     * 创建一个新的查询对象
255
     * @access protected
256
     * @param string     $class      查询对象类名
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
257
     * @param Connection $connection 连接对象
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
258
     * @return mixed
259
     */
260
    protected function newQuery(string $class, $connection = null)
261
    {
262
        /** @var Query $query */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
263
        $query = new $class($connection ?: $this->connection);
264
265
        $query->setDb($this);
266
267
        return $query;
268
    }
269
270
    public function __call($method, $args)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __call()
Loading history...
271
    {
272
        $query = $this->newQuery($this->option['query'], $this->connection);
273
274
        return call_user_func_array([$query, $method], $args);
275
    }
276
}
277