ellipse/middleware-container
Composer 安装命令:
composer require ellipse/middleware-container
包简介
Psr-15 middleware proxying a Psr-11 container entry
README 文档
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);
统计信息
- 总下载量: 137
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-01-30