webdevcave/yadic
Composer 安装命令:
composer require webdevcave/yadic
包简介
Yet another dependency injection container for PHP
README 文档
README
This is a simple to use, yet powerful service container that provides a seamless way to automate dependency injection featuring auto-wiring and object hydration.
composer require webdevcave/yadic
Alternatively, you can clone the repository or download the source files directly and include them in your project.
Usage
Autowiring
<?php require_once __DIR__.'/vendor/autoload.php'; use Webdevcave\Yadic\Annotations\Provides; use Webdevcave\Yadic\Annotations\Singleton; use Webdevcave\Yadic\ServiceContainer; interface StorageInterface { public function store(mixed $data): bool; } #[Provides('storage')] #[Provides(StorageInterface::class)] #[Singleton] class Storage implements StorageInterface { public function store(mixed $data): bool { //store data... return true; } } class MyController { public function __construct( private StorageInterface $storage ) { } public function save(): bool { return $this->storage->store('my data...'); } } $container = new ServiceContainer(); //No need to do this in a real world application: $container->addAlias(StorageInterface::class, Storage::class); //Use this instead: //$container->loadDefinitionsFromDirectory($directory, $namespace); //Loads annotations from classes declared in a PSR4 directory //var_dump($container->get('storage')->store($data)); var_dump($container->get(MyController::class)->save()); //bool(true)
Invoking a method ft. autowiring
$arguments = ['nonInjectableArgument' => 'value']; //optional $container->invoke([$instance, 'methodName'], $arguments);
Hydration
//Class declarations: use Webdevcave\Yadic\Annotations\ArrayOf; class Candidate { public function __construct( public ?string $name = null, public ?int $age = null, #[ArrayOf(Skill::class)] public array $skills = [] ) { } } class Skill { public function __construct( public string $title, ) { } } // Hydration example 1: $data = [ 'name' => 'John Doe', 'age' => 25, 'skills' => [ ['title' => 'PHP'], ['title' => 'Java'], ['title' => 'Rust'], ['title' => 'React'], ], ]; $instance = $container->hydrate(Candidate::class, $data); //Results output /* print_r($instance); This test printed output: Candidate Object ( [name] => John Doe [age] => 25 [skills] => Array ( [0] => Skill Object ( [title] => PHP ) [1] => Skill Object ( [title] => Java ) [2] => Skill Object ( [title] => Rust ) [3] => Skill Object ( [title] => React ) ) ) */ // Hydration example 2: $data = [ [ 'name' => 'Foo', //... ], [ 'name' => 'Bar', //... ] ]; $instances = $container->hydrate(Candidate::class, $data); //Results output /* print_r($instances); This test printed output: Array ( [0] => Candidate Object ( [name] => Foo //... ) [1] => Candidate Object ( [name] => Bar //... ) ) */
Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or a pull request on GitHub.
License
This project is licensed under the MIT License - see the LICENSE file for details.
webdevcave/yadic 适用场景与选型建议
webdevcave/yadic 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 36 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 06 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「container」 「php」 「repository」 「dependency-injection」 「Autowiring」 「hydration」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webdevcave/yadic 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webdevcave/yadic 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webdevcave/yadic 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
A fast and intuitive dependency injection container.
Laravel 5 - Repositories to the database layer
Structure for the Laravel Service-Repository Pattern
Dependency injection container for the Monolith framework.
The PSR-11 container bridges
统计信息
- 总下载量: 36
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-06-26