Completed
Push — 6.0 ( cfe58d...b7fde5 )
by liu
02:56
created

Db::instance()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 8
nop 2
dl 0
loc 20
ccs 0
cts 10
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Db::getQueryTimes() 0 3 1
A Db::clearQueryTimes() 0 3 1
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 array
30
     */
31
    protected $instance = [];
32
33
    /**
34
     * Event对象
35
     * @var Event
36
     */
37
    protected $event;
38
39
    /**
40
     * 数据库配置
41
     * @var array
42
     */
43
    protected $config = [];
44
45
    /**
46
     * SQL监听
47
     * @var array
48
     */
49
    protected $listen = [];
50
51
    /**
52
     * 查询次数
53
     * @var int
54
     */
55
    protected $queryTimes = 0;
56
57
    /**
58
     * 架构函数
59
     * @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...
60
     * @access public
61
     */
62 1
    public function __construct(array $config = [])
63
    {
64 1
        $this->config = $config;
65
66 1
    }
67
68
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
69
     * @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...
70
     * @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...
71
     * @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...
72
     * @codeCoverageIgnore
73
     */
74
    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...
75
    {
76
        $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

76
        $db = new static(/** @scrutinizer ignore-type */ $config->get('database'));
Loading history...
77
78
        $db->setEvent($event);
79
80
        return $db;
81
    }
82
83
    /**
84
     * 连接/切换数据库连接
85
     * @access public
86
     * @param string|null $name  连接标识
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
87
     * @param bool        $force 强制重新连接
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
88
     * @return Connection
89
     */
90
    public function connect(string $name = null, bool $force = false): Connection
91
    {
92
        if (empty($name)) {
93
            $name = $this->config['default'] ?? 'mysql';
94
        }
95
96
        $config = $this->config['connections'][$name];
97
98
        if ($force || !isset($this->instance[$name])) {
99
            if (empty($config['type'])) {
100
                throw new InvalidArgumentException('Undefined db type');
101
            }
102
103
            $this->instance[$name] = App::factory($config['type'], '\\think\\db\\connector\\', $config);
104
        }
105
106
        return $this->instance[$name];
107
    }
108
109
    /**
110
     * 使用表达式设置数据
111
     * @access public
112
     * @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...
113
     * @return Raw
114
     */
115
    public function raw(string $value): Raw
116
    {
117
        return new Raw($value);
118
    }
119
120
    /**
121
     * 更新查询次数
122
     * @access public
123
     * @return void
124
     */
125
    public function updateQueryTimes(): void
126
    {
127
        $this->queryTimes++;
128
    }
129
130
    /**
131
     * 重置查询次数
132
     * @access public
133
     * @return void
134
     */
135
    public function clearQueryTimes(): void
136
    {
137
        $this->queryTimes = 0;
138
    }
139
140
    /**
141
     * 获得查询次数
142
     * @access public
143
     * @return integer
144
     */
145
    public function getQueryTimes(): int
146
    {
147
        return $this->queryTimes;
148
    }
149
150
    /**
151
     * 创建一个新的查询对象
152
     * @access public
153
     * @param string|null $connection 连接配置标识
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
154
     * @return Query
155
     */
156
    public function buildQuery(string $connection = null): Query
157
    {
158
        $connection = $this->connect($connection);
159
        return $this->newQuery($connection);
160
    }
161
162
    /**
163
     * 监听SQL执行
164
     * @access public
165
     * @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...
166
     * @return void
167
     */
168
    public function listen(callable $callback): void
169
    {
170
        $this->listen[] = $callback;
171
    }
172
173
    /**
174
     * 获取监听SQL执行
175
     * @access public
176
     * @return array
177
     */
178
    public function getListen(): array
179
    {
180
        return $this->listen;
181
    }
182
183
    /**
184
     * 设置Event对象
185
     * @param Event $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
186
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
187
    public function setEvent(Event $event)
188
    {
189
        $this->event = $event;
190
    }
191
192
    /**
193
     * 注册回调方法
194
     * @access public
195
     * @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...
196
     * @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...
197
     * @return void
198
     */
199
    public function event(string $event, callable $callback): void
200
    {
201
        if ($this->event) {
202
            $this->event->listen('db.' . $event, $callback);
203
        }
204
    }
205
206
    /**
207
     * 触发事件
208
     * @access public
209
     * @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...
210
     * @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...
211
     * @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...
212
     * @return mixed
213
     */
214
    public function trigger(string $event, $params = null, bool $once = false)
215
    {
216
        if ($this->event) {
217
            return $this->event->trigger('db.' . $event, $params, $once);
218
        }
219
    }
220
221
    /**
222
     * 创建一个新的查询对象
223
     * @access protected
224
     * @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...
225
     * @return Query
226
     */
227
    protected function newQuery(Connection $connection = null): Query
228
    {
229
        /** @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...
230
        $connection = $connection ?: $this->connect();
231
        $class      = $connection->getQueryClass();
232
        $query      = new $class($connection);
233
234
        $query->setDb($this);
235
236
        return $query;
237
    }
238
239
    public function __call($method, $args)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __call()
Loading history...
240
    {
241
        $query = $this->newQuery();
242
243
        return call_user_func_array([$query, $method], $args);
244
    }
245
}
246