ehough/iconic

此包已被放弃且不再维护。未建议替代包。

与PHP 5.2+兼容的Symfony依赖注入组件的分支。

v2.5.4 2014-09-24 18:21 UTC

This package is not auto-updated.

Last update: 2020-01-24 14:48:04 UTC


README

Build Status Project Status: Unsupported - The project has reached a stable, usable state but the author(s) have ceased all work on it. A new maintainer may be desired. Latest Stable Version License

此库不再受支持或维护,因为PHP 5.2的使用率最终降至10%以下

Symfony的依赖注入组件的分支,兼容PHP 5.2+。

动机

Symfony的依赖注入组件是一个强大的DI库,但它只与PHP 5.3+兼容。虽然97%的PHP服务器运行PHP 5.2或更高版本,但32%的所有服务器仍在运行PHP 5.2或更低版本 (来源)。如果仅仅因为版本不兼容,就排除这个库几乎三分之一的全球服务器,那就太可惜了。

Symfony的依赖注入组件的区别

主要区别是Symfony类的命名约定。而不是使用\Symfony\Component\DependencyInjection命名空间(及其子命名空间),将Symfony类的名称以ehough_iconic为前缀,并遵循PEAR命名约定

类命名转换的几个示例

\Symfony\Component\DependencyInjection\ContainerBuilder           ----->    ehough_iconic_ContainerBuilder
\Symfony\Component\DependencyInjection\Compiler\Compiler          ----->    ehough_iconic_compiler_Compiler
\Symfony\Component\DependencyInjection\ParameterBag\ParameterBag  ----->    ehough_iconic_parameterbag_ParameterBag

使用iconic而不是Symfony的依赖注入组件时的一些其他注意事项

  • 大多数加载器和转储器只能与PHP 5.3+一起使用
  • 表达式语言功能仅适用于PHP 5.3+

使用方法

以下是一个简单示例,说明如何注册服务和参数

$sc = new ehough_iconic_ContainerBuilder();
$sc
    ->register('foo', '%foo.class%')
    ->addArgument(new ehough_iconic_Reference('bar'))
;
$sc->setParameter('foo.class', 'Foo');

$sc->get('foo');

方法调用(设置注入)

$sc = new ehough_iconic_ContainerBuilder();

$sc
    ->register('bar', '%bar.class%')
    ->addMethodCall('setFoo', array(new ehough_iconic_Reference('foo')))
;
$sc->setParameter('bar.class', 'Bar');

$sc->get('bar');

工厂类

如果你的服务是通过调用静态方法来检索的

$sc = new ehough_iconic_ContainerBuilder();

$sc
    ->register('bar', '%bar.class%')
    ->setFactoryClass('%bar.class%')
    ->setFactoryMethod('getInstance')
    ->addArgument('Aarrg!!!')
;
$sc->setParameter('bar.class', 'Bar');

$sc->get('bar');

文件包含

对于某些服务,特别是那些难以或无法自动加载的服务,你可能需要在实例化你的类之前让容器包含一个文件。

$sc = new ehough_iconic_ContainerBuilder();

$sc
    ->register('bar', '%bar.class%')
    ->setFile('/path/to/file')
    ->addArgument('Aarrg!!!')
;
$sc->setParameter('bar.class', 'Bar');

$sc->get('bar');

版本和版本控制

版本与上游Symfony存储库同步。例如,ehough/iconic v2.3.2已合并来自symfony/DependencyInjection v2.3.2的代码。