定制 dakujem/generic-middleware 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dakujem/generic-middleware

最新稳定版本:1.0

Composer 安装命令:

composer require dakujem/generic-middleware

包简介

Generic PSR-15 implementation: Turns callables into Handlers and Middleware.

README 文档

README

PHP from Packagist Test Suite Coverage Status

💿 composer require dakujem/generic-middleware

Contains a couple of classes:

  • GenericMiddleware, an implementation of Psr\Http\Server\MiddlewareInterface
  • GenericHandler, an implementation of Psr\Http\Server\RequestHandlerInterface

Note that I'm using aliases Request, Response and Handler for their respective PSR interface names for brevity.

Consider the following use statements in use:

use Psr\Http\Message\ServerRequestInterface  as Request;
use Psr\Http\Message\ResponseInterface       as Response;
use Psr\Http\Server\RequestHandlerInterface  as Handler;

GenericMiddleware

The GenericMiddleware is a general purpose middleware that turns a callable into a PSR-15 implementation. It accepts any callable with signature fn(Request,Handler):Response.

It can be used for convenient inline middleware implementation:

$app->add(new GenericMiddleware(function(Request $request, Handler $next): Response {
    $request = $request->withAttribute('foo', 42);
    $response = $next->handle($request);
    return $response->withHeader('foo', 'bar');
}));

GenericHandler

The GenericHandler is a general purpose request handler, as per the PSR-15 specification. It turns a callable with signature fn(Request):Response into a PSR-15 implementation.

It can be used for convenient inline handler implementation where you don't want to bother with neither anonymous classes nor named classes:

$kernel = new GenericHandler(
    fn() => new Response(404, 'Not Found')
);
$dispatcher = new MiddlewareDispatcher($kernel);

Testing

Run unit tests using the following command:

$ composer test

Contributing

Ideas, feature requests and other contribution is welcome. Please send a PR or create an issue.

统计信息

  • 总下载量: 4.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unlicense
  • 更新时间: 2020-11-28