noglitchyo / middleware-collection-request-handler
简单的PSR-15中间件集合请求处理器。
Requires
- php: >=7.3
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- nyholm/psr7: ^1.1
- phpstan/phpstan: ^0.11.8
- phpunit/phpunit: ^8.1
- squizlabs/php_codesniffer: *
This package is auto-updated.
Last update: 2024-09-16 01:39:06 UTC
README
轻量级且简单的PSR-15服务器请求处理器实现,用于处理中间件集合。
描述
PSR-15请求处理器,实现了RequestHandlerInterface和MiddlewareInterface,能够管理实现MiddlewareInterface的中间件集合。
它可以作为请求处理器或中间件使用,以适应任何实现。
提供了一套使用不同策略(LIFO、FIFO等)的中间件集合,这些策略决定了如何将集合中的中间件提供给请求处理器,并还提供了一个简单的MiddlewareCollectionInterface,以便快速实现自己的策略。
目标
- 简单性
- 互操作性
入门
要求
- PHP >= 7.3
安装
composer require noglitchyo/middleware-collection-request-handler
运行
创建请求处理器类的新实例,该实例可以用作请求处理器或中间件。
从构造函数开始
RequestHandler::__construct(MiddlewareCollectionInterface $middlewareCollection, RequestHandlerInterface $defaultRequestHandler = null)
-
$middlewareCollection
: MiddlewareCollectionInterface包含中间件并定义了存储中间件和检索下一个中间件所使用的策略。提供了一些使用常见策略的实现:堆栈(LIFO)、队列(FIFO)。
-
$defaultRequestHandler = null
: RequestHandlerInterface如果没有中间件能够创建响应,则提供默认响应实现ResponseInterface。
以下是一些可能的“默认请求处理器”的示例
使用工厂方法
RequestHandler::fromCallable(callable $callable, MiddlewareCollectionInterface $middlewareCollection)
它通过将给定的callable
封装在一个匿名实例中创建一个请求处理器实例,该实例实现了RequestHandlerInterface。这个可调用的是$defaultRequestHandler。它必须返回一个实现ResponseInterface的响应。
示例
下面是如何简单启动中间件处理器的示例
<?php use NoGlitchYo\MiddlewareCollectionRequestHandler\RequestHandler; use NoGlitchYo\MiddlewareCollectionRequestHandler\Collection\SplStackMiddlewareCollection; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Message\ResponseInterface; // Instantiate a collection of middlewares with an anonymous middleware class. // In this example, we are using a "stack" implementation of the collection. $middlewareCollection = new SplStackMiddlewareCollection([ new class implements MiddlewareInterface{ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface{ return $handler->handle($request); } } ]); // Let's add one more middleware in the collection (this time, from a callable) $middlewareCollection->addFromCallable(function(ServerRequestInterface $request, RequestHandlerInterface $handler){ return $handler->handle($request); }); // Instantiate a new request handler with a default handler and the middleware collection. $requestHandler = RequestHandler::fromCallable( function (ServerRequestInterface $serverRequest){ return new /* instance of ResponseInterface */; }, $middlewareCollection ); // As a RequestHandler: // Pass the request to the request handler which will dispatch the request to the middlewares. $response = $requestHandler->handle(/* ServerRequestInterface */);
创建自定义MiddlewareCollectionInterface实现
如果需要,创建新的MiddlewareCollectionInterface实现非常简单。该接口只需要3个方法。
<?php interface MiddlewareCollectionInterface { /** * Must return true if there is no middleware in the collection to process. * @return bool */ public function isEmpty(): bool; /** * Must return the next middleware to process in the collection. * Depending on the implemented strategy, the middleware MAY not be removed from the collection. * @return MiddlewareInterface */ public function next(): MiddlewareInterface; /** * Add a middleware instance of MiddlewareInterface to the collection. * * @param MiddlewareInterface $middleware * * @return MiddlewareCollectionInterface */ public function add(MiddlewareInterface $middleware): MiddlewareCollectionInterface; }
测试
想要运行测试套件吗?请继续。
composer测试
参考文献
https://www.php-fig.org/psr/psr-15/
许可证
本项目采用MIT许可证 - 详细信息请参阅LICENSE.md文件。