定制 gamringer/pipe 二次开发

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

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

gamringer/pipe

最新稳定版本:1.1

Composer 安装命令:

composer require gamringer/pipe

包简介

PSR-15 HTTP Middleware Dispatcher

README 文档

README

A PSR-15 handler with no assumption

License

Pipe is licensed under the MIT license.

Installation

$ composer require gamringer/pipe

Tests

$ composer install
$ phpunit

Documentation

These are all the various ways in which Pipe can be used. These examples can be found in the examples directory. First, let the following code simply be assumed present in all examples:

<?php

declare(strict_types=1);

include __DIR__.'/../vendor/autoload.php';

use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Psr7\Response;
use gamringer\Pipe\Pipe;
use gamringer\Pipe\Example\StaticMiddleware;

// Request to be handled later
$request = new ServerRequest('GET', '/');

// For use in this example, we build a few Middleware objects

// One that simply returns a predetermined response in all cases
$staticResponse = new Response();
$staticResponseMiddleware = new StaticMiddleware($staticResponse);

// One that echoes it's constructor argument before passing to the next one.
$fooMiddleware = new FooMiddleware('1');

Create pipe from an array of Middlewares

// Build Pipe from an array of Middlewares
$pipe = new Pipe([
    $staticResponseMiddleware,
]);

// Use Pipe via RequestHandlerInterface
$response = $pipe->handle($request);

Create an empty pipe and add Middlewares dynamically

// Build empty Pipe
$pipe = new Pipe();

// Add Middlewares dynamically
$pipe->push($staticResponseMiddleware);

// Use Pipe via RequestHandlerInterface
$response = $pipe->handle($request);

Stack many Middlewares dynamically

// Build empty Pipe
$pipe = new Pipe();

// Add Middlewares dynamically
$pipe->push($fooMiddleware);
$pipe->push($staticResponseMiddleware);

// Use Pipe via RequestHandlerInterface
$response = $pipe->handle($request);

Use pipe as a Middleware

Pipe can also be used via the MiddlewareInterface by pushing it into another Pipe

// Build empty Pipe
$pipe = new Pipe();

// Add Middlewares dynamically
$pipe->push($fooMiddleware);
$pipe->push(new Pipe([
    new FooMiddleware('2') 
]));
$pipe->push($staticResponseMiddleware);

// Use Pipe via RequestHandlerInterface
$response = $pipe->handle($request);

Running out of Middlewares

If it runs out of middlewares to call, Pipe will respond differently depending on the way it was called.

As a RequestHandler

Most applications will be using Pipe as a RequestHandler to stack Middlewares and handle user requests. If the request goes through the Pipe and the last Middleware attempts to delegate to the next one, a TerminalException is raised. The same logic also covers the empty Pipe case:

$pipe = new Pipe();
$pipe->handle($request); // Throws new \gamringer\Pipe\TerminalException()

As a Middleware

If called as a Middleware, when running out of Middlewares itself, it will simply pass along to its next sibling:

// Build empty Pipe
$pipe = new Pipe();

// Add Middlewares dynamically
$pipe->push($fooMiddleware);
$pipe->push(new Pipe([
    new FooMiddleware('2')
]));
$pipe->push(new Pipe());
$pipe->push($staticResponseMiddleware);

// Use Pipe via RequestHandlerInterface
$response = $pipe->handle($request);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-10-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固