Passed
Push — 5.2 ( 62a622...ec44db )
by liu
02:31
created

Db::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 7
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
     * @param  array         $config 连接配置
35
     * @access public
36
     */
37
    public function __construct(array $config = [])
38
    {
39
        if (empty($config['query'])) {
40
            $config['query'] = '\\think\\db\\Query';
41
        }
42
43
        $this->config = $config;
44
45
        $this->connect($config);
46
    }
47
48
    public static function __make(Config $config)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
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...
49
    {
50
        return new static($config->get('database', []));
51
    }
52
53
    /**
54
     * 切换数据库连接
55
     * @access public
56
     * @param  mixed         $config 连接配置
57
     * @param  bool|string   $name 连接标识 true 强制重新连接
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
58
     * @return $this|object
59
     * @throws Exception
60
     */
61
    public function connect($config = [], $name = false)
62
    {
63
        $this->connection = Connection::instance($this->parseConfig($config), $name);
64
65
        return $this;
66
    }
67
68
    /**
69
     * 使用表达式设置数据
70
     * @access public
71
     * @param  string $value 表达式
72
     * @return Raw
73
     */
74
    public function raw(string $value): Raw
75
    {
76
        return new Raw($value);
77
    }
78
79
    /**
80
     * 获得查询次数
81
     * @access public
82
     * @return integer
83
     */
84
    public function getQueryTimes(): int
85
    {
86
        return $this->connection->getQueryTimes();
87
    }
88
89
    /**
90
     * 数据库连接参数解析
91
     * @access private
92
     * @param  mixed $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
93
     * @return array
94
     */
95
    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...
96
    {
97
        if (empty($config)) {
98
            $config = $this->config;
99
        } elseif (is_string($config)) {
100
            // 支持读取配置参数
101
            $config = $this->config[$config] ?? null;
102
        }
103
104
        return $config;
105
    }
106
107
    /**
108
     * 获取数据库的配置参数
109
     * @access public
110
     * @param  string $name 参数名称
111
     * @return mixed
112
     */
113
    public function getConfig(string $name = '')
114
    {
115
        return $name ? ($this->config[$name] ?? null) : $this->config;
116
    }
117
118
    /**
119
     * 创建一个新的Connection对象
120
     * @access public
121
     * @param  mixed  $connection   连接配置信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
122
     * @return mixed
123
     */
124
    public function buildConnection($connection)
125
    {
126
        return Connection::instance($this->parseConfig($connection));
127
    }
128
129
    /**
130
     * 创建一个新的查询对象
131
     * @access public
132
     * @param  string $query        查询对象类名
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 8 found
Loading history...
133
     * @param  mixed  $connection   连接配置信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
134
     * @return mixed
135
     */
136
    public function buildQuery(string $query, $connection)
137
    {
138
        $connection = $this->buildConnection($connection);
139
        return new $query($connection);
140
    }
141
142
    public function __call($method, $args)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
143
    {
144
        $class = $this->config['query'];
145
146
        $query = new $class($this->connection);
147
148
        return call_user_func_array([$query, $method], $args);
149
    }
150
}
151