From 035d2c174f0378538aff03f460cdebafb375bd85 Mon Sep 17 00:00:00 2001 From: YouJiaXing <287009007@qq.com> Date: Fri, 20 Jul 2018 15:35:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=A1=8C=E4=B8=BA=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit + 容器的数组形式使用时不再仅针对 raw + 提供 getInstance() 获取单例 --- src/Container.php | 53 ++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/Container.php b/src/Container.php index beb96c6..89ba4e6 100644 --- a/src/Container.php +++ b/src/Container.php @@ -11,11 +11,13 @@ class Container implements ContainerInterface, \ArrayAccess { + protected static $instance; + /** * 保存 参数, 已实例化的对象 * @var array */ - private $instance = []; + private $instances = []; private $shared = []; @@ -29,6 +31,25 @@ class Container implements ContainerInterface, \ArrayAccess */ private $binding = []; + + public static function setInstance(ContainerInterface $c = null) + { + return static::$instance = $c; + } + + /** + * @return Container + */ + public static function getInstance() + { + if (is_null(static::$instance)) { + static::$instance = new static; + } + + return static::$instance; + } + + public function __construct() { $this->raw('Psr\Container\ContainerInterface', $this); @@ -56,8 +77,8 @@ public function get($id, $parameters = [], $shared=false) return $this->raw[$id]; } - if (array_key_exists($id, $this->instance)) { - return $this->instance[$id]; + if (array_key_exists($id, $this->instances)) { + return $this->instances[$id]; } $define = array_key_exists($id, $this->binding) ? $this->binding[$id] : $id; @@ -82,7 +103,7 @@ public function get($id, $parameters = [], $shared=false) } if ($shared || (isset($this->shared[$id]) && $this->shared[$id])) { - $this->instance[$id] = $instance; + $this->instances[$id] = $instance; } return $instance; } @@ -310,7 +331,7 @@ protected function getReflectionFunction($name) */ public function has($id) { - $has = array_key_exists($id, $this->binding) || array_key_exists($id, $this->raw) || array_key_exists($id, $this->instance); + $has = array_key_exists($id, $this->binding) || array_key_exists($id, $this->raw) || array_key_exists($id, $this->instances); if (!$has) { $reflectionClass = $this->getReflectionClass($id, true); if (!empty($reflectionClass)) { @@ -322,22 +343,22 @@ public function has($id) public function needResolve($id) { - return !(array_key_exists($id, $this->raw) && (array_key_exists($id, $this->instance) && $this->shared[$id])); + return !(array_key_exists($id, $this->raw) && (array_key_exists($id, $this->instances) && $this->shared[$id])); } public function keys() { - return array_unique(array_merge(array_keys($this->raw), array_keys($this->binding), array_keys($this->instance))); + return array_unique(array_merge(array_keys($this->raw), array_keys($this->binding), array_keys($this->instances))); } public function instanceKeys() { - return array_unique(array_keys($this->instance)); + return array_unique(array_keys($this->instances)); } public function _unset($id) { - unset($this->shared[$id], $this->binding[$id], $this->raw[$id], $this->instance[$id], $this->params[$id]); + unset($this->shared[$id], $this->binding[$id], $this->raw[$id], $this->instances[$id], $this->params[$id]); } public function remove($id) @@ -404,7 +425,6 @@ public function batchSet(array $data, $shared=false) } /** - * 仅针对 raw * Whether a offset exists * @link http://php.net/manual/en/arrayaccess.offsetexists.php * @param mixed $offset

@@ -418,11 +438,10 @@ public function batchSet(array $data, $shared=false) */ public function offsetExists($offset) { - return array_key_exists($offset, $this->raw); + return $this->has($offset); } /** - * 仅针对 raw * Offset to retrieve * @link http://php.net/manual/en/arrayaccess.offsetget.php * @param mixed $offset

@@ -433,11 +452,10 @@ public function offsetExists($offset) */ public function offsetGet($offset) { - return array_key_exists($offset, $this->raw) ? $this->raw[$offset] : null; + return $this->get($offset); } /** - * 仅针对 raw * Offset to set * @link http://php.net/manual/en/arrayaccess.offsetset.php * @param mixed $offset

@@ -451,11 +469,10 @@ public function offsetGet($offset) */ public function offsetSet($offset, $value) { - $this->raw($offset, $value); + $this->set($offset, $value); } /** - * 仅针对 raw * Offset to unset * @link http://php.net/manual/en/arrayaccess.offsetunset.php * @param mixed $offset

@@ -466,8 +483,6 @@ public function offsetSet($offset, $value) */ public function offsetUnset($offset) { - if (array_key_exists($offset, $this->raw)) { - $this->_unset($offset); - } + $this->_unset($offset); } } \ No newline at end of file