Completed
Push — 6.0 ( 8661a3...f9d0f5 )
by yun
06:28
created

Db::event()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
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
/**
16
 * 数据库管理类
17
 * @package think
0 ignored issues
show
Coding Style introduced by
Package name "think" is not valid; consider "Think" instead
Loading history...
18
 */
19
class Db extends DbManager
20
{
21
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
22
     * @param Event  $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
23
     * @param Config $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
24
     * @param Log    $log
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
25
     * @param Cache  $cache
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
26
     * @return Db
27
     * @codeCoverageIgnore
28
     */
29
    public static function __make(Event $event, Config $config, Log $log, Cache $cache)
30
    {
31
        $db = new static();
32
        $db->setConfig($config);
33
        $db->setEvent($event);
34
        $db->setLog($log);
35
        $db->setCache($cache);
36
        $db->triggerSql();
37
38
        return $db;
39
    }
40
41
    /**
42
     * 注入模型对象
43
     * @access public
44
     * @return void
45
     */
46 1
    protected function modelMaker()
47
    {
48 1
    }
49
50
    /**
51
     * 设置配置对象
52
     * @access public
53
     * @param Config $config 配置对象
54
     * @return void
55
     */
56 1
    public function setConfig($config): void
57
    {
58 1
        $this->config = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type think\Config is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59 1
    }
60
61
    /**
62
     * 获取配置参数
63
     * @access public
64
     * @param string $name    配置参数
65
     * @param mixed  $default 默认值
66
     * @return mixed
67
     */
68 1
    public function getConfig(string $name = '', $default = null)
69
    {
70 1
        if ('' !== $name) {
71 1
            return $this->config->get('database.' . $name, $default);
72
        }
73
74 1
        return $this->config->get('database', []);
75
    }
76
77
    /**
78
     * 设置Event对象
79
     * @param Event $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
80
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
81 1
    public function setEvent(Event $event): void
82
    {
83 1
        $this->event = $event;
84 1
    }
85
86
    /**
87
     * 注册回调方法
88
     * @access public
89
     * @param string   $event    事件名
90
     * @param callable $callback 回调方法
91
     * @return void
92
     */
93 1
    public function event(string $event, callable $callback): void
94
    {
95 1
        if ($this->event) {
96 1
            $this->event->listen('db.' . $event, $callback);
97
        }
98 1
    }
99
100
    /**
101
     * 触发事件
102
     * @access public
103
     * @param string $event  事件名
104
     * @param mixed  $params 传入参数
105
     * @param bool   $once
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
106
     * @return mixed
107
     */
108 1
    public function trigger(string $event, $params = null, bool $once = false)
109
    {
110 1
        if ($this->event) {
111 1
            return $this->event->trigger('db.' . $event, $params, $once);
112
        }
113
    }
114
}
115