0
0

Facade.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 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;
  12. class Facade
  13. {
  14. /**
  15. * 绑定对象
  16. * @var array
  17. */
  18. protected static $bind = [];
  19. /**
  20. * 始终创建新的对象实例
  21. * @var bool
  22. */
  23. protected static $alwaysNewInstance;
  24. /**
  25. * 绑定类的静态代理
  26. * @static
  27. * @access public
  28. * @param string|array $name 类标识
  29. * @param string $class 类名
  30. * @return object
  31. */
  32. public static function bind($name, $class = null)
  33. {
  34. if (__CLASS__ != static::class) {
  35. return self::__callStatic('bind', func_get_args());
  36. }
  37. if (is_array($name)) {
  38. self::$bind = array_merge(self::$bind, $name);
  39. } else {
  40. self::$bind[$name] = $class;
  41. }
  42. }
  43. /**
  44. * 创建Facade实例
  45. * @static
  46. * @access protected
  47. * @param string $class 类名或标识
  48. * @param array $args 变量
  49. * @param bool $newInstance 是否每次创建新的实例
  50. * @return object
  51. */
  52. protected static function createFacade($class = '', $args = [], $newInstance = false)
  53. {
  54. $class = $class ?: static::class;
  55. $facadeClass = static::getFacadeClass();
  56. if ($facadeClass) {
  57. $class = $facadeClass;
  58. } elseif (isset(self::$bind[$class])) {
  59. $class = self::$bind[$class];
  60. }
  61. if (static::$alwaysNewInstance) {
  62. $newInstance = true;
  63. }
  64. return Container::getInstance()->make($class, $args, $newInstance);
  65. }
  66. /**
  67. * 获取当前Facade对应类名(或者已经绑定的容器对象标识)
  68. * @access protected
  69. * @return string
  70. */
  71. protected static function getFacadeClass()
  72. {}
  73. /**
  74. * 带参数实例化当前Facade类
  75. * @access public
  76. * @return mixed
  77. */
  78. public static function instance(...$args)
  79. {
  80. if (__CLASS__ != static::class) {
  81. return self::createFacade('', $args);
  82. }
  83. }
  84. /**
  85. * 调用类的实例
  86. * @access public
  87. * @param string $class 类名或者标识
  88. * @param array|true $args 变量
  89. * @param bool $newInstance 是否每次创建新的实例
  90. * @return mixed
  91. */
  92. public static function make($class, $args = [], $newInstance = false)
  93. {
  94. if (__CLASS__ != static::class) {
  95. return self::__callStatic('make', func_get_args());
  96. }
  97. if (true === $args) {
  98. // 总是创建新的实例化对象
  99. $newInstance = true;
  100. $args = [];
  101. }
  102. return self::createFacade($class, $args, $newInstance);
  103. }
  104. // 调用实际类的方法
  105. public static function __callStatic($method, $params)
  106. {
  107. return call_user_func_array([static::createFacade(), $method], $params);
  108. }
  109. }