Db.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think;
  13. /**
  14. * 数据库管理类
  15. * @package think
  16. */
  17. class Db extends DbManager
  18. {
  19. /**
  20. * @param Event $event
  21. * @param Config $config
  22. * @param Log $log
  23. * @param Cache $cache
  24. * @return Db
  25. * @codeCoverageIgnore
  26. */
  27. public static function __make(Event $event, Config $config, Log $log, Cache $cache)
  28. {
  29. $db = new static();
  30. $db->setConfig($config);
  31. $db->setEvent($event);
  32. $db->setLog($log);
  33. $db->setCache($cache);
  34. $db->triggerSql();
  35. return $db;
  36. }
  37. /**
  38. * 注入模型对象
  39. * @access public
  40. * @return void
  41. */
  42. protected function modelMaker()
  43. {
  44. }
  45. /**
  46. * 设置配置对象
  47. * @access public
  48. * @param Config $config 配置对象
  49. * @return void
  50. */
  51. public function setConfig($config): void
  52. {
  53. $this->config = $config;
  54. }
  55. /**
  56. * 获取配置参数
  57. * @access public
  58. * @param string $name 配置参数
  59. * @param mixed $default 默认值
  60. * @return mixed
  61. */
  62. public function getConfig(string $name = '', $default = null)
  63. {
  64. if ('' !== $name) {
  65. return $this->config->get('database.' . $name, $default);
  66. }
  67. return $this->config->get('database', []);
  68. }
  69. /**
  70. * 设置Event对象
  71. * @param Event $event
  72. */
  73. public function setEvent(Event $event): void
  74. {
  75. $this->event = $event;
  76. }
  77. /**
  78. * 注册回调方法
  79. * @access public
  80. * @param string $event 事件名
  81. * @param callable $callback 回调方法
  82. * @return void
  83. */
  84. public function event(string $event, callable $callback): void
  85. {
  86. if ($this->event) {
  87. $this->event->listen('db.' . $event, $callback);
  88. }
  89. }
  90. /**
  91. * 触发事件
  92. * @access public
  93. * @param string $event 事件名
  94. * @param mixed $params 传入参数
  95. * @param bool $once
  96. * @return mixed
  97. */
  98. public function trigger(string $event, $params = null, bool $once = false)
  99. {
  100. if ($this->event) {
  101. return $this->event->trigger('db.' . $event, $params, $once);
  102. }
  103. }
  104. }