nhlm/pipechain

具有回退功能的线性可链式管道模式实现

v1.2 2017-03-15 01:42 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:40:50 UTC


README

具有回退功能的线性可链式管道模式实现。

什么是线性管道

线性管道是一个对象,它堆叠回调并提供一个处理逻辑,通过回调堆栈处理有效载荷,从第一个到最后一个。

处理器

PipeChain自带4种不同的处理器

通用调用处理器

通用调用处理器实现了一个队列的处理,将提供的回退回调视为失败阶段回调的后继。

通用调用处理器是默认处理器。

示例

<?php declare(strict_types=1);

use PipeChain\{
    PipeChain
};

$pipeline = new PipeChain();

$pipeline->pipe(
    # the stage callback
    function(string $inbound) {
        return 'something';
    },
    # the fallback callback
    function(int $inbound) {
        return ++$inbound;
    }
);

var_dump(
    $pipeline->process(100) // int(3) => 101
);

天真调用处理器

天真调用处理器实现了一个队列的处理,忽略了提供的回退回调。

示例

<?php declare(strict_types=1);

use PipeChain\{
    PipeChain,
    InvokerProcessors\NaiveInvokerProcessor
};

$pipeline = new PipeChain(new NaiveInvokerProcessor());

$pipeline->pipe(function(int $inbound) {
    return ++$inbound;
});

var_dump(
    $pipeline->process(100) // int(3) => 101
);

忠实接口调用处理器

忠实接口调用处理器实现了一个队列的处理,类似于通用调用处理器,但增加了对每个有效载荷变化的接口检查。

接口检查有2种模式

  • LoyalInterfaceInvokerProcessor::INTERRUPT_ON_MISMATCH 强制处理器将可抛出对象传递到下一个作用域。
  • LoyalInterfaceInvokerProcessor::RESET_ON_MISMATCH 强制处理器使用之前的有效载荷。

示例

<?php declare(strict_types=1);

use PipeChain\{
    PipeChain,
    InvokerProcessors\LoyalInterfaceInvokerProcessor
};

$pipeline = new PipeChain(
    new LoyalInterfaceInvokerProcessor(
        DateTimeInterface::class,
        LoyalInterfaceInvokerProcessor::RESET_ON_MISMATCH
    )
);

$prior = function($inbound) {
    if ( ! $inbound instanceof DateTime ) {
        throw new Exception('Whats this?');
    }
    
    return $inbound->modify('+10 hours');
};

$failure = function (string $inbound) use ($prior) {
    return $prior(date_create($inbound));
};

$pipeline->pipe($prior, $failure);

var_dump(
    $pipeline
        ->process('2017-10-12 18:00:00')
        ->format('Y-m-d') // string(10) "2017-10-13"
);

忠实类型调用处理器

忠实类型调用处理器实现了一个队列的处理,类似于忠实接口调用处理器,但对每个有效载荷变化进行类型检查。

类型检查有2种模式

  • LoyalInterfaceInvokerProcessor::INTERRUPT_ON_MISMATCH 强制处理器将可抛出对象传递到下一个作用域。
  • LoyalInterfaceInvokerProcessor::RESET_ON_MISMATCH 强制处理器使用之前的有效载荷。

示例

<?php declare(strict_types=1);

use PipeChain\{
    PipeChain,
    InvokerProcessors\LoyalTypeInvokerProcessor
};

$pipeline = new PipeChain(
    new LoyalTypeInvokerProcessor(
        'string',
        LoyalTypeInvokerProcessor::RESET_ON_MISMATCH
    )
);

$pipeline->pipe(function(string $name) {
    return strtolower($name);
});
$pipeline->pipe(function(string $name) {
    return ucfirst($name);
});

var_dump(
    $pipeline->process('jOhN') // string(4) "John"
);

启动自己的实现

<?php declare(strict_types=1);

namespace Somewhat;

use PipeChain\{
    PipeChain,
    InvokerProcessorInterface as Processor,
    PipeChainCollectionInterface as Container
};

class MyFirstPipeline extends PipeChain {
    
    protected function boot(Container $container, Processor $processor = null): Processor
    {
        # pipe something. The signature of PipeChainCollectionInterface::attach() is equal
        # to PipeChain::pipe(..).
        
        $container->attach(
            # stage callable
            function() {
                
            },
            # fallback callable (optional)
            function() {
                
            }
        );
        
        # ...
        
        # Don't forget to call the parent, the parent method ensures
        # that a processor will be created when $processor is null.
        
        return parent::boot($container, $processor);
    }
}

# later ...

echo MyFirstPipeline::create()->process('I can get no satisfaction!');

链式管道

<?php declare(strict_types=1);

$pipeline = MyFirstPipeline::create()->chain(
    MySecondPipeline::create()
)->chain(
    MyThirdPipeline::create()
);

$pipeline->process('This is awesome.');

维护者,本包的状态和许可证

以下是本包的维护者

本软件包在MIT许可证下发布。许可证副本位于本存储库的根目录中。

除非添加单元测试,否则本包的状态不稳定。

待办事项

  • 添加单元测试