zap/injector

一个简单的PHP 5.4+依赖注入器

0.3.0 2014-03-28 16:28 UTC

This package is auto-updated.

Last update: 2024-09-13 11:41:27 UTC


README

Build Status

一个简单的依赖注入库

Zap Injector 旨在在像 Pimple 这样的微型服务定位器提供的简洁性和像 PHP-DI 这样的真实依赖注入库提供的功能之间找到平衡。

为什么我会使用这个?

  • 它按照顺序处理依赖项及其配置的解析。
  • 它允许仅在需要时加载依赖项。
  • 它允许仅在需要时加载配置,并且可以从任何地方进行配置。
  • 如果您在父类或接口下注册,它将模块与其依赖项解耦。

用法

$injector = new \iMarc\Zap\Injector();

// Register a factory under a class or interface name:
$injector->register('Request', function() {
	return new Request();
});

// Or Register a specific instance:
$injector->register(Request::createFromGlobals());

// Or register a class to simply be constructed:
$injector->register('Session');

// Invoke a callable, and Injector will fill in the dependencies:
$returnValue = $injector->invoke(function(Request $req, Session $sess) {
	return array($req, $sess);
});

// Similarly, construct an instance of a class with dependencies:
$instance = $injector->create('some\class');

此外,注入器还有一个 extend 方法。扩展在从工厂或类名构造新实例后立即调用

$injector->extend('Request', function(Request $req) {
	$req->setSomethingImportant(true);
});

变更日志

0.x

0.3.0

  • 主要重构和代码清理。

0.2.0

  • 添加 ->register(),删除 ->addFactory(),->addInstance() 和 ->addClass()
  • 将 ->remove() 重命名为 ->unregister()
  • 其他重构

0.1.1

  • 添加 ->extend() 方法,类似于 Pimple。使用 ->extend() 配置的回调在通过工厂创建实例后调用。

0.1.0

  • 初始发布,充满错误