Skip to content

Commit

Permalink
服务定义添加数组类型支持
Browse files Browse the repository at this point in the history
    $config = [ /** 数组内容 */ ];
    $container->set('config', $config);
  • Loading branch information
ueaner committed Jul 10, 2017
1 parent 31c4460 commit 63fd887
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Soli Dependency Injection Container

### 注册服务

`服务提供者的格式`,可以是 `匿名函数、对象实例或类名`
`服务提供者的格式`,可以是 `匿名函数、对象实例、类名或数组`

#### 使用匿名函数注册服务

Expand Down Expand Up @@ -64,6 +64,23 @@ Soli Dependency Injection Container

将在获取服务时,返回对应类名的实例化对象。

#### 使用数组注册服务

注:`注册数组类型数据作为服务,仅作为存储,不做任何解析和转换。`

$config = [
'application' => [
'viewsDir' => __DIR__ . '/views/',
'logsDir' => __DIR__ . '/logs/',
'cacheDir' => __DIR__ . '/cache/',
],
// ...
];

$container->set('config', $config);

将在获取服务时,返回注册的数组信息。

#### 获取服务

// 获取服务,调用服务定义,返回服务定义的执行结果
Expand Down
4 changes: 2 additions & 2 deletions src/Di/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static function instance()
* 注册一个服务到容器
*
* @param string $id 服务标识
* @param \Closure|object|string $definition 服务定义
* @param mixed $definition 服务定义
* @return \Soli\Di\ServiceInterface
*/
public function set($id, $definition)
Expand All @@ -73,7 +73,7 @@ public function set($id, $definition)
* 注册单例服务
*
* @param string $id 服务标识
* @param \Closure|object|string $definition 服务定义
* @param mixed $definition 服务定义
* @return \Soli\Di\ServiceInterface
*/
public function setShared($id, $definition)
Expand Down
4 changes: 2 additions & 2 deletions src/Di/ContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface ContainerInterface extends PsrContainerInterface
* 注册一个服务到容器
*
* @param string $id 服务标识
* @param \Closure|object|string $definition 服务定义
* @param mixed $definition 服务定义
* @return \Soli\Di\ServiceInterface
*/
public function set($id, $definition);
Expand All @@ -24,7 +24,7 @@ public function set($id, $definition);
* 注册单例服务
*
* @param string $id 服务标识
* @param \Closure|object|string $definition 服务定义
* @param mixed $definition 服务定义
* @return \Soli\Di\ServiceInterface
*/
public function setShared($id, $definition);
Expand Down
8 changes: 6 additions & 2 deletions src/Di/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class Service implements ServiceInterface
protected $id;

/**
* 服务定义, Closure|对象实例|类名
* 服务定义, Closure|对象实例|类名|数组
*
* @var \Closure|object|string
* @var \Closure|object|string|array
*/
protected $definition;

Expand Down Expand Up @@ -106,6 +106,10 @@ public function resolve(array $parameters = null, ContainerInterface $container
// 已存在的类名
$instance = $this->createInstanceFromClassName($definition, $parameters);
break;
case 'array':
// 数组,仅存储
$instance = $definition;
break;
default:
throw new \DomainException("Service '{$this->id}' cannot be resolved");
}
Expand Down
5 changes: 0 additions & 5 deletions tests/Di/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ public function testInstanceInjection()
$this->assertInstanceOf($this->myComponent, $service);
}

/**
* 不支持注册数组
*
* @expectedException \Exception
*/
public function testArrayInjection()
{
$array = [
Expand Down
5 changes: 0 additions & 5 deletions tests/Di/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ public function testResolveObjectInstance()
$this->assertInstanceOf('\stdClass', $a);
}

/**
* 不支持注册数组
*
* @expectedException \Exception
*/
public function testResolveArray()
{
$arr = [1, 2];
Expand Down

0 comments on commit 63fd887

Please sign in to comment.