samburns/pimple3-containerinterop

1.3.0 2017-02-13 16:09 UTC

This package is auto-updated.

Last update: 2024-08-24 04:48:17 UTC


README

Build Status Scrutinizer Code Quality

Pimple3-ContainerInterop

简介

Pimple 3 是一个快速、轻量级、流行的 PHP 依赖注入容器。 ContainerInterop 是一个开源标准,用于依赖注入容器之间的互操作性。此工具是符合标准 ContainerInterop 的 Pimple 3 包装器。

它支持 PHP 5.5、5.6 和 7.0。

用户 - 直接 Pimple 访问

包装器通过 __call() 方法和 ArrayAccess 实现允许完全访问所有 Pimple 功能。这允许你直接在包装器上调用 Pimple 中存在的任何方法。你也可以使用 $container['service-id'] = //something 添加服务,因为支持数组访问。

使用 - 服务检索

根据标准,$container->has($serviceId) 会告诉你容器中是否配置了服务。$container->get($serviceId) 将检索服务。如果你调用 get() 但服务不存在,则会抛出 Interop\Container\Exception\NotFoundException 实现的异常。Pimple 的所有其他错误都会导致抛出 Interop\Container\Exception\ContainerException 实例的异常。

使用 - 安装

建议通过 composer 安装。

composer require samburns/pimple3-containerinterop

使用 - 配置

你可以在将 Pimple 容器包装在符合标准的适配器之前对其进行配置。

use Pimple\Container as PimpleContainer;
use SamBurns\Pimple3ContainerInterop\ServiceContainer;

$rawPimpleContainer = new PimpleContainer();
$rawPimpleContainer['service-id'] = function () {return new \Whatever();};
$container = ServiceContainer($rawPimpleContainer);

或者,你可以将你自己的 Pimple\ServiceProviderInterface 实现传入包装器,以应用于内部 Pimple 容器。

use SamBurns\Pimple3ContainerInterop\ServiceContainer;
use My\ServiceProvider;

$container = new ServiceContainer();
$container->addConfig(new ServiceProvider());

如果你想要在单行中启动一个 ServiceContainer 并使用 ServiceProviderInterface 进行配置,甚至有一个命名的构造函数可以使用。

use SamBurns\Pimple3ContainerInterop\ServiceContainer;
use My\ServiceProvider;

$container = ServiceContainer::constructConfiguredWith(new ServiceProvider());

类似项目

PimpleInteropAcclimate 如果你不喜欢这个库,提供了优秀的替代方案。虽然它们使用的是 Pimple v1 而不是 v3,但它们提供了标准中描述为可选的 '代理查找' 功能,允许你组合多个容器。

贡献

欢迎贡献。Fork 仓库,进行更改,并创建一个 pull request。要运行测试,请输入 ./bin/test。将运行 PHPUnit 集成测试和 PHPSpec 单元测试。