mouf/container-installer
Composer 安装命令:
composer require mouf/container-installer
包简介
This package contains a PHP dependency injection container detector and aggregator. The idea is to have potentially one DI container per composer package and to aggregate all those containers into a 'root' container.
README 文档
README
This project is a test project developed as a proof of concept while working on the ContainerInterop project.
The big picture
The ultimate goal is to allow the application developer to easily create a "root container", that can automatically detect and add containers contained in other packages into a global composite container that can be used by the application.
Compared to the classical way of thinking about a web application, this is a paradigm shift.
In a "classical" application, packages added to the application may add new instances to the main and only DI container. This is what SF2 bundles, ZF2 modules or Mouf2 packages are doing.
Using this approach, each package provides its own DI container that contains instances. DI containers are added to a global container that is queried.
About this package
The goal of this package is simply to provide an easy way for application developers to detect DI containers that might be declared in Composer packages they use.
This project adds an additional step to Composer "install", just after the Composer dumps the autoloader. The Container-Installer will scan all packages composer.json files and will look for a section like:
{
"extra": {
"container-interop": {
"container-factory": "My\\ContainerFactory::getContainer"
}
}
}
This section actually declares container factories that can be bundled in the package.
The "container-factory" parameter must point to a function or a static method that returns the container.
Here is a sample implementation:
class ContainerFactory { private static $container; /** * This method is returning a configured container * * @param ContainerInterface $rootContainer * @return ContainerInterface */ public static function getContainer(ContainerInterface $rootContainer) { if (!$this->container) { // Delegate dependencies fetching to the root container. $this->container = new Picotainer([ "hello" => function(ContainerInterface $container) { return array('hello' => $container->get('world')); } ], $rootContainer); } return $this->container; } }
A quick note about this code: we are providing a Picotainer container. Picotainer is a minimalistic container fully compativle with the ContainerInterop project.
Important: the factory takes one compulsory parameter: the $rootContainer. If some entries in your container are containing
external dependencies (dependencies that are not part of the container), then your container needs to be able
to delegate dependencies fetching to the $rootContainer. For instance, PimpleInterop can delegate dependencies fetching if
you pass another container as the first argument of the constructor.
Note: your package does not have to require the mouf/container-installer package. This is sweet because if
other container aggregators follow the same convention (referencing factory code in composer.json extra section),
there can easily be many different implementations of a root-container (maybe one per framework).
How to use a root container in your project?
This package will simply create a containers.php file at the root of your project.
This containers.php file will contain a list of containers in this form:
<?php return [ [ 'name' => '__root___0', 'description' => 'Container number 0 for package __root__', 'factory' => My\ContainerFactory::getContainer, 'enable' => true ], ];
Please note that the developer can enable or disable packages manually, using the 'enable' attribute.
From there it is up to the application developer to use that file.
Using Acclimate's CompositeContainer, a usage might look like this:
use Acclimate\Container\CompositeContainer; // Let's create a composite container $rootContainer = new CompositeContainer(); // Let's get the containers list $container_descriptors = require 'containers.php'; // Let's add containers to the root container. foreach ($container_descriptors as $descriptor) { if (descriptor['enable']) { $container = $descriptor['factory']($rootContainer); $rootContainer->addContainer($container); } } $myEntry = $rootContainer->get('myEntry');
Allowed syntax
Those syntaxes are all valid to declare container factories in composer.json:
Simply declaring a container-factory via callback:
{
"extra": {
"container-interop": {
"container-factory": "My\\ContainerFactory::getContainer"
}
}
}
Declaring an array of container-factories via callback:
{
"extra": {
"container-interop": {
"container-factory": [
"My\\ContainerFactory1::getContainer",
"My\\ContainerFactory2::getContainer",
"My\\ContainerFactory3::getContainer"
]
}
}
}
Declaring a container-factory descriptor (it contains additionnal data about the factory):
{
"extra": {
"container-interop": {
"container-factory": {
"name": "a unique name for the factory",
"description": "a description of what the factory does",
"factory": "My\\ContainerFactory::getContainer"
}
}
}
}
Note: all parameters of a descriptor are optionnal, except for the "factory" part.
Declaring an array of container-factory descriptors:
{
"extra": {
"container-interop": {
"container-factory":
[{
"name": "a unique name for the factory",
"description": "a description of what the factory does",
"factory": "My\\ContainerFactory::getContainer"
},
{
"name": "a unique name for another factory",
"description": "a description of what the factory does",
"factory": "My\\ContainerFactory2::getContainer"
}]
}
}
}
Benefits
Each package provides its container. The package is not dependent on the DI container used in the application. This way, we can provide packages that are framework agnostic.
Downsides
The classical implementation of the composite controller might imply a performance hit. We will need to think of a way to improve the performance of the composite container (maybe by doing entries maps, mapping entries to their associated container...)
About other projects
This is not the only project working on the "one container per package" paradigm. The FrameworkInterop project by @mnapoli is also taking the same route (although its scope is larger).
About performance
The current implementation of RootContainer is relying on the Acclimate's CompositeContainer. It is a proof-of-concept and no effort has been done performance-wise. The more container you have in your application, the lower the performance should be (linearly).
It does not mean however that performance cannot be improved. There are many possible strategies to improve performance, like building a map of all entries associated to their respective container. This is going further than the current scope of this project.
mouf/container-installer 适用场景与选型建议
mouf/container-installer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 283 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「installer」 「di」 「ContainerInterop」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mouf/container-installer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mouf/container-installer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mouf/container-installer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Installs Aura packages to the Aura system "package" directory.
Install WordPress must-use plugins with Composer
Arikam CMS installers
Install Laravel Framework with any version you want.
Composer asset installer
统计信息
- 总下载量: 283
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 21
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-01-16