nimbly/resolve
Composer 安装命令:
composer require nimbly/resolve
包简介
A simple PSR-11 compliant autowiring and dependency injector.
README 文档
README
Resolve
Resolve is an autowiring and dependency resolver trait able to call functions or methods or make new instances of classes with (or without) the aid of a PSR-11 compliant container.
Use Resolve in your own project or library when you would like to leverage dependency injection decoupled from a specific ContainerInterface implmentation.
Installation
composer require nimbly/resolve
Requirements
- PHP >= 8.0
Container support
Resolve can optionally be passed a PSR-11 Container instance but does not ship with an implementation.
You can try one of these:
Usage
Add the Resolve trait to anything you would like to add dependency injection capabilities to.
class Dispatcher { use Resolve; public function __construct( protected Router $router, protected ContainterInterface $container) { } public function dispatch(ServerRequestInterface $request): ResponseInterface { $route = $this->router->resolve($request); $handler = $this->makeCallable($route->getHandler()); return $this->call( $handler, $this->container, [ServerRequestInterface::class => $request] ); } }
Make
The make method can instantiate any class you may need and resolve the constructor dependencies automatically from both the container instance (if one was provided) and the optional parameters you provide.
$instance = $this->make( FooHandler::class, $this->container, ["additional_parameter" => "Foo"] );
Make a thing callable
Often you would like to make something that represents a callable into an actual callable type. You can pass a string that represents a callable or an actual callable into the makeCallable method.
// An invokable class. $invokable = $this->makeCallable(Foo::class, $this->container);
You can pass in a fully qualified class namespace, an @ symbol, and the method name. For example:
// A class and method name string. $instance_method = $this->makeCallable("\App\Http\Handlers\FooHandler@createNewFoo");
Call
The call method will call any callable you pass in, resolve the dependencies of that callable from both the container and the optional set of parameters passed, and invoke that callable.
If a dependency cannot be resolved from the container or optional parameters, Resolve will attempt to make one for you automatically.
If making the dependecy fails or is not possible, an exception will be thrown.
Callable types
Instance method
$this->call( [new FooHandler, "findById"], $this->container, [ ServerRequestInteface::class => $serverRequest, "id" => "3122accd-e640-4c4c-b299-ccad074cb077" ] );
Static method
$this->call( [FooHandler::class, "findById"], $this->container, [ ServerRequestInteface::class => $serverRequest, "id" => "3122accd-e640-4c4c-b299-ccad074cb077" ] );
Invokable
$this->call( new FooHandler, $this->container, [ ServerRequestInteface::class => $serverRequest, "id" => "3122accd-e640-4c4c-b299-ccad074cb077" ] );
Function
$this->call( "\Handlers\Foo\findById", $this->container, [ ServerRequestInteface::class => $serverRequest, "id" => "3122accd-e640-4c4c-b299-ccad074cb077" ] );
nimbly/resolve 适用场景与选型建议
nimbly/resolve 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.74k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2020 年 08 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dependency injection」 「di」 「Autowiring」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nimbly/resolve 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nimbly/resolve 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nimbly/resolve 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and intuitive dependency injection container.
Powerful auto wiring dependency injection container including PSR-11.
Dependency injection container for the Monolith framework.
A dependency injection container with autowiring
Http Microframework for Hack
An auto-wired dependency injection container
统计信息
- 总下载量: 1.74k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 29
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-14