张柏 burn/samburns/dirty-needle

PHP 的依赖注入容器

3.0.3 2015-03-15 17:50 UTC

This package is auto-updated.

Last update: 2024-08-24 03:47:28 UTC


README

Build Status Coverage Status Scrutinizer Code Quality

dirty-needle

简介

PHP 的依赖注入容器

设置一个返回 PHP 数组的配置文件,例如

<?php
return array(
    'dirty-needle' => array(
        '@dependency' => array(
            'class' => '\Dependency'
        ),
        '@class-with-dependency' => array(
            'class' => '\ClassWithDependency',
            'arguments' => array(
                '@dependency'
            )
        ),
    ),
);

然后从容器中获取东西,如下所示

$diContainer = \DirtyNeedle\DiContainer()::getInstance();
$diContainer->addConfigFile('/path/to/config.php');
$classWithDependency = $diContainer->get('class-with-dependency');

您还可以注入模拟对象

$diContainer->set('service-id', $mockObject);

重置定义和模拟

$diContainer->reset();

版本支持 PHP5.3-5.6,其中 5.6 兼容版本在实现上速度明显更快。

版本控制

本项目使用 语义版本控制