av/bonefish-di

该包已被废弃,不再维护。作者建议使用av/bonefish-injection包。
此包的最新版本(v1.0)没有可用的许可信息。

本项目维护Bonefish依赖注入容器

v1.0 2014-09-27 12:53 UTC

This package is not auto-updated.

Last update: 2019-02-20 18:10:44 UTC


README

Scrutinizer Code Quality Code Coverage Build Status

Bonefish-DI是一个非常简单且小巧的依赖注入容器。

特性

  • 无外部依赖
  • 使用@inject注解注入服务
  • 默认实现懒加载依赖注入

安装

请使用Composer安装此包。

$ composer require av/bonefish-di:dev-master

使用

简单创建带有注入的实例,无需将其添加到容器中

// Create an Object and inject all Services
$container = new Bonefish\DependencyInjection\Container();
$foo = $container->create('\Some\Random\Class');
// or with parameters
$bar = $container->create('\Some\Random\Class',array('bar','baz'));

创建一个新的服务并将其保存在容器中

// Create a service, no parameters here
$container = new Bonefish\DependencyInjection\Container();
$service = $container->get('\Some\Random\Service');

您还可以创建对象,稍后将其添加到容器中以用作服务

// Create an Object and inject all Services
$container = new Bonefish\DependencyInjection\Container();
$bar = $container->create('\Some\Random\Class',array('bar','baz'));
$container->add('\Some\Random\Class',$bar);

您还可以定义别名

// Create an Object and inject all Services
$container = new Bonefish\DependencyInjection\Container();
$service = $container->get('\Some\Random\Service');
$container->alias('Alias','\Some\Random\Service');
// This will now return \Some\Random\Service
$service2 = $container->get('Alias');

您还可以检查别名是否已设置,销毁整个容器并列出此容器中的所有服务。