ircmaxell / tari-php
PHP 的中间件原型概念库
dev-master
2016-05-21 13:21 UTC
Requires
- php: ^7
- psr/http-message: ^1.0
Requires (Dev)
- guzzlehttp/psr7: ^1.3
- phpunit/phpunit: ^5.3
This package is auto-updated.
Last update: 2024-09-20 00:56:27 UTC
README
PHP 的 PSR-7 中间件接口原型
需求
- PHP 7.0
是的,这是唯一的硬性要求
作为终端用户的用法
要使用这个运行器,您需要选择一个 PSR-7 库。我们将使用 Guzzle 的。
首先安装它: composer require guzzle/psr7
现在,我们需要 PSR-7 库的工厂实例;
$factory = new Tari\Adapter\Guzzle\Factory;
接下来,启动“服务器”;
$server = new Tari\Server($factory);
接下来,添加我们想要的任何中间件。在这种情况下,让我们添加错误处理器和 HSTS 中间件;
$server->append(new Tari\ServerMiddleware\ErrorHandler); $server->append(new Tari\ServerMiddleware\HSTS(300 /* Max-age in seconds */));
我们还可以将中间件作为闭包添加(注意我们不需要类型);
$server->append(function($request, $frame) { $response = $frame->next($request); return $response->withHeader('X-Powered-By', 'Tari-PHP'); });
我们还需要一个“默认”操作来执行;
$default = function($request) use ($factory) { // Default to a 404 NOT FOUND response return $factory->createResponse("Not Found", 404); };
最后,我们可以运行我们的栈;
$request = new Guzzle\Psr7\ServerRequest("http://www.example.com/foo", "GET"); $response = $server->run($request, $default);
这就是全部内容...
作为库构建者(服务器模式)的用法
要将此中间件用作库作者,只需实现 Tari\MiddlewareInterface
接口即可。就这么简单
use Tari\ServerMiddlewareInterface; use Tari\ServerFrameInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; class Foo implements ServerMiddlewareInterface { public function handle(ServerRequestInterface $request, ServerFrameInterface $frame): ResponseInterface { // Do your modifications to the request here $response = $frame->next($request); // Do your modifications to the response here return $response; } }
就这么简单。
中断请求
有时,您不想继续请求。如果在您的中间件中检测到这种情况,只需创建一个新的响应即可;
use Tari\ServerMiddlewareInterface; use Tari\ServerFrameInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; class Foo implements ServerMiddlewareInterface { public function handle(ServerRequestInterface $request, ServerFrameInterface $frame): ResponseInterface { if ($this->isBadRequest($request)) { return $frame->factory()->createResponse("Bad Request", 400); } return $frame->next($request); } }
接口
Tari 定义了 3 个可消费的接口
ServerMiddlewareInterface
interface ServerMiddlewareInterface { public function handle(ServerRequestInterface $request, ServerFrameInterface $frame): ResponseInterface; }
用于服务器请求处理
ServerFrameInterface
interface ServerFrameInterface { public function next(ServerRequestInterface $request): ResponseInterface; public function factory(): FactoryInterface; }
这用于处理服务器请求
FactoryInterface
interface FactoryInterface { public function createRequest( UriInterface $uri = null, string $method = '', array $headers = [], $body = null ): RequestInterface; public function createServerRequest( UriInterface $uri = null, string $method = '', array $headers = [], $body = null ): ServerRequestInterface; public function createResponse( int $status = 200, array $headers = [], $body = null ): ResponseInterface; public function createStream($data = null): StreamInterface; public function createUri(string $uri = ''): UriInterface; public function createUploadedFile( $data, int $size, int $error, string $clientFile = '', string $clientMediaType = '' ): UploadedFileInterface; }
这里还有更多的事情在发生,但仍然非常直接和简单。
每个方法都创建一个 PSR-7 对象,并初始化它。
许可证
MIT