alexmanno/pipeline-remix

重新设计的PHP管道

0.0.2 2017-11-26 09:50 UTC

This package is auto-updated.

Last update: 2024-09-22 21:25:23 UTC


README

重新设计的PHP管道

Latest Stable Version Latest Unstable Version GitHub license composer.lock available Scrutinizer Code Quality Code Coverage Build Status

什么是管道

管道是一系列数据处理元素的集合,这些元素以串联方式连接,其中某个元素的输出是下一个元素的输入。

安装

从Composer安装

要使用此包,请使用Composer

  • 从CLI: composer require alexmanno/pipeline-remix
  • 或者,直接在您的 composer.json
{
    "require": {
        "alexmanno/pipeline-remix": "dev-master"
    }
}

架构

管道

管道是一个简单的SplQueue的Stage。

您可以通过这种方式初始化它:$pipeline = new AlexManno\Remix\Pipelines\Pipeline();

您可以使用 $pipeline->pipe($stage) 将Stage添加到Pipeline

阶段

阶段是一个实现 AlexManno\Remix\Pipelines\Interfaces\StageInterface 的对象,并具有一个 __invoke() 方法。

此对象是管道中最小的一部分,它应该是一个单一操作。

您可以创建一个实现该接口的类。

例如:

 class MyCoolStage implements AlexManno\Remix\Pipelines\Interfaces\StageInterface
 {
    /**
     * @param Payload $payload
     */
    public function __invoke(Payload $payload)
    {
        $payload->setData('Hello!');
        
        return $payload;
    }
}

有效载荷

有效载荷是一个实现 PayloadInterface 的对象,可以存储任何类型的数据。例如,在Web应用程序中,它可以存储 RequestResponse

例如:

class Payload implements AlexManno\Remix\Pipelines\Interfaces\PayloadInterface
{
    /** @var RequestInterface */
    public $request;
    /** @var ResponseInterface */
    public $response;
}

组合并运行您的管道

如果您已经初始化了Payload对象和Stage对象,则可以组合您的管道。

例如:

// -- Initialized objects: $payload, $pipeline, $stage1, $stage2 --

$pipeline
    ->pipe($stage1) // Add $stage1 to queue
    ->pipe($stage2); // Add $stage2 to queue

$pipeline($payload);  // Run pipeline: invoke $stage1 and then $stage2 with payload from $stage1

您还可以使用 add() 方法将两个或更多管道组合在一起。

例如:

// -- Initialized objects: $payload, $pipeline1, $pipeline2, $stage1, $stage2 --

$pipeline1->pipe($stage1); // Add $stage1 to $pipeline1
$pipeline2->pipe($stage2); // Add $stage2 to $pipeline2

$pipeline1->add($pipeline2); // Add stages from $pipeline2

$payload = $pipeline1($payload); // Run pipeline: invoke $stage1 (from $pipeline1) and then $stage2 (from $pipeline2) with payload from $stage1

示例

结论

希望您觉得这个repo很有用。感谢您的关注。

@alexmanno 制作 ❤️