phapi/pipeline
最新稳定版本:1.0.0
Composer 安装命令:
composer require phapi/pipeline
包简介
Phapi Pipeline is a middleware pipeline handling the middleware queue. Phapi Pipeline relies on PSR-7
关键字:
README 文档
README
Phapi relies heavily on middleware. Almost everything is a middleware: routing, content negotiation and many other things.
Why middleware?
The benefits are many. Middleware are easy to replace and from a performance perspective they can, if implemented right, bypass a lot of code that doesn't need to run in specific circumstances, for example, if a client hits their rate limit there is no point in executing routing, dispatching etc.
Installation
The package is installed by default by the Phapi framework. Installing the package to use is separately can be done by using composer:
$ composer require phapi/pipeline:1.*
Usage
Attaching a new middleware to the pipeline are done by using the pipe() method.
<?php $pipeline = new Phapi\Middleware\Pipeline(); $pipeline->pipe(new Middleware\Cors());
Please note that middleware's will be called in the order they are attached.
A middleware CAN implement the Middleware Interface, but MUST be callable. All middleware WILL be called with three parameters: Request, Response and Next. A middleware should invoke the $next variable and pass the $request, $response and $next parameters. A middleware must also return a response.
Once the queue is empty the resulted response instance will be returned.
Error handling
Implementing the ErrorMiddleware interface enables the pipeline to be able to handle errors. The middleware must take care of the error handling and call the prepareErrorQueue() method. This will trigger a reset in the pipeline and only the middleware added before (usually serializer middleware and a middleware sending the response to the client) the ErrorMiddleware will be called.
Create your own middleware class
Middleware requires a request and a response and a callback ($next) that is called if the middleware allows further middleware to process the request. If a middleware doesn't need or desire to allow further processing it should not call the callback ($next) and should instead return a response. Note that $next can be null.
The main task of a middleware is to process an incoming request and/or the response. The middleware must accept a request and a response (PSR-7 compatible) instance and do something with them.
The middleware must pass a request and a response to the ($next) callback and finally either return the response from the ($next) callback or by modifying the response before returning it.
Examples:
<?php public function __invoke( Request $request, Response $response, callable $next) { // Do something ... return $next($request, $response, $next); }
OR
<?php public function __invoke( Request $request, Response $response, callable $next) { // Do something ... $response = $next($request, $response, $next); // Modify response ... return $response; }
If the middleware should break the pipeline, example: the client hit the rate limit, it should return a response instead of invoking $next.
Example:
<?php public function __invoke( Request $request, Response $response, callable $next) { if ($this->tooManyRequests()) { // Set appropriate headers and body // Return response return $response; } return $next($request, $response, $next); }
Dependency Injection Container
If you implement a method named setContainer() the pipeline will call that method and provide the Dependency Injector Container that can be used in the __invoke() method.
Example:
<?php public function setContainer($container) { $this->container = $container; }
Complete example
Here is a complete example of how a middleware should look. Use this example as a starting point when you wan't to write your own middleware.
<?php namespace Phapi\Middleware\Example; use Phapi\Contract\Di\Container; use Phapi\Contract\Middleware\Middleware; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; /** * Eample middleware * * @category Phapi * @package Phapi\Middleware\Example * @author Peter Ahinko <peter@ahinko.se> * @license MIT (http://opensource.org/licenses/MIT) * @link https://github.com/phapi/middleware-example */ class Example implements Middleware { /** * Dependency injection container * * @var Container */ private $container; /** * Set dependency injection container * * @param Container $container */ public function setContainer(Container $container) { $this->container = $container; } /** * Invoking the middleware * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * @return ResponseInterface $response */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { // Do something ... // Call next middleware $response = $next($request, $response, $next); // Do something with the response ... // Return the response return $response; } }
License
Phapi Middleware Pipeline is licensed under the MIT License - see the license.md file for details
Contribute
Contribution, bug fixes etc are always welcome.
phapi/pipeline 适用场景与选型建议
phapi/pipeline 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.55k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 04 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「middleware」 「pipeline」 「phapi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phapi/pipeline 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phapi/pipeline 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phapi/pipeline 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Phapi Contract is a set of interfaces used by the Phapi framework
Phapi Exceptions
Pipeline
Phapi Dependency Injection Container
Phapi HTTP Message is an implementation of PSR-7 used by the Phapi framework
统计信息
- 总下载量: 1.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 23
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-04-13