phespro/container
Composer 安装命令:
composer require phespro/container
包简介
Super simple dependency injection container for php
README 文档
README
phespro/container
Super simple dependency injection container for php.
- LOC < 300
- No cache required
- Includes tagging and decorating
- 100% line coverage & 100% mutation coverage
- Implements PSR-11
Usage
Install it:
composer require phespro/container
Create it:
<?php
require __DIR__ . '/vendor/autoload.php';
$container = new Container;
Use it!!!
Adding Services
$container->add('some_id', fn(Container $c) => new MyService); // register singleton
$container->addFactory('other_id', fn(Container $c) => new OtherService); // register factory
$container->add('tagged_service', fn(Container $c) => new TaggedService, ['console_command']);
Get Services
$container->has('some_id'); // does the service exist?
$container->get('some_id'); // get the service
$container->getByTag('console_command'); // get all services, tagged with 'console_command'
Decorating Services
You can decorate (or overwrite) services:
$container->decorate('some_id', fn(Container $c, callable $prev) => new Decorator($prev());
// or decorate it with factory
$container->decorateWithFactory('some_id', fn(Container $c, callable $prev) => new Decorator($prev()));
Scoping
Depending on the type of execution model you are running your PHP app, you might have multiple requests / commands per process.
In this case you might consider using scoped services. A scoped service is used for one specific execution context.
Scoping example
<?php $container = new Container(); $container->add('your-service-id', fn() => new YourService, Type::SCOPED); $service1 = $scope->get('your-service-id'); $scope = $container->newScope(); $service2 = $scope->get('your-service-id'); $service3 = $scope->get('your-service-id'); // $service1 === $service2 -> false -> different instance // $service1 === $service3 -> false -> different instance // $service2 === $service3 -> true -> same instance because it's the same scope
phespro/container 适用场景与选型建议
phespro/container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.99k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 11 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 phespro/container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phespro/container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-11-19