phoole/middleware

适用于PHP的简洁简单的PSR-15兼容中间件运行器库

1.1.0 2019-11-19 06:46 UTC

This package is auto-updated.

Last update: 2024-09-19 17:19:13 UTC


README

Build Status Scrutinizer Code Quality Code Climate PHP 7 Latest Stable Version License

适用于PHP的简洁简单的PSR-15兼容中间件运行器库。

安装

通过composer工具进行安装。

composer require "phoole/middleware"

或者在您的composer.json中添加以下行

{
    "require": {
       "phoole/middleware": "1.*"
    }
}

特性

  • 能够使用符合PSR-15规范的中间件。

  • 能够将中间件队列(一组中间件)作为一个通用中间件用于另一个(或主)队列。

使用方法

创建中间件队列,然后处理所有中间件。

use Phoole\Middleware\Queue;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ServerRequest;

// create middleware queue with 404 response as default(last)
$mws = new Queue(new Response(404));

// add middlewares
$mws->add(
    new LoggerMiddleware(),
    new DispatcherMiddleware()
);

// process the request with middlewares
$response = $mws->handle(new ServerRequest('GET', 'http://bingo.com/get'));

高级

  • 子队列

    Phoole\Middleware\Queue实现了Psr\Http\Server\MiddlewareInterface,因此队列本身可以用作通用中间件。

    // subqueue may need no default response if not the last in the main queue
    $subQueue = (new Queue())->add(
        new ResponseTimeMiddleware(),
        new LoggingMiddleware()
    );
    
    // main middleware queue
    $mws = (new Queue(new Response(404)))->add(
        $subQueue,
        new DispatcherMiddleware()
    );

测试

$ composer test

依赖项

  • PHP >= 7.2.0

  • phoole/base 1.*

  • PSR-7 HTTP消息实现,例如guzzle/psr7

许可证