parable-php/di
Composer 安装命令:
composer require parable-php/di
包简介
Parable DI is a micro Dependency Injection Container
README 文档
README
Parable DI is a no-nonsense dependency injection container that gets the job done.
Install
Php 8.0+ and composer are required.
$ composer require parable-php/di
Usage
Example usage of a straightforward situation:
use \Parable\Di\Container; $container = new Container(); $app = $container->get(App::class); $app->run();
Example usage of an interface-hinted dependency being mapped:
use \Parable\Di\Container; $container = new Container(); class App { public function __construct(ThatInterface $classWithThatInterface) { } public function run() { echo "Run? RUN!"; } } $container->map(ThatInterface::class, ClassWithThatInterface::class); $app = $container->get(App::class); $app->run();
The above situation can also be solved by instantiating and then storing ClassWithThatInterface under ThatInterface.
Example usage of a class that needs the di itself, in case you need to do dynamic DI:
use \Parable\Di\Container; $container = new Container(); class App { public $container; public function __construct( public Container $container ) {} } $app = $container->get(App::class); var_dump($app->container->has(App::class)); // output: bool(true)
For all other use cases, simply check the tests in tests/ContainerTest.php.
API
get(string $name): object- creates or gets instancehas(string $name): bool- check if instance is storedassertHas(string $name): void- check and throw exception if instance is not storedbuild(string $name): object- build instance with stored deps, don't storebuildAll(string $name): object- build instance with new deps, don't storemap(string $requested, string $replacement): void- allow pre-emptively defining a replacement class to be instantiated when the requested name is retrieved or built. Use for lazy-loading classes, i.e. for interface deps.unmap(string $requested): void- removes the mapping and clears any previously mapped instancesgetDependenciesFor(string $name, [int $storedDependencies]): array- get dependencies for instance, with stored (default) or new depsstore(object $instance, [string $name]): void- store instance under name, or under instance name by defaultclear(string $name): void- clear instanceclearExcept(array $keep): void- clear all except provided names, throws if any of the instances to keep doesn't existclearAll(): void- clear all
Where object refers to any instance of any class.
Contributing
Any suggestions, bug reports or general feedback is welcome. Use github issues and pull requests, or find me over at devvoh.com.
License
All Parable components are open-source software, licensed under the MIT license.
parable-php/di 适用场景与选型建议
parable-php/di 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 903 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「container」 「library」 「micro」 「di」 「dependency-injection」 「parable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 parable-php/di 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 parable-php/di 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 parable-php/di 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Anax htmlform module.
A fast and intuitive dependency injection container.
Micro Active Record library in PHP, support chain calls, events, and relations.
Onix Micro Framework
Inbox pattern process implementation for your Laravel Applications
PHP classes for database handling upon PDO
统计信息
- 总下载量: 903
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 11
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-08-09