# php-pipeline **Repository Path**: liuxiaojinla/php-pipeline ## Basic Information - **Project Name**: php-pipeline - **Description**: 基于 league/pipeline 实现几种管道模式,线性调度管道器、线性调度管道器(可中断)、调用中间件管道器等 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-01-09 - **Last Updated**: 2025-12-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Xin/ Pipeline · 轻量级 PHP 管道流库 > 基于 [`league/pipeline`](https://github.com/thephpleague/pipeline) 二次封装,提供**顺序流、可中断流、中间件流**三种常用模式; > 支持「闭包、可调用对象、配置数组」三种方式声明阶段; > 零依赖、可扩展、可快速构建「统一支付」「工作流」「请求拦截链」等复杂流程。 --- ## 安装 ```bash composer require xin/pipeline ``` --- ## 30 秒上手 ```php use Xin\Pipeline\PipelineManager; require 'vendor/autoload.php'; $pipe = new PipelineManager; // 1. 顺序流:一步接一步,全部跑完 $result = $pipe->then('hello', 'demo'); // 输出:HELLO WORLD! // 2. 可中断流:遇到条件提前跳出 $result = $pipe->thenInterruptible('hello', fn($v) => $v === 'HELLO', 'demo'); // 3. 中间件流:双向 next 机制 $result = $pipe->through('hello', fn($v,$next) => $next(strtoupper($v)), 'demo'); ``` --- ## 三种核心模式 | 模式 | 方法 | 特点 | 小箭头记忆 | |---|---|---|---| | **顺序流** | `then()` | 阶段依次执行,不可中断 | `->->->->` | | **可中断顺序流** | `thenInterruptible()` | 阶段可提前 `break` | `->->|->->` | | **中间件流** | `through()` | 经典 `function($input, $next)` 双向 | `->-><-<-` | --- ## 声明阶段的 3 种姿势 ### ① 闭包 / 可调用对象 ```php $pipe->push(function ($payload) { $payload .= ' A'; return $payload; }); ``` ### ② 配置数组(推荐:配置即代码) ```php $metas = [ ['name' => 'trim'], ['name' => 'upper'], ['handler' => fn($v) => ('👋 ' . $v)], ]; $pipe->pushManyFromMetas($metas, 'global'); ``` ### ③ 自定义工厂(一次注册,多处复用) ```php PipelineManager::customHandlerFactory('upper', fn() => fn($v) => (strtoupper($v))); ``` --- ## 完整示例:构建 ```php use Xin\Pipeline\PipelineManager; $payment = new PipelineManager; // 阶段配置:校验→签名→渠道→结果包装 $stages = [ ['name' => 'validate'], ['name' => 'sign'], ['name' => 'channel'], ['name' => 'format'], ]; $payment->pushManyFromMetas($stages, 'unified'); // 跑起来 $order = ['amount' => 100, 'currency' => 'CNY']; $response = $payment->then($order, 'unified'); var_dump($response); // 最终支付网关返回结构 ``` --- ## 静态快捷方法 ```php // 直接根据配置数组跑顺序流 $response = PipelineManager::thenFromMetas($stages, $input); // 直接跑可中断顺序流 $response = PipelineManager::thenInterruptibleFromMetas($stages, $input, $check); ``` --- ## 高级:自定义处理器 实现 `League\Pipeline\ProcessorInterface` 即可插入自己的调度逻辑: ```php class AsyncProcessor implements ProcessorInterface { public function process($payload, StageInterface ...$stages) { // 你的异步/并发逻辑 } } ``` --- ## API 速查 | 目的 | 代码 | |---|---| | 顺序流 | `$pipe->then($input, $name)` | | 可中断流 | `$pipe->thenInterruptible($input, callable $check, $name)` | | 中间件流 | `$pipe->through($input, callable $destination, $name)` | | 批量注册 | `$pipe->pipesFromMetas(array $metas, $name)` | | 静态顺序流 | `PipelineManager::thenFromMetas($metas, $input, $name)` | | 静态可中断流 | `PipelineManager::thenInterruptibleFromMetas($metas, $input, $name)` | --- ## 兼容性 - PHP ≥ 7.3 - 依赖仅 `league/pipeline`(自动安装) --- ## 许可证 Apache 2.0