定制 snelling/sequence 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

snelling/sequence

Composer 安装命令:

composer create-project snelling/sequence

包简介

README 文档

README

This library provides a different approach to the pipeline pattern. The pipeline pattern is often compared to a production line, where each stage performs a certain operation on a given payload/subject. Stages can act on, manipulate, decorate, or even replace the payload.

The goals of this library are:

  • Be small in size
  • Be simple to understand
  • Make writing code natural

Warning: You may not like the amount of magic that happens internally. With that said, take a look at the examples, and the source code. It's currently at ~150 LOC.

If you would like to know more:

  • Martin Fowler has written extensively about collection pipelines.
  • The PHP League has a very good pipeline package. This package inspired mine.

License

MIT License

Setup

There are two objects

Sequence Object

$sequence = (new Sequence)
    ->then(Callable)
    ->then(Callable, null);

$sequence->run(Payload);

Payload Object

The payload object is a provided way to magically containerize values that you can send through your sequence.

$payload = new Payload();

$payload->number = 1;       // Sets a number object to 1
echo $payload->number;      // Will return 1

$payload->setNumber(2);     // Sets the number object to 2
echo $payload->getNumber(); // Will return 2
echo $payload->number;      // Will return 2

echo $payload->notset;      // Will throw exception

$payload->setNotSet(true);  // Set the notset object
echo $payload->notset;      // Will return true

Basic Example

If you're familiar with League\Pipeline, you'll be right at home. Here is an exact replica of chaining sequences:

class TimesTwoStage
{
    public function __invoke($payload)
    {
        return $payload * 2;
    }
}

class AddOneStage
{
    public function __invoke($payload)
    {
        return $payload + 1;
    }
}

$sequence = (new Sequence)
    ->then(new TimesTwoStage)
    ->then(new AddOneStage);

// Returns 21
$sequence->run(10);

Callable

class TimesTwoStage
{
    public function __invoke($payload)
    {
        return $payload * 2;
    }
}

$sequence = (new Sequence)
    ->then(new TimesTwoStage)
    ->then(function ($payload) {
        return $payload + 1;
    });

// Returns 21
$sequence->run(10);

Re-usable

class TimesTwoStage
{
    public function __invoke($payload)
    {
        return $payload * 2;
    }
}

class AddOneStage
{
    public function __invoke($payload)
    {
        return $payload + 1;
    }
}

$minusSequence = (new Sequence)
    ->then(function ($payload) {
        return $payload - 2;    // 2
    });

$sequence = (new Sequence)
    ->then(new TimesTwoStage)   // 1
    ->then($minusSequence)      // 2
    ->then(new AddOneStage);    // 3

// Returns 19
echo $sequence->run(10);

Exception handling

$sequence = (new Sequence)
    ->then(function () {
        throw new LogicException();
    });

try {
    $sequence->run($payload);
} catch (LogicException $e) {
    // Handle the exception.
}

Payload

$payload         = new Payload();
$payload->number = 0;

$sequence = (new Sequence)
    ->then(function ($payload) {
        $payload->number = 1;

        return $payload;
    });

$payload = $sequence->run($payload);

// Returns 1
echo $payload->number;

Payload return into key

$payload         = new Payload();
$payload->number = 0;

$sequence = (new Sequence)
    ->then(function () {
        return 1;
    }, 'number');

$payload = $sequence->run($payload);

// Returns 1
echo $payload->number;

Payload dependency injection

$payload         = new Payload();
$payload->number = 0;

// Will automatically input $payload->number into $number
$sequence = (new Sequence)
    ->then(function ($number) {
        return ($number + 1);
    }, 'number');

$payload = $sequence->run($payload);

// Returns 1
echo $payload->number;

Payload dependency injection with typehints

class Multiplier
{
    private $multiple;

    public function __construct(float $multiple)
    {
        $this->multiple = $multiple;
    }

    public function getMultiple(): float
    {
        return $this->multiple;
    }
}

class Multiply
{
    public function __invoke(Multiplier $multiplier, int $number)
    {
        return ($number * $multiplier->getMultiple());
    }
}

$payload             = new Payload();
$payload->multiplier = new Multiplier(2);
$payload->number     = 0;

$sequence = (new Sequence)
    ->then(function ($number) {
        return ($number + 1);
    }, 'number')                        // Will return 1
    ->then(new Multiply, 'number');     // Will return 1 * 2 = 2

$payload = $sequence->run($payload);

// Returns 2
echo $payload->number;

snelling/sequence 适用场景与选型建议

snelling/sequence 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 04 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 snelling/sequence 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 snelling/sequence 我们能提供哪些服务?
定制开发 / 二次开发

基于 snelling/sequence 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 17
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-04-20