ellipse / middleware-container
Psr-15 middleware proxying a Psr-11 container entry
Requires
- php: >=7.0
- ellipse/type-errors: ^1.0
- psr/container: ^1.0
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- eloquent/phony-kahlan: ^1.0
- kahlan/kahlan: ^4.0
This package is auto-updated.
Last update: 2024-10-26 01:06:58 UTC
README
This package provides a Psr-15 middleware proxying a Psr-11 container entry.
Require php >= 7.0
Installation composer require ellipse/middleware-container
Run tests ./vendor/bin/kahlan
Using container entries as middleware
The class Ellipse\Middleware\ContainerMiddleware
takes an implementation of Psr\Container\ContainerInterface
and a container id as parameters. Its ->process()
method retrieve a middleware from the container using this id and proxy its ->process()
method.
It can be useful in situations the container entry should be resolved at the time the request is processed.
An Ellipse\Middleware\Exceptions\ContainedMiddlewareTypeException
is thrown when the value retrieved from the container is not an object implementing Psr\Http\Server\MiddlewareInterface
.
<?php namespace App; use SomePsr11Container; use Ellipse\Middleware\ContainerMiddleware; // Get some Psr-11 container. $container = new SomePsr11Container; // Add a middleware in the container. $container->set('some.middleware', function () { return new SomeMiddleware; }); // Create a container middleware with the Psr-11 container and the entry id. $middleware = new ContainerMiddleware($container, 'some.middleware'); // The middleware ->process() method retrieve the middleware from the container and proxy it. $response = $middleware->process($request, new SomeRequestHandler);
Example using auto wiring
It can be cumbersome to register every middleware classes in the container. Here is how to auto wire middleware instances using the Ellipse\Container\ReflectionContainer
class from the ellipse/container-reflection package.
<?php namespace App; use Psr\Http\Server\MiddlewareInterface; use SomePsr11Container; use Ellipse\Container\ReflectionContainer; use Ellipse\Middleware\ContainerMiddleware; // Get some Psr-11 container. $container = new SomePsr11Container; // Decorate the container with a reflection container. // Specify the middleware implementations can be auto wired. $reflection = new ReflectionContainer($container, [ MiddlewareInterface::class, ]); // Create a container middleware with the reflection container and a middleware class name. $middleware = new ContainerMiddleware($reflection, SomeMiddleware::class); // An instance of SomeMiddleware is built and its ->process() method is proxied. $response = $middleware->process($request, new SomeRequestHandler);