Skip to content

Commit

Permalink
使用行为修改
Browse files Browse the repository at this point in the history
+ 容器的数组形式使用时不再仅针对 raw
+ 提供 getInstance() 获取单例
  • Loading branch information
youjiaxing committed Jul 20, 2018
1 parent e2dfe0c commit 035d2c1
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

class Container implements ContainerInterface, \ArrayAccess
{
protected static $instance;

/**
* 保存 参数, 已实例化的对象
* @var array
*/
private $instance = [];
private $instances = [];

private $shared = [];

Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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)
Expand Down Expand Up @@ -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 <p>
Expand All @@ -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 <p>
Expand All @@ -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 <p>
Expand All @@ -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 <p>
Expand All @@ -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);
}
}

0 comments on commit 035d2c1

Please sign in to comment.