quillstack/di
Composer 安装命令:
composer require quillstack/di
包简介
The dependency injection container based on PSR-11: Container interface.
关键字:
README 文档
README
Quillstack DI Container is the dependency injection container based
on PSR-11: Container interface, and with the main goal: to be fast.
You can find the full documentation on the website:
https://quillstack.org/di
This DI container uses constructors and types of class properties.
Installation
To install this package, run the standard command using Composer:
composer require quillstack/di
Usage
You can use Quillstack DI Container when you want:
- To have a simple and fast DI container.
- Define dependencies based on interfaces.
- Define parameters e.g. credentials for a database in the
Databaseclass. - To use constructors or/and class properties.
- To implement your own instance factories e.g. for
Requestclasses. - To use objects as dependencies.
Simple usage
You can easily start using a DI Container:
<?php use Quillstack\DI\Container; require __DIR__ . '/../vendor/autoload.php'; $container = new Container(); $controller = $container->get(ExampleController::class);
Where your ExampleController class looks like:
<?php class ExampleController { private $example = 3; }
Dependencies based on interfaces
If you want to define which class should be loaded based on an interface:
$container = new Container([ LoggerInterface::class => Logger::class, ]); $controller = $container->get(ExampleController::class);
You can define your dependencies using interfaces:
<?php class ExampleController { public function __construct( private LoggerInterface $logger ) { } }
When you create the object using the DI container, the type of $logger property will be set to Logger.
Dependencies with parameters
If some of your classes require parameters, define them as an array passed on the second parameter to the container:
$container = new Container([ Database::class => [ 'hostname' => 'localhost', ], ]); $controller = $container->get(ExampleController::class);
Every time you will get a database object, a container will use localhost as
a value for $hostname parameter:
<?php class Database { public function __construct( private string $hostname ) { } }
Custom instance factories
You can implement your own instance factory. This is especially useful when you want to create many objects in a class family that are very similar in some way.
In our example we want to create different request objects:
<?php class CreateUserRequest implements RequestInterface { }
First, we had to create RequestInterface as a common interface for all requests.
Next, we have to create an instance factory class. To create it, extend a class with CustomFactoryInterface:
<?php use Quillstack\DI\CustomFactoryInterface; class RequestClassFactory implements CustomFactoryInterface { /** * {@inheritDoc} */ public function create(string $id): object { $factory = $this->container->get(GivenRequestFromGlobalsFactory::class); return $factory->createGivenServerRequest($id); } }
Also, use this configuration array when you create a DI container:
$container = new Container([ RequestInterface::class => RequestClassFactory::class, ]); $controller = $container->get(ExampleController::class);
Custom factories are useful for objects you want to create similarly.
Dependencies as objects
In this example, whenever a new class of LoggerInterface will be required as a dependency, a container will use a previously defined object. This object can be created once in a bootstrap file and used in the entire application:
$logger = new Logger('name'); $logger->pushHandler(new StreamHandler('var/app.log');); $container = new Container([ LoggerInterface::class => $logger, ]);
This configuration is helpful if an object should be created once and its instance should be used in other places in the application.
Unit tests
Run tests using a command:
phpdbg -qrr ./vendor/bin/unit-tests
Docker
$ docker-compose up -d
$ docker exec -w /var/www/html -it quillstack_di sh
quillstack/di 适用场景与选型建议
quillstack/di 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 946 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 09 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「container」 「di」 「class」 「dependency-injection」 「properties」 「constructor」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 quillstack/di 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 quillstack/di 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 quillstack/di 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Collection of methods that I or you usually use.
Supporing Symfony's DependencyInjection component for your symfony1 project
A very simple yet useful helper class to handle PHP file uploads.
A fast and intuitive dependency injection container.
Yii2 js base class
Class loader implementation (PSR-0, PSR-4)
统计信息
- 总下载量: 946
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 17
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-03