Completed
Push — 6.0 ( 06cef6...9ae69a )
by liu
04:12
created

Db::getConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
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
/**
21
 * Class Db
22
 * @package think
0 ignored issues
show
Coding Style introduced by
Package name "think" is not valid; consider "Think" instead
Loading history...
23
 * @mixin Query
1 ignored issue
show
Coding Style introduced by
Tag value for @mixin tag indented incorrectly; expected 3 spaces but found 1
Loading history...
24
 */
4 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
25
class Db
26
{
27
    /**
28
     * 当前数据库连接对象
29
     * @var Connection
30
     */
31
    protected $connection;
32
33
    /**
34
     * 数据库连接实例
35
     * @var array
36
     */
37
    protected $instance = [];
38
39
    /**
40
     * Event对象
41
     * @var Event
42
     */
43
    protected $event;
44
45
    /**
46
     * 数据库配置
47
     * @var array
48
     */
49
    protected $config = [];
50
51
    /**
52
     * SQL监听
53
     * @var array
54
     */
55
    protected $listen = [];
56
57
    /**
58
     * 查询次数
59
     * @var int
60
     */
61
    protected $queryTimes = 0;
62
63
    /**
64
     * 架构函数
65
     * @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...
66
     * @access public
67
     */
68
    public function __construct(array $config = [])
69 1
    {
70
        $this->config = $config;
71 1
72 1
    }
73
74
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
75
     * @param Event  $event
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 14 spaces but found 1
Loading history...
76
     * @param Config $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 14 spaces but found 1
Loading history...
77
     * @return Db
1 ignored issue
show
Coding Style introduced by
Tag value for @return tag indented incorrectly; expected 13 spaces but found 1
Loading history...
78
     * @codeCoverageIgnore
79
     */
80
    public static function __make(Event $event, Config $config)
2 ignored issues
show
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->setEvent($event);
85
86
        return $db;
87
    }
88
89
    /**
90
     * 切换数据库连接
91
     * @access public
92
     * @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...
93
     * @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...
94
     * @return $this
95
     */
96
    public function connect($config = '', $name = false)
97
    {
98
        $this->connection = $this->instance($this->parseConfig($config), $name);
99
        return $this;
100
    }
101
102
    /**
103
     * 数据库连接参数解析
104
     * @access private
105
     * @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...
106
     * @return array
107
     */
108
    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...
109
    {
110
        $defaultConnector = $this->config['default'] ?? 'mysql';
111
112
        if (empty($config)) {
113
            $config = $this->config['connections'][$defaultConnector];
114
        } elseif (is_string($config)) {
115
            $config = $this->config['connections'][$config] ?? $this->config['connections'][$defaultConnector];
116
        }
117
118
        if (!is_array($config)) {
119
            throw new DbException('database config error:' . $config);
0 ignored issues
show
Bug introduced by
The type think\DbException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
120
        }
121
122
        return $config;
123
    }
124
125
    /**
126
     * 取得数据库连接类实例
127
     * @access public
128
     * @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...
129
     * @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...
130
     * @return Connection
131
     */
132
    public function instance(array $config = [], $name = false)
133
    {
134
        if (false === $name) {
135
            $name = md5(serialize($config));
136
        }
137
138
        if (true === $name || !isset($this->instance[$name])) {
139
140
            if (empty($config['type'])) {
141
                throw new InvalidArgumentException('Undefined db type');
142
            }
143
144
            if (true === $name) {
145
                $name = md5(serialize($config));
146
            }
147
148
            $this->instance[$name] = App::factory($config['type'], '\\think\\db\\connector\\', $config);
149
        }
150
151
        return $this->instance[$name];
152
    }
153
154
    /**
155
     * 使用表达式设置数据
156
     * @access public
157
     * @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...
158
     * @return Raw
159
     */
160
    public function raw(string $value): Raw
161
    {
162
        return new Raw($value);
163
    }
164
165
    /**
166
     * 更新查询次数
167
     * @access public
168
     * @return void
169
     */
170
    public function updateQueryTimes(): void
171
    {
172
        $this->queryTimes++;
173
    }
174
175
    /**
176
     * 重置查询次数
177
     * @access public
178
     * @return void
179
     */
180
    public function clearQueryTimes(): void
181
    {
182
        $this->queryTimes = 0;
183
    }
184
185
    /**
186
     * 获得查询次数
187
     * @access public
188
     * @return integer
189
     */
190
    public function getQueryTimes(): int
191
    {
192
        return $this->queryTimes;
193
    }
194
195
    /**
196
     * 创建一个新的查询对象
197
     * @access public
198
     * @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...
199
     * @return mixed
200
     */
201
    public function buildQuery($connection = [])
202
    {
203
        $connection = $this->instance($this->parseConfig($connection));
204
        return $this->newQuery($connection);
205
    }
206
207
    /**
208
     * 监听SQL执行
209
     * @access public
210
     * @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...
211
     * @return void
212
     */
213
    public function listen(callable $callback): void
214
    {
215
        $this->listen[] = $callback;
216
    }
217
218
    /**
219
     * 获取监听SQL执行
220
     * @access public
221
     * @return array
222
     */
223
    public function getListen(): array
224
    {
225
        return $this->listen;
226
    }
227
228
    /**
229
     * 设置Event对象
230
     * @param Event $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
231
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
232
    public function setEvent(Event $event)
233
    {
234
        $this->event = $event;
235
    }
236
237
    /**
238
     * 注册回调方法
239
     * @access public
240
     * @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...
241
     * @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...
242
     * @return void
243
     */
244
    public function event(string $event, callable $callback): void
245
    {
246
        if ($this->event) {
247
            $this->event->listen('db.' . $event, $callback);
248
        }
249
    }
250
251
    /**
252
     * 触发事件
253
     * @access public
254
     * @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...
255
     * @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...
256
     * @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...
257
     * @return mixed
258
     */
259
    public function trigger(string $event, $params = null, bool $once = false)
260
    {
261
        if ($this->event) {
262
            return $this->event->trigger('db.' . $event, $params, $once);
263
        }
264
    }
265
266
    /**
267
     * 创建一个新的查询对象
268
     * @access protected
269
     * @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...
270
     * @return mixed
271
     */
272
    protected function newQuery($connection = null)
273
    {
274
        /** @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...
275
        if (is_null($connection) && !$this->connection) {
276
            $this->connect($this->config);
277
        }
278
279
        $connection = $connection ?: $this->connection;
280
        $class      = $connection->getQueryClass();
281
        $query      = new $class($connection);
282
283
        $query->setDb($this);
284
285
        return $query;
286
    }
287
288
    public function __call($method, $args)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __call()
Loading history...
289
    {
290
        $query = $this->newQuery($this->connection);
291
292
        return call_user_func_array([$query, $method], $args);
293
    }
294
}
295