infuse-di/infuse
Composer 安装命令:
composer require infuse-di/infuse
包简介
A PSR-11 compliant dependency injection library
README 文档
README
Infuse 🍃
A minimal PSR-11 implementation.
Features
- ✔️ Autowiring (powered by Reflection)
- ✔️ Simple API (only 3 methods)
- ✔️ Container can be built from definitions
- ✔️ Singletons
- ✔️ Detects circular dependencies
- ⏳ Compilable for production
- ⏳ PHPStan generics support for container bindings
Documentation
Infuse has a very minimal API with only has three methods:
/** * Finds an entry of the container by its identifier and returns it. * * @param string $id identifier of the entry to look for * * @return mixed entry * * @throws NotFoundExceptionInterface no entry was found for the provided identifier * @throws ContainerExceptionInterface error while retrieving the entry */ public function get(string $id): mixed; /** * Returns true if the container can return an entry for the given identifier. * Returns false otherwise. * * `has($id)` returning true does not mean that `get($id)` will not throw an exception. * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. * * @param string $id identifier of the entry to look for */ public function has(string $id): bool; /** * @param \Closure(Container): mixed $definition * * @throws ContainerExceptionInterface if provided id is not unique */ public function bind(string $id, \Closure $definition): void;
Example:
use Infuse\Container; $container = new Container(); // Container::bind tells the Container how to build a Bar object $container->bind(Bar::class, function () { return new Bar(); }); // Container::has checks if there's a binding with the provided id $container->has(Bar::class); // true // the callable parameter receives the Container itself as argument $container->bind(Foo::class, function (Container $c) { $bar = $c->get(Bar::class); return new Foo($bar): }); // Container::get retrieves an instance from the Container, the bound callable will be called at this moment $foo = $container->get(Foo::class); $isFoo = $foo instanceof Foo; // true // This will throw a ContainerException, ids must be unique $container->bind(Bar::class, function (Container $c) { return new Bar(); }); // This will throw a NotFoundException, this id has not been bound $container->get(Zee::class); // You can bind basically anything $container->bind('some_array', fn () => ['hello' => 'world']); $container->bind('some_scalar', fn () => 42);
You can also create a ready to use Container from a definitions array:
// definitions.php <?php use Infuse\Container; // should be shaped as array<string, callable> return [ GeoLocationService::class => function (Container $c) { $config = $c->get('config'); return new GeoLocationService($config['GEOLOCATION_API_KEY']); }, 'config' => function () { return [ 'GEOLOCATION_API_KEY' => $_ENV['GEOLOCATION_API_KEY'], ]; }, ];
// something.php use Infuse\ContainerFactory; $definitions = require __DIR__ . '/definitions.php'; $container = ContainerFactory::FromDefinitions($definitions); $container->has('config'); // true $container->has(GeoLocationService::class); // true
Mark your classes with the Singleton attribute to always receive the same instance:
use Infuse\Attributes\Singleton; #[Singleton] class SomeSingleton {} $container = new Container(); $sameInstance = $container->get(SomeSingleton::class); $asThisOne = $container->get(SomeSingleton::class);
Installing
The recommended way to install Infuse is through Composer.
composer require infuse-di/infuse
Tests
To execute the test suite, you'll need to install all development dependencies.
git clone https://github.com/davisenra/infuse
composer install
composer test
License
This project is licensed under the MIT license. See License File for more information.
infuse-di/infuse 适用场景与选型建议
infuse-di/infuse 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 06 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dependency injection」 「di」 「PSR-11」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 infuse-di/infuse 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 infuse-di/infuse 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 infuse-di/infuse 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Facilitates the routine step of bootstrapping PHP applications.
A fast and intuitive dependency injection container.
PSR-11 Aura.DI container configurator for Laminas and Mezzio applications. Drop-in replacement for the archived laminas/laminas-auradi-config with PHP 8.4 and 8.5 support.
Symfony compiler pass that allows to use service providers as defined in container-interop/service-provider
Dependency injection container for the Monolith framework.
Http Microframework for Hack
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-06-27