当前位置:首页 > PHP

php实现pipeline

2026-02-28 06:27:18PHP

PHP 实现 Pipeline 模式

Pipeline(管道)模式是一种将任务分解为多个步骤的设计模式,每个步骤处理输入并传递给下一个步骤。在 PHP 中可以通过闭包、生成器或类实现。

使用闭包实现 Pipeline

通过闭包链式调用实现 Pipeline,每个闭包处理数据并传递给下一个闭包:

php实现pipeline

function pipeline(array $stages): callable
{
    return function ($payload) use ($stages) {
        return array_reduce(
            $stages,
            function ($carry, $stage) {
                return $stage($carry);
            },
            $payload
        );
    };
}

// 示例使用
$process = pipeline([
    function ($input) { return $input * 2; },
    function ($input) { return $input + 10; },
    function ($input) { return $input / 3; }
]);

echo $process(5); // 输出 6.666...

使用生成器实现 Pipeline

生成器可以按需处理数据流,适合大数据量场景:

function generatorPipeline(array $stages): Generator
{
    $value = yield;
    foreach ($stages as $stage) {
        $value = $stage($value);
        yield $value;
    }
}

// 示例使用
$pipeline = generatorPipeline([
    function ($x) { return $x + 1; },
    function ($x) { return $x * 2; }
]);

$pipeline->send(5);
echo $pipeline->current(); // 输出 12

面向对象实现 Pipeline

通过类封装 Pipeline 逻辑,提供更好的可扩展性:

php实现pipeline

class Pipeline
{
    private $stages = [];

    public function addStage(callable $stage): self
    {
        $this->stages[] = $stage;
        return $this;
    }

    public function process($payload)
    {
        foreach ($this->stages as $stage) {
            $payload = $stage($payload);
        }
        return $payload;
    }
}

// 示例使用
$pipeline = (new Pipeline())
    ->addStage(function ($x) { return $x * 3; })
    ->addStage(function ($x) { return $x - 1; });

echo $pipeline->process(5); // 输出 14

中间件风格的 Pipeline

适合 Web 中间件场景的实现方式:

class MiddlewarePipeline
{
    private $middlewares = [];
    private $index = 0;

    public function addMiddleware(callable $middleware): void
    {
        $this->middlewares[] = $middleware;
    }

    public function handle($request, $next)
    {
        if (!isset($this->middlewares[$this->index])) {
            return $next($request);
        }

        $middleware = $this->middlewares[$this->index];
        $this->index++;

        return $middleware($request, function ($request) use ($next) {
            return $this->handle($request, $next);
        });
    }
}

// 示例使用
$pipeline = new MiddlewarePipeline();
$pipeline->addMiddleware(function ($req, $next) {
    $req .= " middleware1";
    return $next($req);
});
$pipeline->addMiddleware(function ($req, $next) {
    $req .= " middleware2";
    return $next($req);
});

$result = $pipeline->handle("request", function ($req) {
    return $req . " final";
});

echo $result; // 输出 "request middleware1 middleware2 final"

Laravel 风格的 Pipeline

Laravel 框架提供了 Pipeline 实现,可以单独使用:

use Illuminate\Pipeline\Pipeline;

$pipeline = new Pipeline();

$result = $pipeline->send('data')
    ->through([
        function ($passable, $next) {
            return $next($passable . '-processed1');
        },
        function ($passable, $next) {
            return $next($passable . '-processed2');
        }
    ])
    ->then(function ($passable) {
        return $passable . '-final';
    });

echo $result; // 输出 "data-processed1-processed2-final"

每种实现方式适用于不同场景,闭包方式适合简单数据处理,生成器适合流式处理,面向对象方式提供更好扩展性,中间件风格适合请求处理流程。

标签: phppipeline
分享给朋友:

相关文章

php实现验证码

php实现验证码

PHP实现验证码的方法 使用GD库生成验证码 GD库是PHP中处理图像的扩展,可用于生成验证码图片。 <?php session_start(); $width = 120; $height…

php实现登录

php实现登录

PHP 登录功能实现 数据库连接与用户表创建 创建一个数据库连接文件 db.php,用于管理数据库连接。MySQL 示例代码如下: <?php $host = 'localhost'; $d…

php实现排序

php实现排序

PHP 数组排序方法 PHP 提供了多种内置函数用于数组排序,适用于不同的排序需求。以下是常见的排序方法: 按值升序排序 sort() 函数对数组按值升序排序,重置键名(索引数组适用): $num…

php 实现验证码

php 实现验证码

使用 PHP 生成验证码 验证码(CAPTCHA)用于防止自动化脚本提交表单,以下是几种常见的 PHP 实现方法。 使用 GD 库生成图片验证码 GD 库是 PHP 的图像处理扩展,可用于生成验证…

php实现文件的下载

php实现文件的下载

PHP 实现文件下载的方法 在 PHP 中实现文件下载功能通常需要设置正确的 HTTP 头信息,并输出文件内容。以下是几种常见的实现方式: 使用 header() 函数强制下载 通过设置 Conte…

php实现一个

php实现一个

PHP 实现一个简单的计算器 创建一个基本的计算器,支持加、减、乘、除运算。以下是一个完整的实现代码示例: <?php $result = ""; if ($_SERVER["REQUEST_…