robertwesner/dependency-injection
Composer 安装命令:
composer require robertwesner/dependency-injection
包简介
A fun PSR-11 container implementation
README 文档
README
PSR-11 container with autowiring
What is this?
This is a small and fun PSR-11 container implementation with autowiring.
It provides plenty of alternative ways to autowire non-object values from .env, JSON files, constants, and more via PHP Attributes.
Installation
composer require robertwesner/dependency-injection
Usage
// Instantiate new container $container = new Container(); // Load class MyClass with all it's dependencies. Store and return its instance. $instance = $container->get(MyClass::class); $instance->myMethod('Some text.', 1234);
Autowiring non-instance values
This package provides multiple ways to load scalar values and arrays into classes.
readonly class Foo { public function __construct( // Resolves all dependencies for Bar, if any, and uses its instance private Bar $bar, // Load any fixed value, can take values from constants... #[AutowireValue(Moody::MOOD)] private string $myMood, // ...or value literals #[AutowireValue(1234)] private int $scalar, // Load from superglobals like _COOKIE, _SESSION, or GLOBALS #[AutowireGlobal('GLOBALS', 'demo')] private string $demo, // Call any function or static method, optionally with arguments #[AutowireCallable([StaticProvider::class, 'provide'], ['123', 'test'])] private string $provided, // Load a full file #[AutowireFile(__DIR__ . '/../../cat.txt')] private string $asciiCat, // Load from a .env, .env.local, or similar file #[AutowireEnv(__DIR__ . '/../../.env', 'TEST')] private string $envTest, // Load from JSON file based on JSONPath // Can return an array of all JSONPath matches with the "multiple" Parameter #[AutowireJson(__DIR__ . '/../../foo.json', '$.test.value')] private int $val, // Load from YAML file based on JSONPath // Can return an array of all JSONPath matches with the "multiple" Parameter #[AutowireYaml(__DIR__ . '/../../foo.yaml', '$.test.value')] private int $fromYaml, // Load from TOML file based on JSONPath // Can return an array of all JSONPath matches with the "multiple" Parameter #[AutowireToml(__DIR__ . '/../../config.toml', '$.database')] private array $databaseConfig, // Load an XML file and parses it as SimpleXml. Then applies xPath to it to acquire an array element result #[AutowireXml(__DIR__ . '/../../test.xml', '/document/chapters/chapter[2]/@title')] private array $chapter2TitleResult, // Reads a header and stores its value. Missing Headers are always NULL so all usages should be nullable! #[AutowireHeader('User-Agent')] private ?string $userAgent, ) {} }
Buffering autowired files for multiple access
When using a single file multiple times you should consider adding the Attribute #[BufferFile]
to store the (parsed) file in memory and load it when reused.
applicable to:
#[AutowireFile]#[AutowireJson]#[AutowireEnv]#[AutowireYaml]#[AutowireToml]#[AutowireXml]
readonly class DatabaseService { public function __construct( #[AutowireEnv(__DIR__ . '../../.env', 'MYSQL_SERVER')] #[BufferFile] private string $dbServer, #[AutowireEnv(__DIR__ . '../../.env', 'MYSQL_USERNAME')] #[BufferFile] private string $dbUsername, #[AutowireEnv(__DIR__ . '../../.env', 'MYSQL_PASSWORD')] #[BufferFile] private string $dbPassword, ) {} }
robertwesner/dependency-injection 适用场景与选型建议
robertwesner/dependency-injection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 75 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 05 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 robertwesner/dependency-injection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 robertwesner/dependency-injection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 75
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 5
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-15