PHPackages                             philiprehberger/php-pipeline - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. philiprehberger/php-pipeline

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

philiprehberger/php-pipeline
============================

Composable pipeline pattern for processing data through ordered stages

v1.3.0(3mo ago)138MITPHPPHP ^8.2CI passing

Since Mar 15Pushed 1mo agoCompare

[ Source](https://github.com/philiprehberger/php-pipeline)[ Packagist](https://packagist.org/packages/philiprehberger/php-pipeline)[ Docs](https://github.com/philiprehberger/php-pipeline)[ GitHub Sponsors](https://github.com/philiprehberger)[ RSS](/packages/philiprehberger-php-pipeline/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (6)Versions (9)Used By (0)

PHP Pipeline
============

[](#php-pipeline)

[![Tests](https://github.com/philiprehberger/php-pipeline/actions/workflows/tests.yml/badge.svg)](https://github.com/philiprehberger/php-pipeline/actions/workflows/tests.yml)[![Latest Version on Packagist](https://camo.githubusercontent.com/b74d918bd62edcfa1d6789a538a8ebe450d26ec80231b02ebf00f08a92b4838c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7068696c69707265686265726765722f7068702d706970656c696e652e737667)](https://packagist.org/packages/philiprehberger/php-pipeline)[![Last updated](https://camo.githubusercontent.com/60863a41cd6c0d58e6916109523d797fa93e88ac07d31080d4e78fd6aae0c45f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f7068696c69707265686265726765722f7068702d706970656c696e65)](https://github.com/philiprehberger/php-pipeline/commits/main)

Composable pipeline pattern for processing data through ordered stages.

Requirements
------------

[](#requirements)

- PHP 8.2+

Installation
------------

[](#installation)

```
composer require philiprehberger/php-pipeline
```

Usage
-----

[](#usage)

### Basic Pipeline

[](#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

[](#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

[](#conditional-stages)

```
$isAdmin = true;

$result = Pipeline::send($data)
    ->pipe(ValidateStage::class)
    ->when($isAdmin, AdminEnrichStage::class)
    ->unless($isAdmin, GuestFilterStage::class)
    ->process();
```

### Pipeline Context

[](#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

[](#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

[](#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 pipe(fn (mixed $value, \Closure $next) => $next($value + 1))
    ->process();

// 21
```

If the checkpoint returns false or throws, a `CheckpointFailedException` is thrown.

### Stage Profiling

[](#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

[](#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

[](#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

[](#error-handling)

```
$result = Pipeline::send($data)
    ->through([RiskyStage::class])
    ->onFailure(function (\Throwable $e, mixed $passable) {
        return $passable; // Return original data on failure
    })
    ->process();
```

API
---

[](#api)

MethodDescription`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
-----------

[](#development)

```
composer install
vendor/bin/phpunit
vendor/bin/pint --test
```

Support
-------

[](#support)

If you find this project useful:

⭐ [Star the repo](https://github.com/philiprehberger/php-pipeline)

🐛 [Report issues](https://github.com/philiprehberger/php-pipeline/issues?q=is%3Aissue+is%3Aopen+label%3Abug)

💡 [Suggest features](https://github.com/philiprehberger/php-pipeline/issues?q=is%3Aissue+is%3Aopen+label%3Aenhancement)

❤️ [Sponsor development](https://github.com/sponsors/philiprehberger)

🌐 [All Open Source Projects](https://philiprehberger.com/open-source-packages)

💻 [GitHub Profile](https://github.com/philiprehberger)

🔗 [LinkedIn Profile](https://www.linkedin.com/in/philiprehberger)

License
-------

[](#license)

[MIT](LICENSE)

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance87

Actively maintained with recent releases

Popularity9

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

8

Last Release

91d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cfd7d24cbbf32400fa13ce0bbe7a31edd2d66a6d4488eafdb3d64c5337bf0435?d=identicon)[philiprehberger](/maintainers/philiprehberger)

---

Top Contributors

[![philiprehberger](https://avatars.githubusercontent.com/u/8218077?v=4)](https://github.com/philiprehberger "philiprehberger (13 commits)")

---

Tags

phpmiddlewarepipelineChainStages

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/philiprehberger-php-pipeline/health.svg)

```
[![Health](https://phpackages.com/badges/philiprehberger-php-pipeline/health.svg)](https://phpackages.com/packages/philiprehberger-php-pipeline)
```

###  Alternatives

[imanghafoori/laravel-middlewarize

Use laravel middlewares on any method calls in your app

1124.5k1](/packages/imanghafoori-laravel-middlewarize)[aldemeery/onion

A layering mechanism for PHP applications.

5812.4k3](/packages/aldemeery-onion)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
