rumur/autowire
Composer 安装命令:
composer require rumur/autowire
包简介
Simple PHP autowiring functionality.
README 文档
README
Package Installation
composer require rumur/autowire
How to use it?
It's actually pretty simple. A good starting point is to create an instance of Autowire class.
// plugin-name.php <?php use Rumur\Autowiring\Autowire; $autowire = Autowire::create();
Register Singletons
Autowire allows you to register any instance/variable as a singleton, so you'll be able always get the same outcome,
when one of your classes or functions would need such dependency.
For an instance, let's imagine that you need to inject wpdb class in some repositories of yours.
For this, we would need to register wbdb as a singleton and just pass one of your repositories into the Autowire::make method and the rest Autowire does for you.
// plugin-name.php <?php // ... $autowire->singleton(wpdb::class, fn() => $GLOBALS['wpdb']); // plugin/repositories/IOrderRepository.php interface IOrderRepository { public function find(int $order_id): ?Order public function findAll(): array } // plugin/repositories/OrderRepository.php use wpdb; class OrderRepository implements IOrderRepository { protected wpdb $connection; public function __construct(wpdb $connection) { $this->connection = $connection; } public function find(int $order_id): ?Order { // ... } public function findAll(): array { // ... } // ... } // Somewhere in the code $order = $autowire->make(OrderRepository::class)->find(2022);
Binding
In case you need to bind a specific interface with its implementation Autowire gets you covered.
NOTE. In some case you don't have to bind all available classes to Autowire in order to be able to resolve them as a dependency, e.g. OrderController
below never been bound to Autowire but it still can make it for you automatically.
// Somewhere where service providers are getting injected. $autowire->bind(IOrderRepository::class, OrderRepository::class) // app/http/api/OrderController.php class OrderController { protected IOrderRepository $repository; public function __construct(IOrderRepository $repository) { $this->repository; } public function index(): array { return $this->repository->findAll(); } } // Somewhere in you app. $ctrl = app()->autowire->make(OrderController::class); $orders = $ctrl->index();
Autowire callables
In case you need to resolve dependencies either for a method or
any other callable instance, Autowire::call comes to rescue.
<?php $autowire->singleton(IOrderRepository::class, OrderRepository::class) class OrderController { public function index(IOrderRepository $repository): array { return $repository->findAll(); } } $ctrl = new OrderController; $orders = app()->autowire->call([$ctrl,'index']); // Or if `index` method is static do the following. $orders = app()->autowire->call('OrderController::index');
License
This package is licensed under the MIT License - see the LICENSE.md file for details.
rumur/autowire 适用场景与选型建议
rumur/autowire 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 04 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「plugin」 「wordpress」 「autowire」 「Autowiring」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rumur/autowire 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rumur/autowire 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rumur/autowire 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
Autowire and configure using PHP 8 Attributes in Laravel.
A fast, autowired PSR-11 dependency injection container for PHP with cached resolution closures.
PSR-11 Container for Dependency Injection (auto-wiring, attributes, factories, closures, array access)
autowire dependency injection from PSR-11 containers
Plugin for YOURLS. Default tools to use in some laemmi plugins
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-04-06