Completed
Push — 6.0 ( 5d7d27...503afe )
by liu
04:33
created

Db::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
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\BaseQuery;
17
use think\db\Connection;
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
24
 */
25
class Db
26
{
27
    /**
28
     * 数据库连接实例
29
     * @var array
30
     */
31
    protected $instance = [];
32
33
    /**
34
     * 当前连接实例
35
     * @var Connection
36
     */
37
    protected $connection;
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 连接配置
66
     * @access public
67
     */
68 1
    public function __construct(array $config = [])
69
    {
70 1
        $this->config = $config;
71
72 1
    }
73
74
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
75
     * @param Event  $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
76
     * @param Config $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
77
     * @return Db
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 string|null $name  连接标识
93
     * @param bool        $force 强制重新连接
94
     * @return $this
95
     */
96
    public function connect(string $name = null, bool $force = false)
97
    {
98
        if (empty($name)) {
99
            $name = $this->config['default'] ?? 'mysql';
100
        }
101
102
        if (!isset($this->config['connections'][$name])) {
103
            throw new InvalidArgumentException('Undefined db config:' . $name);
104
        }
105
106
        $config = $this->config['connections'][$name];
107
108
        if ($force || !isset($this->instance[$name])) {
109
            if (empty($config['type'])) {
110
                throw new InvalidArgumentException('Undefined db type');
111
            }
112
113
            $this->instance[$name] = App::factory($config['type'], '\\think\\db\\connector\\', $config);
114
        }
115
116
        $this->connection = $this->instance[$name];
117
        return $this;
118
    }
119
120
    /**
121
     * 使用表达式设置数据
122
     * @access public
123
     * @param string $value 表达式
124
     * @return Raw
125
     */
126
    public function raw(string $value): Raw
127
    {
128
        return new Raw($value);
129
    }
130
131
    /**
132
     * 更新查询次数
133
     * @access public
134
     * @return void
135
     */
136
    public function updateQueryTimes(): void
137
    {
138
        $this->queryTimes++;
139
    }
140
141
    /**
142
     * 重置查询次数
143
     * @access public
144
     * @return void
145
     */
146
    public function clearQueryTimes(): void
147
    {
148
        $this->queryTimes = 0;
149
    }
150
151
    /**
152
     * 获得查询次数
153
     * @access public
154
     * @return integer
155
     */
156
    public function getQueryTimes(): int
157
    {
158
        return $this->queryTimes;
159
    }
160
161
    /**
162
     * 创建一个新的查询对象
163
     * @access public
164
     * @param string|null $name 连接配置标识
165
     * @return BaseQuery
166
     */
167
    public function buildQuery(string $name = null): BaseQuery
168
    {
169
        $this->connect($name);
170
171
        $connection = $this->connection;
172
        $connection->setDb($this);
173
174
        $class = $connection->getQueryClass();
175
        $query = new $class($connection);
176
177
        if (!empty($this->config['time_query_rule'])) {
178
            $query->timeRule($this->config['time_query_rule']);
179
        }
180
181
        return $query;
182
    }
183
184
    /**
185
     * 监听SQL执行
186
     * @access public
187
     * @param callable $callback 回调方法
188
     * @return void
189
     */
190
    public function listen(callable $callback): void
191
    {
192
        $this->listen[] = $callback;
193
    }
194
195
    /**
196
     * 获取监听SQL执行
197
     * @access public
198
     * @return array
199
     */
200
    public function getListen(): array
201
    {
202
        return $this->listen;
203
    }
204
205
    /**
206
     * 设置Event对象
207
     * @param Event $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
208
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
209
    public function setEvent(Event $event)
210
    {
211
        $this->event = $event;
212
    }
213
214
    /**
215
     * 注册回调方法
216
     * @access public
217
     * @param string   $event    事件名
218
     * @param callable $callback 回调方法
219
     * @return void
220
     */
221
    public function event(string $event, callable $callback): void
222
    {
223
        if ($this->event) {
224
            $this->event->listen('db.' . $event, $callback);
225
        }
226
    }
227
228
    /**
229
     * 触发事件
230
     * @access public
231
     * @param string $event  事件名
232
     * @param mixed  $params 传入参数
233
     * @param bool   $once
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
234
     * @return mixed
235
     */
236
    public function trigger(string $event, $params = null, bool $once = false)
237
    {
238
        if ($this->event) {
239
            return $this->event->trigger('db.' . $event, $params, $once);
240
        }
241
    }
242
243
    public function __call($method, $args)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __call()
Loading history...
244
    {
245
        return call_user_func_array([$this->buildQuery(), $method], $args);
246
    }
247
}
248