noglitchyo/middleware-collection-request-handler

简单的PSR-15中间件集合请求处理器。

2.0.1 2019-07-15 14:33 UTC

This package is auto-updated.

Last update: 2024-09-16 01:39:06 UTC


README

轻量级且简单的PSR-15服务器请求处理器实现,用于处理中间件集合。

PHP from Packagist Build Status codecov Scrutinizer code quality (GitHub/Bitbucket) Packagist

描述

PSR-15请求处理器,实现了RequestHandlerInterfaceMiddlewareInterface,能够管理实现MiddlewareInterface的中间件集合。

它可以作为请求处理器或中间件使用,以适应任何实现。

提供了一套使用不同策略(LIFO、FIFO等)的中间件集合,这些策略决定了如何将集合中的中间件提供给请求处理器,并还提供了一个简单的MiddlewareCollectionInterface,以便快速实现自己的策略。

目标

  • 简单性
  • 互操作性

入门

要求

  • PHP >= 7.3

安装

composer require noglitchyo/middleware-collection-request-handler

运行

创建请求处理器类的新实例,该实例可以用作请求处理器或中间件。

从构造函数开始

RequestHandler::__construct(MiddlewareCollectionInterface $middlewareCollection, RequestHandlerInterface $defaultRequestHandler = null)

使用工厂方法

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文件。