定制 crysalead/box 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

crysalead/box

最新稳定版本:2.0.2

Composer 安装命令:

composer require crysalead/box

包简介

Minimalist Dependency Injection Container.

README 文档

README

Build Status Code Coverage

Box is dependency injection container which manage dependencies based on closure definitions only. This approch has the particularity to be simple, easy and flexible. It also support lazy dependencies resolution using wrappers.

API

Creating a Dependency container

$box = new Box();

Setting up a service

To to share a unique service over your application use Box::service(). A service can be a class, an instance, a string or any kind of value.

Example:

$box->service('foo', new MyClass());

Each shared service defined with Box::service() can be retreived using Box::get().

Note: If a share is defined using a closure, the closure will be executed once and the result will be returned for all next Box::get() calls.

Setting up a factory

Use Box::factory() to setup a factory. It can be a closure:

use MyClass;

$box->factory('foo', function($param1, $param2) {
	return new MyClass($param1, $param2);
});

Or a fully-namespaced class name.

Box::factory('foo', 'otherNamespace\MyClass');

Box::factory() will create an new instance of the definition when resolved using Box::get().

Resolving a dependency

To resolve a dependency, use Box::get():

$box->get('foo', $param1, $param2);

All $paramX are optional parameters passed to the closure or directly to the constructor if the definition is a fully-namespaced class name string.

Returing a wrapped dependency

Wrapping a dependency has the advantage to allow to inject a dependency without resolving it directly. To be able to lazily resolve a dependency you need to use Box::wrap():

$box->wrap('foo', $param1, $param2);

All $paramX are optional parameters passed to the closure or directly to the constructor if the definition is a fully-namespaced class name string.

Then dependency is resolved by doing:

$dependency = $wrapper->get($param1, $param2);

All $paramX are optional and will overrides the ones setted at the wrapping step.

Cleanup

Use Box::remove('foo') to remove a specific dependency or Box::clear() to remove all dependencies.

Global API

You can use the box() function to get/set a DI anywhere in your code.

Setter

$box = box('mynamespace', new Box());

Getter

$box = box('mynamespace');

Unsetting a DI

$box = box('mynamespace', false);

Clear everything.

$box = box(false);

统计信息

  • 总下载量: 30.38k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-12-21