Skip to content

Commit

Permalink
优化容器代码 (#361)
Browse files Browse the repository at this point in the history
* 为容器新增 newInstance() 方法

* 更新文档

* 更新文档

* 优化代码

* 删除代码

* 修复测试
  • Loading branch information
Yurunsoft authored Jun 25, 2022
1 parent ff8bfc1 commit 1c2130b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
15 changes: 12 additions & 3 deletions doc/core/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,11 @@ App::getContainer()->bind('aaa', XXX::class);
// 绑定带参数,非单例模式,禁用递归依赖
App::getContainer()->bind('aaa', XXX::class, \Imi\Bean\Annotation\Bean::INSTANCE_TYPE_EACH_NEW, false);

// 实例化
// 实例化,带缓存
$obj = App::getBean('aaa');

// 实例化,带参数,不缓存
$obj = App::getBean('aaa', 1);
```

服务器容器:
Expand All @@ -132,8 +135,11 @@ ServerManager::getServer()->getContainer()->bind('aaa', XXX::class);
// 绑定带参数,非单例模式,禁用递归依赖
ServerManager::getServer()->getContainer()->bind('aaa', XXX::class, \Imi\Bean\Annotation\Bean::INSTANCE_TYPE_EACH_NEW, false);

// 实例化
// 实例化,带缓存
$obj = ServerManager::getServer()->getContainer()->getBean('aaa');

// 实例化,带参数,不缓存
$obj = ServerManager::getServer()->getContainer()->getBean('aaa', 1);
```

请求上下文容器:
Expand All @@ -147,8 +153,11 @@ RequestContext::getContainer()->bind('aaa', XXX::class);
// 绑定带参数,非单例模式,禁用递归依赖
RequestContext::getContainer()->bind('aaa', XXX::class, \Imi\Bean\Annotation\Bean::INSTANCE_TYPE_EACH_NEW, false);

// 实例化
// 实例化,带缓存
$obj = RequestContext::getBean('aaa');

// 实例化,带参数,不缓存
$obj = RequestContext::getBean('aaa', 1);
```

> 禁用递归依赖可以规避服务启动后,第一次访问概率报错问题
Expand Down
23 changes: 9 additions & 14 deletions src/Bean/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,19 @@ public function __construct(array $binds = [])
/**
* 从容器中获取实例对象,如果不存在则实例化.
*
* @param string $id 标识符
* @param string $id 标识符
* @param mixed ...$params
*
* @throws \Psr\Container\NotFoundExceptionInterface 没有找到对象
* @throws \Psr\Container\ContainerExceptionInterface 检索时出错
*
* @return mixed entry
*/
public function get(string $id)
public function get(string $id, ...$params)
{
$object = null;
// 实现传递实例化参数
$params = \func_get_args();
// 单例中有数据,且无实例化参数时直接返回单例
$beanObjects = &$this->beanObjects;
if (isset($beanObjects[$id]) && 1 === \func_num_args())
if (isset($beanObjects[$id]) && empty($params))
{
return $beanObjects[$id];
}
Expand All @@ -57,7 +55,7 @@ public function get(string $id)
throw new ContainerException('$id can not be a empty string value');
}

unset($params[0]);
$object = null;

$binds = $this->binds;
$originId = $id;
Expand Down Expand Up @@ -140,16 +138,14 @@ public function get(string $id)
*
* 此方法实例化的对象,AOP、注解等都对它不产生作用
*
* @param string $id 标识符
* @param string $id 标识符
* @param mixed ...$params
*
* @throws \Psr\Container\NotFoundExceptionInterface 没有找到对象
* @throws \Psr\Container\ContainerExceptionInterface 检索时出错
*/
public function getSingleton(string $id): object
public function getSingleton(string $id, ...$params): object
{
$object = null;
// 实现传递实例化参数
$params = \func_get_args();
// 单例中有数据,且无实例化参数时直接返回单例
$singletonObjects = &$this->singletonObjects;
if (isset($singletonObjects[$id]) && 1 === \func_num_args())
Expand All @@ -162,8 +158,7 @@ public function getSingleton(string $id): object
throw new ContainerException('$id can not be a empty string value');
}

unset($params[0]);

$object = null;
$binds = $this->binds;

while (true)
Expand Down

0 comments on commit 1c2130b

Please sign in to comment.