robertwesner/dependency-injection
最新稳定版本:v1.4.0
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, ) {} }
统计信息
- 总下载量: 48
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 5
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-15