webimpress / http-middleware-compatibility
Requires
- php: ^5.6 || ^7.0
- http-interop/http-middleware: ^0.1.1 || ^0.2 || ^0.3 || ^0.4.1 || ^0.5
- webimpress/composer-extra-dependency: ^0.2.2
Requires (Dev)
- phpunit/phpunit: ^5.7.23 || ^6.4.3
This package is auto-updated.
Last update: 2019-02-20 19:52:29 UTC
README
目的
库的目的是为不同版本的 http-interop/http-middleware
提供一致的用户界面,这些版本实现了 PSR-15 HTTP 中间件草案。
许多项目目前使用不同版本的 http-interop/http-middleware
库,升级到最新版本通常需要主要版本更新。该库允许组件消费者决定他们想使用哪个版本的 http-interop/http-middleware
,并允许他们在任何时间迁移到最新版本。
使用方法
您的中间件应该实现接口 Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface
,并且对于委托者/请求处理器,您应该使用接口 Webimpress\HttpMiddlewareCompatibility\HandlerInterface
。
<?php namespace MyNamespace; use Psr\Http\Message\ServerRequestInterface; use Webimpress\HttpMiddlewareCompatibility\HandlerInterface; use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface; class MyMiddleware implements MiddlewareInterface { public function process(ServerRequestInterface $request, HandlerInterface $handler) { // ... } }
这两个接口只是别名。它允许您的中间件与当前安装的 http-interop/http-middleware
库版本一起工作。
委托/处理器方法
在不同版本的 http-interop/http-middleware
中方法名已更改。最初方法名为 next
(版本 0.1.1),然后是 process
(从 0.2 - 0.4.1),最后在 0.5.0 中变为 handle
。为了实现所有这些,我们提供了常量 Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD
,它具有根据使用的 http-middleware 版本而适当的值。以下是在您的中间件中使用它的示例:
<?php namespace MyNamespace; use Psr\Http\Message\ServerRequestInterface; use Webimpress\HttpMiddlewareCompatibility\HandlerInterface; use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface; use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD; class MyMiddleware implements MiddlewareInterface { public function process(ServerRequestInterface $request, HandlerInterface $handler) { return $handler->{HANDLER_METHOD}($request); } }
就这样!现在,您的组件的消费者可以决定他们想使用哪个版本的 http-interop/http-middleware
。