sergix44/container
Composer 安装命令:
composer require sergix44/container
包简介
A simple and fast service container
README 文档
README
A simple, fast and PSR-11 compliant service container.
Perfect for libraries that benefit from dependency injection, which integrate with other frameworks, thanks to the delegation feature.
🚀 Installation
composer require sergix44/container
🔧 Usage
Register a definition:
$container = new \SergiX44\Container\Container(); $container->bind(ServiceInterface::class, MyService::class); $instance = $container->get(ClassThatUseMyService::class);
Register a shared definition (first resolution will be cached, and the same instance will be returned)
$container = new \SergiX44\Container\Container(); $container->singleton(ServiceInterface::class, MyService::class); $instance = $container->get(ClassThatUseMyService::class);
You can define factories as closures:
$container = new \SergiX44\Container\Container(); $value = 'dynamic'; // factory $container->bind(ServiceInterface::class, function (\Psr\Container\ContainerInterface $container) use ($value) { return new MyService($container->get(AnotherService::class), $value); }); // shared/singleton $container->singleton(FooServiceInterface::class, function (\Psr\Container\ContainerInterface $container) { return new FooService($container->get(ServiceInterface::class)); }); $instance = $container->get(ClassThatUseMyService::class);
You can set an already resolved instance:
$container = new \SergiX44\Container\Container(); $service = new MyService(); $container->set(ServiceInterface::class, $service); // or even as string: // $container->set('service', $service); // $service = $container->get('service'); $instance = $container->get(ClassThatUseMyService::class);
It can be also used to inject parameters inside any callable:
// InvokableClass.php class InvokableClass { public function __invoke(ServiceInterface $service) { // } } // ClassAndMethod.php class ClassAndMethod { public function method(ServiceInterface $service) { // } } // -- $container = new \SergiX44\Container\Container(); $container->bind(ServiceInterface::class, MyService::class); $result = $container->call(InvokableClass::class); // calls __invoke $result = $container->call(new InvokableClass()); // calls __invoke $result = $container->call([ClassAndMethod::class, 'method']); // calls method $result = $container->call([new ClassAndMethod(), 'method']); // calls method $result = $container->call(function (ServiceInterface $service) { // });
It's also possible to pass arbitrary parameters with an associative array:
// InvokableClass.php class InvokableClass { public function __invoke(ServiceInterface $service, string $a, int $b) { // } } $container = new \SergiX44\Container\Container(); // map parameter name => value $result = $container->call(InvokableClass::class, ['a' => 'foo', 'b' => 123]); // positional $result = $container->call(InvokableClass::class, ['foo', 123]);
It's also possible to mix positional and associative notation:
// InvokableClass.php class InvokableClass { public function __invoke(ServiceInterface $service, string $a, int $b) { // same result: b=456 and a=foo } } $container = new \SergiX44\Container\Container(); $result = $container->call(InvokableClass::class, ['b' => 456, 'foo']);
⚗️ Testing
To run the test suite:
vendor/bin/pest
🏅 Credits
📖 License
The MIT License (MIT). Please see License File for more information.
sergix44/container 适用场景与选型建议
sergix44/container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 262.09k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2023 年 06 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sergix44/container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sergix44/container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 262.09k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 13
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-27