定制 corbosman/laravel-pipeline-passable 二次开发

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

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

corbosman/laravel-pipeline-passable

Composer 安装命令:

composer require corbosman/laravel-pipeline-passable

包简介

A Passable class for Laravel's Pipeline

README 文档

README

Latest Version on Packagist Total Downloads

Laravel's Pipeline class only takes a single input parameter. This unfortunately limits how you can use that class to your advantage. This is an opinionated solution using a Passable class that extends Spatie's Data Transfer Object. It's just something I needed myself in several projects, so I extracted it to a package.

Installation

Via Composer

$ composer require corbosman/laravel-pipeline-passable

Usage

You should be familiar on how Laravel Pipelines work. They're used in the Router and Middleware. It lets you pass a variable through a set of classes, and return the result. Sort of like array_reduce. Since you can't pass in multiple variables, it's not possible to send in some kind of input and output that are separate. That's where this package comes in. It allows you to create a class with multiple properties, and in the end return one of the properties as the result of the pipeline. By default, it just returns the class itself as the result, but you can implement a return method that returns whatever you want.

This package extends Spatie's DataTransferObject, so you can check their docs on how you can instantiate this class. It helps to understand what a DTO is and how you can use it. There is two common patterns, you either pass your class properties through the constructor, or you create a static method.

Constructor

use CorBosman\Pipeline\Passable;

class MyPipeline extends Passable
{
    public string $username;
    public array $output;

    public function return() : array
    {
        return $this->output;
    }
}

To use this pipeline, construct it using the properties as parameters, then give it some classes to pipe through.

$output = (new MyPipeline(['username' => $foo, 'output' => []])->pipeline([
    PipeClass1::class,
    PipeClass2::class
    ...
]);

Factory

Sometimes it's easier to use a factory method. For example, you can use it to initialise a response DataTransferObject.

use CorBosman\Pipeline\Passable;

class MyPipeline extends Passable
{
    public string $username;
    public Output $output;
    
    public static function factory($username) : self
    {
        return new self([
            'username' => $username,
            'output'   => new Output
        ]);
    }

    public function return() : array
    {
        return $this->output->toArray();
    }
}
$output = MyPipeline::factory($username)->pipeline([
    PipeClass1::class,
    PipeClass2::class
    ...
]);

Pipeline classes

To send this Passable through Laravel's Pipeline, you call the pipeline method with an array of classes. Just like with middleware classes, you have to make sure that your class calls the next class, as shown in the examples below. The Pipeline class allows several different options.

  • Normal class with a handle method
class Uppercase
{
    public function handle(MyPipeline $passable, $next)
    {
        $passable->output = strtoupper($passable->username);

        return $next($passable);
    }
}
  • An invokable class
class Uppercase
{
    public function __invoke(MyPipeline $passable, $next)
    {
        $passable->output = strtoupper($passable->username);

        return $next($passable);
    }
}
  • An object, note that the object should have the handle method on it
$uppercase = new Uppercase;
$result = MyPipeline::factory($input)->pipeline([$uppercase]);

Changing the called method

By default the handle() method is called on each pipeline class. If you want to override that, you can set the method name as the second parameter of the pipeline() method.

$result = MyPipeline::factory($input)->pipeline([...], 'filter');

Change log

Please see the changelog for more information on what has changed recently.

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email cor@in.ter.net instead of using the issue tracker.

Credits

License

MIT. Please see the license file for more information.

corbosman/laravel-pipeline-passable 适用场景与选型建议

corbosman/laravel-pipeline-passable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 206 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 12 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「pipeline」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 corbosman/laravel-pipeline-passable 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-12-10