imunew/php-pipeline

此包提供了一种管道模式实现。

v0.3.0 2024-04-13 12:54 UTC

This package is auto-updated.

Last update: 2024-09-13 15:13:46 UTC


README

此包提供了一系列具有 __invoke() 方法的 callable 函数或类的管道。
此包深受 League\Pipeline 的启发。

Quality Assurance

为什么不使用 League\Pipeline

League\Pipeline 只提供了向 pipeline 添加功能。
我想在管道中插入或替换 callable

通过 composer 安装

$ composer require imunew/php-pipeline

基本用法

$pipes = (new Pipes())
    ->add(10, function($context) {
        /** @var ContextInterface $context */
        $data = $context->getData('number', 0);         // 1) $data = 0
        return $context->setData('number', $data + 1);  // 2) $data = 1
    })
    ->add(20, function($context) {
        /** @var ContextInterface $context */
        $data = $context->getData('number', 0);         // 2) $data = 1
        return $context->setData('number', $data * 10); // 3) $data = 10
    })
;

$pipeline = new Pipeline($pipes);
$context = $pipeline->process(new Context());

echo $context->getData('number'); // 10

插入管道

$pipes = (new Pipes())
    ->add(10, function($context) {
        /** @var ContextInterface $context */
        $data = $context->getData('number', 0);         // 1) $data = 0
        return $context->setData('number', $data + 1);  // 2) $data = 1
    })
    ->add(20, function($context) {
        /** @var ContextInterface $context */
        $data = $context->getData('number', 0);         // 3) $data = 3
        return $context->setData('number', $data * 10); // 4) $data = 30
    })
;

// Insert pipe between 10 and 20.
$pipes = $pipes->add(15, function($context) {
    /** @var ContextInterface $context */
    $data = $context->getData('number', 0);            // 2) $data = 1
    return $context->setData('number', $data + 2);     // 3) $data = 3
});

$pipeline = new Pipeline($pipes);
$context = $pipeline->process(new Context());

echo $context->getData('number'); // 30