nimbly/resolve 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nimbly/resolve

最新稳定版本:2.4

Composer 安装命令:

composer require nimbly/resolve

包简介

A simple PSR-11 compliant autowiring and dependency injector.

README 文档

README

Latest Stable Version GitHub Workflow Status Codecov branch License

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"
	]
);

统计信息

  • 总下载量: 1.29k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-14