league/pipeline
Composer 安装命令:
composer require league/pipeline
包简介
A plug and play pipeline implementation.
README 文档
README
This package provides a pipeline pattern implementation.
Pipeline Pattern
The pipeline pattern allows you to easily compose sequential stages by chaining stages.
In this particular implementation the interface consists of two parts:
- StageInterface
- PipelineInterface
A pipeline consists of zero, one, or multiple stages. A pipeline can process a payload. During the processing the payload will be passed to the first stage. From that moment on the resulting value is passed on from stage to stage.
In the simplest form, the execution chain can be represented as a foreach:
$result = $payload; foreach ($stages as $stage) { $result = $stage($result); } return $result;
Effectively this is the same as:
$result = $stage3($stage2($stage1($payload)));
Immutability
Pipelines are implemented as immutable stage chains. When you pipe a new stage, a new pipeline will be created with the added stage. This makes pipelines easy to reuse, and minimizes side-effects.
Usage
Operations in a pipeline, stages, can be anything that satisfies the callable
type-hint. So closures and anything that's invokable is good.
$pipeline = (new Pipeline)->pipe(function ($payload) { return $payload * 10; });
Class based stages.
Class based stages are also possible. The StageInterface can be implemented which
ensures you have the correct method signature for the __invoke method.
use League\Pipeline\Pipeline; use League\Pipeline\StageInterface; class TimesTwoStage implements StageInterface { public function __invoke($payload) { return $payload * 2; } } class AddOneStage implements StageInterface { public function __invoke($payload) { return $payload + 1; } } $pipeline = (new Pipeline) ->pipe(new TimesTwoStage) ->pipe(new AddOneStage); // Returns 21 $pipeline->process(10);
Re-usable Pipelines
Because the PipelineInterface is an extension of the StageInterface pipelines can be re-used as stages. This creates a highly composable model to create complex execution patterns while keeping the cognitive load low.
For example, if we'd want to compose a pipeline to process API calls, we'd create something along these lines:
$processApiRequest = (new Pipeline) ->pipe(new ExecuteHttpRequest) // 2 ->pipe(new ParseJsonResponse); // 3 $pipeline = (new Pipeline) ->pipe(new ConvertToPsr7Request) // 1 ->pipe($processApiRequest) // (2,3) ->pipe(new ConvertToResponseDto); // 4 $pipeline->process(new DeleteBlogPost($postId));
Pipeline Builders
Because pipelines themselves are immutable, pipeline builders are introduced to facilitate distributed composition of a pipeline.
The pipeline builders collect stages and allow you to create a pipeline at any given time.
use League\Pipeline\PipelineBuilder; // Prepare the builder $pipelineBuilder = (new PipelineBuilder) ->add(new LogicalStage) ->add(new AnotherStage) ->add(new LastStage); // Build the pipeline $pipeline = $pipelineBuilder->build();
Exception handling
This package is completely transparent when dealing with exceptions. In no case will this package catch an exception or silence an error. Exceptions should be dealt with on a per-case basis. Either inside a stage or at the time the pipeline processes a payload.
$pipeline = (new Pipeline)->pipe(function () { throw new LogicException(); }); try { $pipeline->process($payload); } catch(LogicException $e) { // Handle the exception. }
league/pipeline 适用场景与选型建议
league/pipeline 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.26M 次下载、GitHub Stars 达 1k, 最近一次更新时间为 2015 年 06 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「pattern」 「pipeline」 「composition」 「design pattern」 「sequential」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 league/pipeline 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 league/pipeline 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 league/pipeline 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Functional programming utilities for composing, decorating, and controlling function execution
Function composition.
Pipeline
A Laravel package for the Repository Design Pattern.
Inbox pattern process implementation for your Laravel Applications
A library for simple pattern matching.
统计信息
- 总下载量: 17.26M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1018
- 点击次数: 39
- 依赖项目数: 84
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-24