Skip to content

Commit

Permalink
删除 setShared()/getShared() 方法,set() 方法默认使用共享实例
Browse files Browse the repository at this point in the history
  • Loading branch information
ueaner committed May 15, 2018
1 parent 57c272f commit a4ff221
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 176 deletions.
2 changes: 1 addition & 1 deletion examples/Soli/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __get($name)
$container = $this->getContainer();

if ($container->has($name)) {
$service = $container->getShared($name);
$service = $container->get($name);
// 将找到的服务添加到属性, 以便下次直接调用
$this->$name = $service;
return $service;
Expand Down
12 changes: 4 additions & 8 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@

$app = new \Soli\ExampleApp();

// 使用访问对象属性的方式,访问容器服务,同 $container->getShared() 方法
// 使用访问对象属性的方式,访问容器服务,同 $container->get() 方法
$service = $app->testService;
var_dump($service);

// 使用访问对象属性的方式,访问容器服务,同 $container->getShared() 方法
// 使用访问对象属性的方式,访问容器服务,同 $container->get() 方法
$service = $app->container->testService;
var_dump($service);

// 数组访问方式获取 testService 服务,同 $container->getShared() 方法
// 数组访问方式获取 testService 服务,同 $container->get() 方法
$service = $app->container['testService'];
var_dump($service);

// 使用 get 方法获取 testService 服务,
// 由 TestServiceProvider 对象的 defer 属性决定是否每次都执行 register() 方法
// 由 TestServiceProvider 对象的 defer 属性决定是否延迟执行 register() 方法
$service = $app->container->get('testService');
var_dump($service);

// TestServiceProvider 服务的共享实例,只执行一次 register() 方法
$service = $app->container->getShared('testService');
var_dump($service);

//$service = $app->container->get('redis');
//var_dump($service);
95 changes: 30 additions & 65 deletions src/Di/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,14 @@ class Container implements ContainerInterface, \ArrayAccess
*
* @var \Soli\Di\ServiceInterface[]
*/
protected static $services = [];
protected $services = [];

/**
* The container's instances.
* 存储共享服务实例
*
* @var array
*/
protected $instances = [];

/**
* 存储 getShared 方法返回的服务实例(服务定义的执行结果)
*
* @var array
*/
protected static $sharedInstances = [];
protected $sharedInstances = [];

/**
* 初始化容器默认实例
Expand Down Expand Up @@ -67,26 +60,13 @@ public static function instance()
*
* @param string $id 服务标识
* @param mixed $definition 服务定义
* @param bool $shared 是否为共享实例,默认为共享实例
* @return \Soli\Di\ServiceInterface
*/
public function set($id, $definition)
{
$service = new Service($id, $definition, false);
static::$services[$id] = $service;
return $service;
}

/**
* 注册单例服务
*
* @param string $id 服务标识
* @param mixed $definition 服务定义
* @return \Soli\Di\ServiceInterface
*/
public function setShared($id, $definition)
public function set($id, $definition, $shared = true)
{
$service = new Service($id, $definition, true);
static::$services[$id] = $service;
$service = new Service($id, $definition, $shared);
$this->services[$id] = $service;
return $service;
}

Expand All @@ -101,12 +81,17 @@ public function setShared($id, $definition)
*/
public function get($id, array $parameters = [])
{
if (isset(static::$services[$id])) {
// 如果是共享实例已解析,则返回
if (isset($this->sharedInstances[$id])) {
return $this->sharedInstances[$id];
}

if (isset($this->services[$id])) {
/** @var \Soli\Di\ServiceInterface $service 服务实例 */
$service = static::$services[$id];
$service = $this->services[$id];
} elseif (class_exists($id)) {
// 自动将类名注册为服务
$service = $this->setShared($id, $id);
$service = $this->set($id, $id);
} else {
throw new \InvalidArgumentException("Service '$id' wasn't found in the dependency injection container");
}
Expand All @@ -119,32 +104,12 @@ public function get($id, array $parameters = [])
$instance->setContainer($this);
}

return $instance;
}

/**
* 获取单例服务
*
* 当一个服务未被注册为单例服务,使用此方法也可以获取单例服务
*
* @param string $id 服务标识
* @param array $parameters 参数
* @return mixed
*/
public function getShared($id, array $parameters = [])
{
// 检查是否已解析
if (isset(static::$sharedInstances[$id])) {
return static::$sharedInstances[$id];
// 保存到共享实例列表
if ($service->isShared()) {
$this->sharedInstances[$id] = $instance;
}

// 解析服务实例
$service = $this->get($id, $parameters);

// 保存到 shared 实例列表
static::$sharedInstances[$id] = $service;

return $service;
return $instance;
}

/**
Expand All @@ -155,7 +120,7 @@ public function getShared($id, array $parameters = [])
*/
public function has($id)
{
return isset(static::$services[$id]);
return isset($this->services[$id]);
}

/**
Expand All @@ -166,8 +131,8 @@ public function has($id)
*/
public function remove($id)
{
unset(static::$services[$id]);
unset(static::$sharedInstances[$id]);
unset($this->services[$id]);
unset($this->sharedInstances[$id]);
}

/**
Expand All @@ -177,8 +142,8 @@ public function remove($id)
*/
public function clear()
{
static::$services = [];
static::$sharedInstances = [];
$this->services = [];
$this->sharedInstances = [];
}

/**
Expand All @@ -190,8 +155,8 @@ public function clear()
*/
public function getService($id)
{
if (isset(static::$services[$id])) {
return static::$services[$id];
if (isset($this->services[$id])) {
return $this->services[$id];
}

throw new \InvalidArgumentException("Service '$id' wasn't found in the dependency injection container");
Expand All @@ -204,7 +169,7 @@ public function getService($id)
*/
public function getServices()
{
return static::$services;
return $this->services;
}

// 实现 \ArrayAccess 方法
Expand All @@ -216,12 +181,12 @@ public function offsetExists($id)

public function offsetGet($id)
{
return $this->getShared($id);
return $this->get($id);
}

public function offsetSet($id, $definition)
{
$this->setShared($id, $definition);
$this->set($id, $definition);
}

public function offsetUnset($id)
Expand All @@ -241,6 +206,6 @@ public function offsetUnset($id)
*/
public function __get($id)
{
return $this->getShared($id);
return $this->get($id);
}
}
22 changes: 2 additions & 20 deletions src/Di/ContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,10 @@ interface ContainerInterface extends PsrContainerInterface
*
* @param string $id 服务标识
* @param mixed $definition 服务定义
* @param bool $shared
* @return \Soli\Di\ServiceInterface
*/
public function set($id, $definition);

/**
* 注册单例服务
*
* @param string $id 服务标识
* @param mixed $definition 服务定义
* @return \Soli\Di\ServiceInterface
*/
public function setShared($id, $definition);
public function set($id, $definition, $shared);

/**
* 从容器中获取一个服务
Expand All @@ -39,16 +31,6 @@ public function setShared($id, $definition);
*/
public function get($id);

/**
* 获取单例服务
*
* 当一个服务未被注册为单例服务,使用此方法也可以获取单例服务
*
* @param string $id 服务标识
* @return mixed
*/
public function getShared($id);

/**
* 查询容器中是否存在某个服务
*
Expand Down
21 changes: 2 additions & 19 deletions src/Di/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ class Service implements ServiceInterface
*/
protected $shared = false;

/**
* 存储共享服务实例(即服务定义的执行结果)
*
* @var mixed
*/
protected $sharedInstance;

/**
* 传入的参数
*
Expand All @@ -68,11 +61,11 @@ public function __construct($id, $definition, $shared = false)
{
$this->id = $id;
$this->definition = $definition;
$this->shared = (bool)$shared;
$this->shared = $shared;
}

/**
* 检查服务是否为共享的
* 服务是否为共享的
*
* @return bool
*/
Expand All @@ -91,11 +84,6 @@ public function isShared()
*/
public function resolve(array $parameters = [], ContainerInterface $container = null)
{
// 为 shared 服务且解析过则直接返回实例
if ($this->shared && $this->sharedInstance !== null) {
return $this->sharedInstance;
}

$this->parameters = $parameters;
$this->container = $container;

Expand Down Expand Up @@ -126,11 +114,6 @@ public function resolve(array $parameters = [], ContainerInterface $container =
throw new \DomainException("Service '{$this->id}' cannot be resolved");
}

// 如果是 shared, 保存实例
if ($this->shared) {
$this->sharedInstance = $instance;
}

return $instance;
}

Expand Down
7 changes: 7 additions & 0 deletions src/Di/ServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ interface ServiceInterface
* @return mixed
*/
public function resolve(array $parameters = [], ContainerInterface $container = null);

/**
* 服务是否为共享的
*
* @return bool
*/
public function isShared();
}
Loading

0 comments on commit a4ff221

Please sign in to comment.