asgard/container

v0.3.1 2016-05-13 11:31 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:13:35 UTC


README

#容器

Build Status

容器为应用程序提供服务。在Asgard框架中,容器通常存储在container变量中。

##安装 如果你正在处理Asgard项目,你不需要安装这个库,因为它已经是标准库的一部分。

composer require asgard/container 0.*

##Asgard框架中的使用

容器通常作为方法参数或通过一个ContainerAware对象访问。你还可以使用单例,但不推荐。

##框架外的使用

$container = new \Asgard\Container\Container;
#or
$container = new \Asgard\Container\Container::singleton();

##注册服务

$container->register('cache', function($container, $param) {
	return new \Cache($param);
});
#or
$container['cache'] = new \Cache($param);
#or
$container->set('cache', new \Cache($param));

注册无持久性的服务

$container->register('cache', function($container, $param) {
	return new \Cache($param);
}, false);

$container['cache']会在每次调用时创建一个新的实例。

##访问服务

$cache = $container->get('cache', [$param]);
#or
$cache = $container['cache'];

如果你多次调用它,容器将确保每次返回相同的实例。

##创建新的服务实例

$cache = $container->make('cache', [$param]);

##检查服务是否存在

$container->has('cache');
#or
isset($container['cache']);

##移除服务

$container->remove('cache');
#or
unset($container['cache']);

##ContainerAware 特性

此特性提供两种方法

  • setContainer($container)
  • getContainer()

以及一个受保护的成员变量 "$container"。

要使用它,在类中添加以下行,紧接在开括号之后

use \Asgard\Container\ContainerAware;

##命令

###ListCommand

显示应用程序中加载的所有服务。

用法

php console services [--defined] [--registered]

--defined: 显示服务在哪里定义

--registered: 显示服务在哪里注册

###贡献

请将所有问题和拉取请求提交到asgardphp/asgard仓库。

许可证

Asgard框架是开源软件,许可协议为MIT许可证