1 | <?php |
||
2 | // +---------------------------------------------------------------------- |
||
3 | // | ThinkPHP [ WE CAN DO IT JUST THINK ] |
||
4 | // +---------------------------------------------------------------------- |
||
5 | // | Copyright (c) 2006~2021 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 |
||
18 | * @property Config $config |
||
19 | */ |
||
20 | class Db extends DbManager |
||
21 | { |
||
22 | /** |
||
23 | * @param Event $event |
||
24 | * @param Config $config |
||
25 | * @param Log $log |
||
26 | * @param Cache $cache |
||
27 | * @return Db |
||
28 | * @codeCoverageIgnore |
||
29 | */ |
||
30 | public static function __make(Event $event, Config $config, Log $log, Cache $cache) |
||
31 | { |
||
32 | $db = new static(); |
||
33 | $db->setConfig($config); |
||
34 | $db->setEvent($event); |
||
35 | $db->setLog($log); |
||
36 | |||
37 | $store = $db->getConfig('cache_store'); |
||
38 | $db->setCache($cache->store($store)); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
39 | $db->triggerSql(); |
||
40 | |||
41 | return $db; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * 注入模型对象 |
||
46 | * @access public |
||
47 | * @return void |
||
48 | */ |
||
49 | 3 | protected function modelMaker() |
|
50 | { |
||
51 | 3 | } |
|
52 | |||
53 | /** |
||
54 | * 设置配置对象 |
||
55 | * @access public |
||
56 | * @param Config $config 配置对象 |
||
57 | * @return void |
||
58 | */ |
||
59 | 3 | public function setConfig($config): void |
|
60 | { |
||
61 | 3 | $this->config = $config; |
|
62 | 3 | } |
|
63 | |||
64 | /** |
||
65 | * 获取配置参数 |
||
66 | * @access public |
||
67 | * @param string $name 配置参数 |
||
68 | * @param mixed $default 默认值 |
||
69 | * @return mixed |
||
70 | */ |
||
71 | 3 | public function getConfig(string $name = '', $default = null) |
|
72 | { |
||
73 | 3 | if ('' !== $name) { |
|
74 | 3 | return $this->config->get('database.' . $name, $default); |
|
75 | } |
||
76 | |||
77 | 3 | return $this->config->get('database', []); |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * 设置Event对象 |
||
82 | * @param Event $event |
||
83 | */ |
||
84 | 3 | public function setEvent(Event $event): void |
|
85 | { |
||
86 | 3 | $this->event = $event; |
|
87 | 3 | } |
|
88 | |||
89 | /** |
||
90 | * 注册回调方法 |
||
91 | * @access public |
||
92 | * @param string $event 事件名 |
||
93 | * @param callable $callback 回调方法 |
||
94 | * @return void |
||
95 | */ |
||
96 | 3 | public function event(string $event, callable $callback): void |
|
97 | { |
||
98 | 3 | if ($this->event) { |
|
99 | 3 | $this->event->listen('db.' . $event, $callback); |
|
100 | } |
||
101 | 3 | } |
|
102 | |||
103 | /** |
||
104 | * 触发事件 |
||
105 | * @access public |
||
106 | * @param string $event 事件名 |
||
107 | * @param mixed $params 传入参数 |
||
108 | * @param bool $once |
||
109 | * @return mixed |
||
110 | */ |
||
111 | 3 | public function trigger(string $event, $params = null, bool $once = false) |
|
112 | { |
||
113 | 3 | if ($this->event) { |
|
114 | 3 | return $this->event->trigger('db.' . $event, $params, $once); |
|
115 | } |
||
116 | } |
||
117 | } |
||
118 |