Container.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. use Closure;
  13. use InvalidArgumentException;
  14. use ReflectionClass;
  15. use ReflectionFunction;
  16. use ReflectionMethod;
  17. class Container
  18. {
  19. /**
  20. * 容器对象实例
  21. * @var Container
  22. */
  23. protected static $instance;
  24. /**
  25. * 容器中的对象实例
  26. * @var array
  27. */
  28. protected $instances = [];
  29. /**
  30. * 容器绑定标识
  31. * @var array
  32. */
  33. protected $bind = [];
  34. /**
  35. * 获取当前容器的实例(单例)
  36. * @access public
  37. * @return static
  38. */
  39. public static function getInstance()
  40. {
  41. if (is_null(static::$instance)) {
  42. static::$instance = new static;
  43. }
  44. return static::$instance;
  45. }
  46. /**
  47. * 获取容器中的对象实例
  48. * @access public
  49. * @param string $abstract 类名或者标识
  50. * @param array|true $vars 变量
  51. * @param bool $newInstance 是否每次创建新的实例
  52. * @return object
  53. */
  54. public static function get($abstract, $vars = [], $newInstance = false)
  55. {
  56. return static::getInstance()->make($abstract, $vars, $newInstance);
  57. }
  58. /**
  59. * 绑定一个类、闭包、实例、接口实现到容器
  60. * @access public
  61. * @param string $abstract 类标识、接口
  62. * @param mixed $concrete 要绑定的类、闭包或者实例
  63. * @return Container
  64. */
  65. public static function set($abstract, $concrete = null)
  66. {
  67. return static::getInstance()->bind($abstract, $concrete);
  68. }
  69. /**
  70. * 绑定一个类、闭包、实例、接口实现到容器
  71. * @access public
  72. * @param string|array $abstract 类标识、接口
  73. * @param mixed $concrete 要绑定的类、闭包或者实例
  74. * @return $this
  75. */
  76. public function bind($abstract, $concrete = null)
  77. {
  78. if (is_array($abstract)) {
  79. $this->bind = array_merge($this->bind, $abstract);
  80. } elseif ($concrete instanceof Closure) {
  81. $this->bind[$abstract] = $concrete;
  82. } elseif (is_object($concrete)) {
  83. $this->instances[$abstract] = $concrete;
  84. } else {
  85. $this->bind[$abstract] = $concrete;
  86. }
  87. return $this;
  88. }
  89. /**
  90. * 绑定一个类实例当容器
  91. * @access public
  92. * @param string $abstract 类名或者标识
  93. * @param object $instance 类的实例
  94. * @return $this
  95. */
  96. public function instance($abstract, $instance)
  97. {
  98. if (isset($this->bind[$abstract])) {
  99. $abstract = $this->bind[$abstract];
  100. }
  101. $this->instances[$abstract] = $instance;
  102. return $this;
  103. }
  104. /**
  105. * 判断容器中是否存在类及标识
  106. * @access public
  107. * @param string $abstract 类名或者标识
  108. * @return bool
  109. */
  110. public function bound($abstract)
  111. {
  112. return isset($this->bind[$abstract]) || isset($this->instances[$abstract]);
  113. }
  114. /**
  115. * 判断容器中是否存在类及标识
  116. * @access public
  117. * @param string $name 类名或者标识
  118. * @return bool
  119. */
  120. public function has($name)
  121. {
  122. return $this->bound($name);
  123. }
  124. /**
  125. * 创建类的实例
  126. * @access public
  127. * @param string $abstract 类名或者标识
  128. * @param array|true $args 变量
  129. * @param bool $newInstance 是否每次创建新的实例
  130. * @return object
  131. */
  132. public function make($abstract, $vars = [], $newInstance = false)
  133. {
  134. if (true === $vars) {
  135. // 总是创建新的实例化对象
  136. $newInstance = true;
  137. $vars = [];
  138. }
  139. if (isset($this->instances[$abstract]) && !$newInstance) {
  140. $object = $this->instances[$abstract];
  141. } else {
  142. if (isset($this->bind[$abstract])) {
  143. $concrete = $this->bind[$abstract];
  144. if ($concrete instanceof Closure) {
  145. $object = $this->invokeFunction($concrete, $vars);
  146. } else {
  147. $object = $this->make($concrete, $vars, $newInstance);
  148. }
  149. } else {
  150. $object = $this->invokeClass($abstract, $vars);
  151. }
  152. if (!$newInstance) {
  153. $this->instances[$abstract] = $object;
  154. }
  155. }
  156. return $object;
  157. }
  158. /**
  159. * 执行函数或者闭包方法 支持参数调用
  160. * @access public
  161. * @param string|array|\Closure $function 函数或者闭包
  162. * @param array $vars 变量
  163. * @return mixed
  164. */
  165. public function invokeFunction($function, $vars = [])
  166. {
  167. $reflect = new ReflectionFunction($function);
  168. $args = $this->bindParams($reflect, $vars);
  169. return $reflect->invokeArgs($args);
  170. }
  171. /**
  172. * 调用反射执行类的方法 支持参数绑定
  173. * @access public
  174. * @param string|array $method 方法
  175. * @param array $vars 变量
  176. * @return mixed
  177. */
  178. public function invokeMethod($method, $vars = [])
  179. {
  180. if (is_array($method)) {
  181. $class = is_object($method[0]) ? $method[0] : $this->invokeClass($method[0]);
  182. $reflect = new ReflectionMethod($class, $method[1]);
  183. } else {
  184. // 静态方法
  185. $reflect = new ReflectionMethod($method);
  186. }
  187. $args = $this->bindParams($reflect, $vars);
  188. return $reflect->invokeArgs(isset($class) ? $class : null, $args);
  189. }
  190. /**
  191. * 调用反射执行callable 支持参数绑定
  192. * @access public
  193. * @param mixed $callable
  194. * @param array $vars 变量
  195. * @return mixed
  196. */
  197. public function invoke($callable, $vars = [])
  198. {
  199. if ($callable instanceof Closure) {
  200. $result = $this->invokeFunction($callable, $vars);
  201. } else {
  202. $result = $this->invokeMethod($callable, $vars);
  203. }
  204. return $result;
  205. }
  206. /**
  207. * 调用反射执行类的实例化 支持依赖注入
  208. * @access public
  209. * @param string $class 类名
  210. * @param array $vars 变量
  211. * @return mixed
  212. */
  213. public function invokeClass($class, $vars = [])
  214. {
  215. $reflect = new ReflectionClass($class);
  216. $constructor = $reflect->getConstructor();
  217. if ($constructor) {
  218. $args = $this->bindParams($constructor, $vars);
  219. } else {
  220. $args = [];
  221. }
  222. return $reflect->newInstanceArgs($args);
  223. }
  224. /**
  225. * 绑定参数
  226. * @access protected
  227. * @param \ReflectionMethod|\ReflectionFunction $reflect 反射类
  228. * @param array $vars 变量
  229. * @return array
  230. */
  231. protected function bindParams($reflect, $vars = [])
  232. {
  233. $args = [];
  234. if ($reflect->getNumberOfParameters() > 0) {
  235. // 判断数组类型 数字数组时按顺序绑定参数
  236. reset($vars);
  237. $type = key($vars) === 0 ? 1 : 0;
  238. $params = $reflect->getParameters();
  239. foreach ($params as $param) {
  240. $name = $param->getName();
  241. $class = $param->getClass();
  242. if ($class) {
  243. $className = $class->getName();
  244. $args[] = $this->make($className);
  245. } elseif (1 == $type && !empty($vars)) {
  246. $args[] = array_shift($vars);
  247. } elseif (0 == $type && isset($vars[$name])) {
  248. $args[] = $vars[$name];
  249. } elseif ($param->isDefaultValueAvailable()) {
  250. $args[] = $param->getDefaultValue();
  251. } else {
  252. throw new InvalidArgumentException('method param miss:' . $name);
  253. }
  254. }
  255. }
  256. return $args;
  257. }
  258. }