ztsu/reacon

PSR-7 中间件运行器,兼容 PSR-15

1.0.0 2018-03-07 08:03 UTC

This package is not auto-updated.

Last update: 2024-09-09 06:56:54 UTC


README

Build Status Coverage Status

最简单的 PSR-15 兼容中间件运行器。

PSR-15 提供了定义与 PSR-7 HTTP 消息 兼容的服务器中间件组件接口的标准推荐。

有几个其他与 PSR-15 兼容的调度器。其中最著名的是 Middleman。还有许多 PSR-15 中间件组件收集在 middlewares/psr15-middlewares 中。

安装

通过 Composer

$ composer require ztsu/reacon

使用方法

<?php

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class CreateRequestMiddleware implements MiddlewareInterface
{
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ): ResponseInterface {
        return new Zend\Diactoros\Response();
    }
}

class HelloMiddleware implements MiddlewareInterface
{
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ): ResponseInterface {
        $response = $handler->handle($request);

        $response->getBody()->write("Hello, World!");

        return $response;
    }
}

$reacon = new Ztsu\Reacon\Reacon(
    new HelloMiddleware(),
    new CreateRequestMiddleware()
);

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();

$response = $reacon->handle($request);

(new Zend\Diactoros\Response\SapiEmitter)->emit($response);

许可证

MIT.