当前位置:首页 > PHP

php实现pipeline

2026-02-14 07:22:40PHP

PHP 实现 Pipeline 模式

Pipeline 模式是一种将任务分解为多个步骤并按顺序执行的架构模式。每个步骤处理输入并传递给下一个步骤。以下是 PHP 中实现 Pipeline 模式的几种方法。

基础实现:使用闭包和数组

通过闭包和数组构建一个简单的 Pipeline,逐步传递数据:

php实现pipeline

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

$result = array_reduce(
    $pipeline,
    function ($carry, $step) { return $step($carry); },
    5 // 初始值
);

echo $result; // 输出: (5 * 2 + 10) / 3 = 6.666...

面向对象实现:Pipeline 类

封装一个 Pipeline 类,支持动态添加步骤和链式调用:

php实现pipeline

class Pipeline
{
    private $steps = [];

    public function add(callable $step): self
    {
        $this->steps[] = $step;
        return $this;
    }

    public function execute($input)
    {
        return array_reduce(
            $this->steps,
            function ($carry, $step) { return $step($carry); },
            $input
        );
    }
}

// 使用示例
$pipeline = (new Pipeline())
    ->add(fn($x) => $x * 2)
    ->add(fn($x) => $x + 10)
    ->add(fn($x) => $x / 3);

echo $pipeline->execute(5); // 输出: 6.666...

中间件风格实现

结合中间件模式,支持步骤间更复杂的逻辑(如提前终止):

class MiddlewarePipeline
{
    private $steps = [];

    public function add(callable $step): self
    {
        $this->steps[] = $step;
        return $this;
    }

    public function execute($input)
    {
        $next = function ($input) { return $input; }; // 默认终止函数

        foreach (array_reverse($this->steps) as $step) {
            $next = function ($input) use ($step, $next) {
                return $step($input, $next);
            };
        }

        return $next($input);
    }
}

// 使用示例
$pipeline = (new MiddlewarePipeline())
    ->add(function ($input, $next) {
        if ($input > 100) return "Too large";
        return $next($input * 2);
    })
    ->add(function ($input, $next) {
        return $next($input + 10);
    });

echo $pipeline->execute(50); // 输出: 110
echo $pipeline->execute(200); // 输出: "Too large"

Laravel 风格的 Pipeline

参考 Laravel 框架的实现方式,支持依赖注入和更灵活的处理器:

class LaravelPipeline
{
    private $steps = [];
    private $container;

    public function __construct($container = null)
    {
        $this->container = $container;
    }

    public function through(array $steps): self
    {
        $this->steps = $steps;
        return $this;
    }

    public function execute($input)
    {
        return array_reduce(
            array_reverse($this->steps),
            $this->carry(),
            function ($input) { return $input; }
        )($input);
    }

    private function carry()
    {
        return function ($stack, $step) {
            return function ($input) use ($stack, $step) {
                if (is_callable($step)) {
                    return $step($input, $stack);
                } elseif (is_string($step) && $this->container) {
                    $step = $this->container->make($step);
                }
                return method_exists($step, 'handle')
                    ? $step->handle($input, $stack)
                    : $step($input, $stack);
            };
        };
    }
}

// 使用示例
$pipeline = (new LaravelPipeline())
    ->through([
        function ($input, $next) { return $next($input * 2); },
        function ($input, $next) { return $next($input + 10); },
    ]);

echo $pipeline->execute(5); // 输出: 20

关键点总结

  • 闭包链式调用:通过 array_reduce 实现步骤的连续执行
  • 中间件支持:允许步骤控制是否继续传递(如权限检查)
  • 依赖注入:可通过容器解析步骤的类实例
  • 类型灵活性:支持闭包、类名或对象作为处理步骤

根据项目复杂度选择合适实现,简单场景用基础版本,复杂需求可参考 Laravel 风格实现。

标签: phppipeline
分享给朋友:

相关文章

php实现登录

php实现登录

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

基于php的设计与实现

基于php的设计与实现

PHP 设计与实现的关键要素 环境搭建与框架选择 PHP开发需配置服务器环境(如Apache/Nginx)、PHP版本(建议7.4+)及数据库(MySQL/MariaDB)。主流框架包括Laravel…

php 函数实现

php 函数实现

PHP 函数实现基础 PHP 函数的定义使用 function 关键字,后跟函数名和参数列表。函数名不区分大小写,但建议保持一致。 function functionName($param1, $p…

php实现登陆

php实现登陆

PHP 实现登录功能 创建登录表单 在 HTML 文件中创建一个登录表单,包含用户名和密码输入框,表单的 action 属性指向处理登录的 PHP 文件。 <form action="logi…

php 实现md5

php 实现md5

使用 PHP 内置函数实现 MD5 PHP 提供了 md5() 函数,可以直接计算字符串的 MD5 哈希值。该函数接受一个字符串参数,返回 32 字符的十六进制哈希值。 $string = "H…

php redis 队列实现

php redis 队列实现

PHP Redis 队列实现 Redis 是一个高性能的键值存储系统,常被用作消息队列。以下是使用 PHP 和 Redis 实现队列的几种方法。 安装依赖 确保已安装 PHP Redis 扩展。可以…