定制 dc/ioc 二次开发

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

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

dc/ioc

Composer 安装命令:

composer require dc/ioc

包简介

Simple IoC container

README 文档

README

DC\IoC - IoC container for PHP

Installation

$ composer install dc/ioc

Or add it to composer.json:

"require": {
	"dc/ioc": "0.*"
}
$ composer install

Registering

You can register objects, classes and factory functions with the container.

Registering a class:

$container = new \DC\IoC\Container();
$container
	->register('\Car') // fully qualified namespace
    ->to('\IVehicle')
    ->withContainerLifetime(); // lives as long as the container

Registering an instance (instance registrations do not support lifetimes, they always live as long as the container does):

$container
	->register(new \Car())
	->to('\IVehicle');

Registering a factory function:

$container
	->register(function() {
		new \Car();
	})->to('\IVehicle');

Understanding lifetimes

There are 3 possible lifetime registrations: withPerResolveLifetime(), withContainerLifetime() and withSingletonLifetime(). You can use these when you pass a class name or factory function to register(), but not if you pass an instance of an object.

  • withPerResolveLifetime() will create a new instance every time you resolve it.
  • withContainerLifetime() will resolve the same instance from that container.
  • withSingletonLifetime() will resolve the same instance from all containers.

Resolving services

You can resolve services by querying the container directly:

$vehicle = $container->resolve('\IVehicle');

If you have multiple registrations for the same interface or class name, you can use resolveAll().

$vehicles = $container->resolveAll('\IVehicle');

Constructor injection

Use type hints to inject a dependency into a class.

class Trailer {
	public __construct(\IVehicle $vehicle) {
		// attach your Trailer to you IVehicle, or something
	}
}

$container->resolve("Trailer"); // you don't even have to register Trailer

If you don't want to use type hints, you can use PhpDoc comments instead. This is particularly useful when you want to get an array of something, as this is not supported by PHPs type-hinting syntax.

class Trailer {
    /**
     * @param $matchesVehicles array|\IVehicle[]
     */
	public __construct(array $matchesVehicles) {

	}
}

Or even simpler:

class Trailer {
    /**
     * @param $matchesVehicles \IVehicle[]
     */
	public __construct($matchesVehicles) {

	}
}

Property injection

All objects resolved through the container will have their non-static properties scanned for possible dependencies. To mark a property as injectable, it needs both a @var and a @inject PhpDoc declaration:

class Car {
   /**
    * @inject
    * @var \Trailer
    */
   private $trailer;
}

For objects that haven't been resolved through the container (meaning objects you have constructed yourself), you can apply property injection using the inject() method:

$car = new Car();
$container->inject($car);

Factory injection

When you provide a factory function to registration, you can have other services injected as in a constructor. You can as before, either rely on type hints, or on PhpDoc comments (for injecting arrays).

$container->register(function(\IFoo $foo) {
    return new Bar($foo);
})->to('\Bar');
$container->register(
	/**
     * @param $foos \IFoo[]
     */
	function(array $foos) {
		return new Bar($foos);
	})->to('\Bar');

Modules

If you have a large project that needs to register a lot of services, implementing a module may be the way to go.

Extend \DC\IoC\Modules\Module and specify a name for your module and which modules it depends on.

class MyModule extends \DC\IoC\Modules\Module {
    public __construct() {
        parent::__construct("package-name", ["dc/router"]);
    }
    
    public register(\DC\IoC\Container $container) {
        $container->register('\Foo')->to('\Bar');
    }
}

When (preferably before) registering your services, register all the modules first:

$container->registerModules([new MyModule(), new \DC\Router\Module()]);

The container will try to register any dependencies in the correct order.

Performance

Some crude performance tests can be found in the perf/ folder of this repos. They show the following (numbers from a VM running on my machine):

  • A single registration uses about 600 bytes of memory, and takes 0.03 ms. Unless you are registering thousands of services, speed and memory should not be a limiting factor. For a typical setup with less than a hundred registrations per page view, you should expect it to add less than 25 ms to your bottom line in most cases.
  • Resolves are negligible. A resolveAll-call that returns 10 objects, takes on average 0.09 ms (to resolve 10 objects).
  • Constructor and function injection is done using reflection, which is often assumed to be slow. Again, to resolve a single interface takes 0.06 ms.

dc/ioc 适用场景与选型建议

dc/ioc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 170 次下载、GitHub Stars 达 5, 最近一次更新时间为 2014 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「dependency injection」 「di」 「inversion of control」 「ioc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 dc/ioc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dc/ioc 我们能提供哪些服务?
定制开发 / 二次开发

基于 dc/ioc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 170
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 8
  • 依赖项目数: 6
  • 推荐数: 10

GitHub 信息

  • Stars: 5
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-03-23