apex/container
Composer 安装命令:
composer require apex/container
包简介
Light-weight DI container
README 文档
README
A lightweight, straight forward dependency injection container that simply works, and works well. Supports config file and all standard injection methods -- constructor, setter, annotation, plus also attributes. Also includes a Di wrapper class that allows container methods to be accessed statically for greater simplicity and efficiency.
Installation
Install via Composer with:
composer require apex/container
Basic Usage
Supports the standard container methods -- has, get, set, make, call,
use Apex\Container\Container; $cntr = new Container('/path/to/definitions.php'); // Set and get $cntr->set('full_name', 'John Doe'); // Get service $logger = $cntr->get(LoggerInterface::class); $logger->info("Here I am"); // Make and set $user = $cntr->makeset(User::class, ['id' => 311]); // Call via method setter injection $cntr->call([$user, 'updateStatus'], ['status' => 'inactive']);
Constructor
Constructor takes the following parameters, none of which are required.
| Parameter | Type | Description |
|---|---|---|
$config_file |
string | Location of the definitions file which defines starting items and services. Should be a PHP file that returns an associative array. Please see the Definitions File page for details. If defind in the constructor, will be automatically loaded. |
$use_autowiring |
bool | Whether or not to enable auto-wiring. If true, the use declarations of all files will be loaded, and checked for injection parameters. Defaults to true. |
$use_attributes |
bool | If true, will scan attributes in all files for injection properties. Please see the Attributes Injection page for details. Defaults to false. |
$use_annotations |
bool | If true, will scan the properties and doc blocks of all files for injected properties. Please see the Annotation Injection page for details. Defaults to false. |
Mark Items as Services
You must explicitly mark necessary items as services, meaning the first time they are retrived from the container, they will be automatically instantiated with the new object instance set in the container for future use. Retrieving instantiable items from the container that are not marked as services will simply return the class name / closure, and will not instantiate the object.
You may easily mark items as services with the markItemAsService() method, for example:
use Apex\Container\Container; use Apex\Psr\Log\LoggerInterface; $cntr = new Container(); $cntr->markItemAsService(LoggerInterface::class); $cntr->markItemAsService('debugger');
Methods
The following base methods are supported, as per all containers.
| Method | Description |
|---|---|
has(string $name):bool |
Check whether or not item is available in container. |
get(string $name):mixed |
Gets an item from the container. If does not already exist, will try to make and set the item. Returns null on non-existent item. |
set(string $name, mixed $item):void |
Sets an item in the container. |
make(string $name, array $params = []):mixed |
Creates new instance of the class with provided parameters. |
makeset(string $name, array $params = []):mixed |
Creates new instance of the class with provided parameters, and also sets class name into container for future use. |
| `call(callable | array $callable, array $params = []):mixed` |
buildContainer(string $config_file):void |
Build container with the specified definitions file. Useful if you didn't pass a definitions file to the constructor, or if you're using the Di class to access the container statically. |
Singleton Di Class
This package includes the Di class which acts as a singleton and allows container methods to be accessed statically, instead of passing the container object from class to class, method to method. This is done to provide efficiency and simplicity, and also assumes you're only managing one DI container per-request.
Modify the properties within the /src/Di.php file as they are used as the constructor parameters for the container. Example usage is below.
namespace myApp; use Apex\Container\Di; use myApp\Users\User; // Get logger $logger = Di::get(LoggerInterface::Class); // Make user object Di::make(User::class, ['id' => 52]); // Set and get variable Di::set('full_name', 'John Doe'); $name = Di::get('full_name'); // John Doe
Follow Apex
Loads of good things coming in the near future including new quality open source packages, more advanced articles / tutorials that go over down to earth useful topics, et al. Stay informed by joining the mailing list on our web site, or follow along on Twitter at @mdizak1.
apex/container 适用场景与选型建议
apex/container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.19k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2021 年 01 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 apex/container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 apex/container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 0
- 依赖项目数: 8
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-29