mouf / interop.symfony.di
此软件包包含了一个可扩展的 Symfony 2 DI 容器实现,可以使用其他 DI 容器(来自其他框架)进行扩展。
Requires
- php: >=5.3.3
- symfony/dependency-injection: ~2.3
README
此软件包包含了一个扩展 ExtensibleContainer
类,该类扩展了 Symfony 2 的 Container
类。扩展后的类将允许您向 Symfony 2 的容器中添加额外的依赖注入容器(DIC)。
这意味着当您开发一个 Symfony 2 应用程序时,您不再被迫只能使用 Symfony 的 DIC。现在您可以添加任何您想要的 DIC!
它如何工作?
为了使这个功能生效,您必须将 Symfony2 容器放入一个复合容器中,该复合容器将包含其他容器。在 Acclimate 项目中有一个出色的 CompositeContainer
实现。
典型的工作流程如下所示
路由器(或其他任何组件)将请求实例传递给 ExtensibleContainer
。ExtensibleContainer
将调用转发到 CompositeContainer
,然后 CompositeContainer
将调用转发到 CompositeContainer
中的任何其他容器。要进入 CompositeContainer
,容器必须遵守在 container-interop 项目 中定义的 ContainerInterface
。
安装
ExtensibleContainer
以 Composer 包 的形式分发。您通常还需要 Acclimate(用于复合容器)和其他容器。
{
// Let's replace container-interop as long as the ParentAwareContainerInterface is not adopted:
"repositories" : [
{
"type" : "vcs",
"url" : "git@github.com:moufmouf/container-interop.git"
}
],
require: {
... the dependencies to Symfony go here ...
"mouf/interop.symfony.di" : "2.3.*",
"acclimate/container" : "~1.0",
// As long as the ParentAwareContainerInterface is not adopted by container-interop, you need this:
"container-interop/container-interop" : "dev-parentaware as 1.0.0",
// You will also need dependencies to the containers you want to use, for instance:
"mouf/mouf" : "~2.0"
}
}
在您的 app/AppKernel.php
文件中,添加以下两个方法
... class AppKernel extends Kernel { ... /** * Gets the container's base class. * We use this to make Symfony use the ExtensibleContainer. * * @return string */ protected function getContainerBaseClass() { return 'Mouf\\Symfony\\Component\\DependencyInjection\\ExtensibleContainer'; } /** * Initializes the service container. * * Use this method to initialize your own DI container and register it * in Symfony DI container. */ protected function initializeContainer() { parent::initializeContainer(); // Here, you can access the Symfony container using $this->container and register // your own container in it. $compositeContainer = new CompositeContainer(); // The SF2 container does not implement the ContainerInterface interface // Therefore, it needs to be "acclimated". $acclimator = new ContainerAcclimator(); $sfContainer = $this->container; $sfContainer->setParentContainer($compositeContainer); $acclimatedSfContainer = $acclimator->acclimate($this->container); $compositeContainer->addContainer($acclimatedSfContainer); // Now, let's add other containers. // They must implement the ContainerInterface (and optionnally the ParentAwareContainerInterface) $compositeContainer->addContainer(MoufManager::getMoufManager()); } }
您的 DI 容器必须遵守由 container-interop 项目提供的 ContainerInterface
。
我可以将哪些 DI 容器插入到 Symfony 中?
默认情况下,您可以使用这些 DI 容器,因为它们遵守 ContainerInterface
接口
- Mouf2 (http://mouf-php.com)
- PHP-DI 4.1+ (https://github.com/auraphp/Aura.Di)
- PimpleInterop (https://github.com/moufmouf/pimple-interop,Pimple 的包装器)
但是等等!多亏了 Jeremy Lindblom 和其出色的 Acclimate 包,您现在几乎可以将任何依赖注入容器插入其中,并获取对该容器的适配器,该适配器遵守 ContainerInterface
接口。