northwoods/lazy-middleware

Lazy loading for middleware and request handlers

2.0.0 2019-08-10 15:58 UTC

This package is auto-updated.

Last update: 2024-12-11 03:56:14 UTC


README

Build Status Code Quality Code Coverage Latest Stable Version Total Downloads License

Lazy loading for PSR-15 middleware and request handlers that supports "just in time" instantiation using a PSR-11 container.

Installation

The best way to install and use this package is with composer:

composer require northwoods/lazy-middleware

Usage

This package contains two factories: one for request handlers and one for middleware.

LazyHandlerFactory::defer($handler)

Create a new lazy handler.

The $handler identifier is not required to be a class name. Any string that refers to a container identifier can be used.

use Northwoods\Middleware\LazyHandlerFactory;

/** @var ContainerInterface */
$container = /* any container */;

$lazyHandler = new LazyHandlerFactory($container);

/** @var \Psr\Http\Server\RequestHandlerInterface */
$handler = $lazyHandler->defer(Acme\FooHandler::class);

LazyMiddlewareFactory::defer($middleware)

Create a new lazy middleware.

The $middleware identifier is not required to be a class name. Any string that refers to a container identifier can be used.

use Northwoods\Middleware\LazyMiddlewareFactory;

/** @var ContainerInterface */
$container = /* any container */;

$lazyMiddleware = new LazyMiddlewareFactory($container);

/** @var \Psr\Http\Server\MiddlewareInterface */
$middleware = $lazyMiddleware->defer(Acme\BarMiddleware::class);