Db.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. namespace think\facade;
  12. if (class_exists('think\Facade')) {
  13. class Facade extends \think\Facade
  14. {}
  15. } else {
  16. class Facade
  17. {
  18. /**
  19. * 始终创建新的对象实例
  20. * @var bool
  21. */
  22. protected static $alwaysNewInstance;
  23. protected static $instance;
  24. /**
  25. * 获取当前Facade对应类名
  26. * @access protected
  27. * @return string
  28. */
  29. protected static function getFacadeClass()
  30. {}
  31. /**
  32. * 创建Facade实例
  33. * @static
  34. * @access protected
  35. * @param bool $newInstance 是否每次创建新的实例
  36. * @return object
  37. */
  38. protected static function createFacade(bool $newInstance = false)
  39. {
  40. $class = static::getFacadeClass() ?: 'think\DbManager';
  41. if (static::$alwaysNewInstance) {
  42. $newInstance = true;
  43. }
  44. if ($newInstance) {
  45. return new $class();
  46. }
  47. if (!self::$instance) {
  48. self::$instance = new $class();
  49. }
  50. return self::$instance;
  51. }
  52. // 调用实际类的方法
  53. public static function __callStatic($method, $params)
  54. {
  55. return call_user_func_array([static::createFacade(), $method], $params);
  56. }
  57. }
  58. }
  59. /**
  60. * @see \think\DbManager
  61. * @mixin \think\DbManager
  62. */
  63. class Db extends Facade
  64. {
  65. /**
  66. * 获取当前Facade对应类名(或者已经绑定的容器对象标识)
  67. * @access protected
  68. * @return string
  69. */
  70. protected static function getFacadeClass()
  71. {
  72. return 'think\DbManager';
  73. }
  74. }