php-pm/psr7-adapter

此包已被废弃,不再维护。未建议替代包。

PHP-PM 的 PSR-7 中间件桥梁

dev-master 2017-11-30 09:01 UTC

This package is not auto-updated.

Last update: 2020-01-24 16:16:28 UTC


README

[已弃用] PSR-7 兼容性现已直接集成到 php-pm 中。

PHP-PM PSR-7 适配器

PSR-7 适配器,用于在 PHP-PM 中使用 PSR-7 中间件应用。请参阅 https://github.com/php-pm/php-pm

设置

composer require php-pm/psr7-adapter

用法

PPM 首次启动您的应用程序,然后将所有传入的请求传递到此实例。此实例需要是一个 PSR-7 兼容的接口,这意味着它需要实现以下接口

public function __invoke($request, $response, $next = null)

因此,为了与该适配器兼容,您需要实现一个类,当实例化时,设置您的应用程序,并实现如上所述的 __invoke 方法。

例如,如果您使用 Zend 的 Stratigility 库,则您的引导程序可能如下所示

namespace Your\App;

use Zend\Stratigility\MiddlewarePipe;

class Middleware
{
    protected $pipe;

    public function __construct()
    {
        // Set up the application

        $this->pipe = new MiddlewarePipe;

        $this->pipe->pipe(new MyFirstMiddleware);
        $this->pipe->pipe(new MySecondMiddleware);
        $this->pipe->pipe(new MyThirdMiddleware);
    }

    public function __invoke($request, $response, $next = null)
    {
        $middleware = $this->pipe;
        return $middleware($request, $response, $next);
    }
}

启动服务器

启动 PPM 时,将中间件作为引导程序传递

vendor/bin/ppm start --bridge=PHPPM\\Psr7\\Psr7Bridge --bootstrap=Your\\App\\Middleware

或者,首先配置 PPM 以默认使用这些选项,然后直接启动它

vendor/bin/ppm config --bridge=PHPPM\\Psr7\\Psr7Bridge --bootstrap=Your\\App\\Middleware
vendor/bin/ppm start