gnumast/pipeline

此软件包已被弃用且不再维护。未建议替代软件包。

简单库,用于通过一系列任务发送数据

dev-master 2015-11-10 18:26 UTC

This package is not auto-updated.

Last update: 2020-11-27 21:56:53 UTC


README

Build Status

Pipeline 允许轻松地动态链式操作/任务,或创建可重用的命令链。完整文档可供查看。

安装

composer require gnumast/pipeline

基本用法

这是一个简单的示例。

class MakeAllCaps implements TaskInterface {
    public function run($data) {
        return mb_strtoupper($data);
    }
}

class RemoveAllSpaces implements TaskInterface {
    public function run($data) {
        return str_replace(' ', '', $data);
    }
}

$pipeline = new Pipeline(
    new MakeAllCaps(),
    new RemoveAllSpaces()
);
$pipeline->execute("Hello, my name is Steve"); // HELLO,MYNAMEISSTEVE

对于不需要定义全新的类或需要快速链式组合的情况,CallablePipe 类可以封装匿名函数以作为管道传递。

$pipeline = new Pipeline(
    new CallablePipe(function($data) {
return $data * 10;
    }),
    new CallablePipe(function($data) {
return $data + 50;
    })
);

$result = $pipeline->execute(10); // 150

您不需要在初始化时传递所有任务。Pipeline 提供了一个 add 方法,用于向现有对象添加步骤。

$pipeline = new Pipeline(new MakeAllCaps());
$pipeline->add(new RemoveAllSpaces());
$pipeline->execute("Hello, world!"); // HELLO,WORLD!