philiprehberger/php-pipeline
Composer 安装命令:
composer require philiprehberger/php-pipeline
包简介
Composable pipeline pattern for processing data through ordered stages
README 文档
README
Composable pipeline pattern for processing data through ordered stages.
Requirements
- PHP 8.2+
Installation
composer require philiprehberger/php-pipeline
Usage
Basic Pipeline
use PhilipRehberger\Pipeline\Pipeline; $result = Pipeline::send('hello world') ->through([ fn (string $value, \Closure $next) => $next(strtoupper($value)), fn (string $value, \Closure $next) => $next(str_replace(' ', '-', $value)), ]) ->thenReturn(); // "HELLO-WORLD"
Class-Based Stages
Implement the Stage contract for reusable, testable stages:
use PhilipRehberger\Pipeline\Contracts\Stage; use Closure; class TrimStage implements Stage { public function handle(mixed $passable, Closure $next): mixed { return $next(trim($passable)); } } $result = Pipeline::send(' hello ') ->pipe(TrimStage::class) ->thenReturn(); // "hello"
Conditional Stages
$isAdmin = true; $result = Pipeline::send($data) ->pipe(ValidateStage::class) ->when($isAdmin, AdminEnrichStage::class) ->unless($isAdmin, GuestFilterStage::class) ->process();
Pipeline Context
Share state between stages using PipelineContext:
use PhilipRehberger\Pipeline\Pipeline; use PhilipRehberger\Pipeline\PipelineContext; $context = new PipelineContext(); $result = Pipeline::send($data) ->withContext($context) ->through([ function (mixed $value, \Closure $next, PipelineContext $ctx) { $ctx->set('started_at', microtime(true)); return $next($value); }, function (mixed $value, \Closure $next, PipelineContext $ctx) { // Access values set by earlier stages $started = $ctx->get('started_at'); return $next($value); }, ]) ->thenReturn(); // Read context after pipeline completes $context->all();
Tap
Add side-effect stages that observe the payload without modifying it:
$result = Pipeline::send('hello') ->pipe(fn (string $value, \Closure $next) => $next(strtoupper($value))) ->tap(function (string $value) { logger()->info('After uppercase: ' . $value); }) ->thenReturn(); // "HELLO" — tap does not change the payload
Validation Checkpoints
Insert validation steps between stages to abort the pipeline on invalid data:
$result = Pipeline::send(10) ->pipe(fn (mixed $value, \Closure $next) => $next($value * 2)) ->checkpoint(fn (mixed $value) => $value <= 100) ->pipe(fn (mixed $value, \Closure $next) => $next($value + 1)) ->process(); // 21
If the checkpoint returns false or throws, a CheckpointFailedException is thrown.
Stage Profiling
Profile stage execution time and memory usage:
use PhilipRehberger\Pipeline\Pipeline; $result = Pipeline::send('hello') ->pipe(UpperCaseStage::class) ->pipe(AppendSuffixStage::class) ->processWithProfile(); $result->value(); // "HELLO_suffix" $result->stages(); // [{name, duration_ms, memory_delta}, ...] $result->totalDuration(); // Total ms across all stages $result->slowestStage(); // Name of the slowest stage
Pipeline Templates
Register reusable pipeline configurations:
use PhilipRehberger\Pipeline\Pipeline; Pipeline::register('text-cleanup', function (PendingPipeline $p) { $p->pipe(TrimStage::class) ->pipe(UpperCaseStage::class); }); $result = Pipeline::fromTemplate('text-cleanup') ->send(' hello ') ->thenReturn(); // "HELLO"
Typed Exception Handling
Catch specific exception types and recover gracefully:
$result = Pipeline::send($data) ->through([RiskyStage::class]) ->catchException(ValidationException::class, function (\Throwable $e, mixed $passable) { return $passable; // Recover from validation errors only }) ->thenReturn(); // Non-matching exceptions propagate normally
Error Handling
$result = Pipeline::send($data) ->through([RiskyStage::class]) ->onFailure(function (\Throwable $e, mixed $passable) { return $passable; // Return original data on failure }) ->process();
API
| Method | Description |
|---|---|
Pipeline::send(mixed $passable) |
Create a new pipeline with the given data |
Pipeline::register(string $name, callable $builder) |
Register a reusable pipeline template |
Pipeline::fromTemplate(string $name) |
Create a pipeline from a registered template |
Pipeline::hasTemplate(string $name) |
Check if a template is registered |
->through(array $stages) |
Set the array of stages |
->pipe(string|callable $stage) |
Append a single stage |
->when(bool $condition, string|callable $stage) |
Add stage if condition is true |
->unless(bool $condition, string|callable $stage) |
Add stage if condition is false |
->withContext(PipelineContext $context) |
Attach shared context passed to stages |
->tap(callable $fn) |
Add a side-effect stage that does not modify the payload |
->checkpoint(callable $validator) |
Insert a validation checkpoint between stages |
->profile() |
Enable profiling mode |
->catchException(string $class, callable $handler) |
Register a handler for a specific exception type |
->onFailure(callable $handler) |
Register a failure handler |
->process() |
Execute the pipeline and return the result |
->processWithProfile() |
Execute and return a ProfiledResult with timing data |
->thenReturn() |
Alias for process() |
Development
composer install vendor/bin/phpunit vendor/bin/pint --test
Support
If you find this project useful:
License
philiprehberger/php-pipeline 适用场景与选型建议
philiprehberger/php-pipeline 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 58 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「middleware」 「pipeline」 「Chain」 「Stages」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 philiprehberger/php-pipeline 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 philiprehberger/php-pipeline 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 philiprehberger/php-pipeline 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Pipeline
Slim Framework 3 CSRF protection middleware utilities
Cloudflare Middleware For Guzzle
Simple asset management for Laravel 4.
PSR-15 middleware to handle CSRF-token verification
统计信息
- 总下载量: 58
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-15