Passed
Push — 5.2 ( bed7ed...545fbb )
by liu
03:57
created

Db::__make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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 think\db\Connection;
16
use think\db\Raw;
17
18
class Db
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
19
{
20
    /**
21
     * 当前数据库连接对象
22
     * @var Connection
23
     */
24
    protected $connection;
25
26
    /**
27
     * 数据库配置
28
     * @var array
29
     */
30
    protected $config = [];
31
32
    /**
33
     * 读取主库
34
     * @var array
35
     */
36
    protected $readMaster = [];
37
38
    /**
39
     * 查询次数
40
     * @var int
41
     */
42
    protected $queryTimes = 0;
43
44
    /**
45
     * 架构函数
46
     * @param  array         $config 连接配置
47
     * @access public
48
     */
49
    public function __construct(array $config = [])
50
    {
51
        if (empty($config['query'])) {
52
            $config['query'] = '\\think\\db\\Query';
53
        }
54
55
        $this->config = $config;
56
57
        $this->connect($config);
58
    }
59
60
    public static function __make(Config $config)
1 ignored issue
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
Missing function doc comment
Loading history...
61
    {
62
        return 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

62
        return new static(/** @scrutinizer ignore-type */ $config->get('database'));
Loading history...
63
    }
64
65
    /**
66
     * 初始化
67
     * @access public
68
     * @return void
69
     */
70
    public function init(): void
71
    {
72
        $this->queryTimes = 0;
73
        $this->readMaster = [];
74
    }
75
76
    /**
77
     * 切换数据库连接
78
     * @access public
79
     * @param  mixed         $config 连接配置
80
     * @param  bool|string   $name 连接标识 true 强制重新连接
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
81
     * @return $this|object
82
     * @throws Exception
83
     */
84
    public function connect($config = [], $name = false)
85
    {
86
        $this->connection = Connection::instance($this->parseConfig($config), $name);
87
88
        return $this;
89
    }
90
91
    /**
92
     * 设置从主库读取数据
93
     * @access public
94
     * @param  string $table 数据表
95
     * @return $this
96
     */
97
    public function readMaster(string $table = '*')
98
    {
99
        $this->readMaster[$table] = true;
100
101
        return $this;
102
    }
103
104
    /**
105
     * 是否从主库读取数据
106
     * @access public
107
     * @param  string $table 数据表
108
     * @return bool
109
     */
110
    public function isReadMaster(string $table): bool
111
    {
112
        return isset($this->readMaster['*']) || isset($this->readMaster[$table]);
113
    }
114
115
    /**
116
     * 使用表达式设置数据
117
     * @access public
118
     * @param  string $value 表达式
119
     * @return Raw
120
     */
121
    public function raw(string $value): Raw
122
    {
123
        return new Raw($value);
124
    }
125
126
    /**
127
     * 更新查询次数
128
     * @access public
129
     * @return void
130
     */
131
    public function updateQueryTimes(): void
132
    {
133
        $this->queryTimes++;
134
    }
135
136
    /**
137
     * 获得查询次数
138
     * @access public
139
     * @return integer
140
     */
141
    public function getQueryTimes(): int
142
    {
143
        return $this->queryTimes;
144
    }
145
146
    /**
147
     * 数据库连接参数解析
148
     * @access private
149
     * @param  mixed $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
150
     * @return array
151
     */
152
    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...
153
    {
154
        if (empty($config)) {
155
            $config = $this->config;
156
        } elseif (is_string($config)) {
157
            // 支持读取配置参数
158
            $config = $this->config[$config] ?? null;
159
        }
160
161
        return $config;
162
    }
163
164
    /**
165
     * 获取数据库的配置参数
166
     * @access public
167
     * @param  string $name 参数名称
168
     * @return mixed
169
     */
170
    public function getConfig(string $name = '')
171
    {
172
        return $name ? ($this->config[$name] ?? null) : $this->config;
173
    }
174
175
    /**
176
     * 创建一个新的Connection对象
177
     * @access public
178
     * @param  mixed  $connection   连接配置信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
179
     * @return mixed
180
     */
181
    public function buildConnection($connection)
182
    {
183
        return Connection::instance($this->parseConfig($connection));
184
    }
185
186
    /**
187
     * 创建一个新的查询对象
188
     * @access public
189
     * @param  string $query        查询对象类名
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 8 found
Loading history...
190
     * @param  mixed  $connection   连接配置信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
191
     * @return mixed
192
     */
193
    public function buildQuery(string $query, $connection)
194
    {
195
        $connection = $this->buildConnection($connection);
196
        return new $query($connection);
197
    }
198
199
    public function __call($method, $args)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
200
    {
201
        $class = $this->config['query'];
202
203
        $query = new $class($this->connection);
204
205
        return call_user_func_array([$query, $method], $args);
206
    }
207
}
208