Completed
Push — 6.0 ( 52d3ce...17b267 )
by liu
02:46
created

Db::trigger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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 int
55
     */
56
    protected $queryTimes = 0;
57
58
    /**
59
     * 架构函数
60
     * @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...
61
     * @access public
62
     */
63
    public function __construct(array $config = [])
64
    {
65
        $this->option = $config;
66
67
        $this->connect($config);
68
    }
69
70
    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...
71
    {
72
        $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

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