nimbly/resolve

A simple PSR-11 compliant autowiring and dependency injector.

2.3 2024-05-04 17:23 UTC

This package is auto-updated.

Last update: 2024-12-04 18:47:46 UTC


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