定制 ghofurgiovany/pipeline 二次开发

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

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

ghofurgiovany/pipeline

最新稳定版本:v2

Composer 安装命令:

composer require ghofurgiovany/pipeline

包简介

README 文档

README

Example code showcasing the Pipeline package using the with transaction method and the pipable trait

Installation

Install via composer:

composer require GhofurGiovany/pipeline

Sending pipes down the pipeline

When configuring the pipeline, you can send an array of class strings, invokable objects, closures, objects with a handle() method, or any other type that passes is_callable().

use GhofurGiovany\Pipeline\Pipeline;

class RegisterController
{
    public function store(StoreRegistrationRequest $request)
    {
        return Pipeline::make()
            ->send($request->all())
            ->through([
                RegisterUser::class,
                AddMemberToTeam::class,
                SendWelcomeEmail::class,
            ])
            ->then(fn ($data) => UserResource::make($data));
    }
}

Another approach you can take is to implement this as a trait on a data object. (You could even implement it on your FormRequest object if you really wanted.)

use GhofurGiovany\Pipeline\Pipable;

class UserDataObject
{
    use Pipable;

    public string $name;
    public string $email;
    public string $password;
    // ...
}

class RegisterController
{
    public function store(StoreRegistrationRequest $request)
    {
        return UserDataObject::fromRequest($request)
            ->pipeThrough([
                RegisterUser::class,
                AddMemberToTeam::class,
                SendWelcomeEmail::class,
            ])
            ->then(fn ($data) => UserResource::make($data));
    }

    // you also can pipe the request

    return $request->pipe()
            ->withTransaction()
            ->through([
                RegisterUser::class,
                AddMemberToTeam::class,
                SendWelcomeEmail::class,
            ])
            ->then(fn ($data) => UserResource::make($data));
}

To maintain compatibility with Laravel's Pipeline class, the through() method can accept either a single array of callables or multiple parameters, where each parameter is one of the callable types listed previously. However, the pipeThrough() trait method only accepts an array, since it also has a second optional parameter.

Using database transactions

When you want to use database transactions in your pipeline, the method will be different depending on if you're using the trait or the Pipeline class.

Using the Pipeline class:

Pipeline::make()->withTransaction()

The withTransaction() method will tell the pipeline to use transactions. When you call the then() or thenReturn() methods, a database transaction will begin before executing the pipes. If an exception is encountered during the pipeline, the transaction will be rolled back so no data is committed to the database. Assuming the pipeline completed successfully, the transaction is committed.

When using the trait, you can pass a second parameter to the pipeThrough() method:

$object->pipeThrough($pipes, withTransaction: true);

Testing

composer test

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固