Laravel 中的 Pipeline — 管道设计范式

file

总的来说,通过使用 Laravel 中的管道,你能够流畅地在若干个类之间传递一个对象,从而执行一个任意类型的任务,一旦所有的任务都被执行完,就会将结果值返回。

接下来,你能了解到更多关于 Laravel pipelines 的知识。

关于管道是运行的方式,最明显的范例其实就在框架本身最常用的一个组件当中,没错,我说的就是中间件。

中间件为过滤进入应用的 HTTP 请求提供了一个便利的机制。

一个基本的中间件应该是这个样子的:

<?php
namespace App\Http\Middleware;
use Closure;
class TestMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // Here you can add your code
        return $next($request);
    }
}

这些「中间件」实际上就是管道,请求经由这里发送,从而执行任何需要的任务。在这里,你可以检查请求是否是一个 HTTP 请求,是否是一个 JSON 请求,是否存在已认证的用户信息等等。

如果你想快速的查看Illuminate\Foundation\Http\Kernel 类, 你将看到如何使用 Pipeline 类的新实例来执行中间件。

/**
  * Send the given request through the middleware / router.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
protected function sendRequestThroughRouter($request)
{
    $this->app->instance('request', $request);
    Facade::clearResolvedInstance('request');
    $this->bootstrap();
    return (new Pipeline($this->app))
                    ->send($request)
                    ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
    ->then($this->dispatchToRouter());
}

你可以在代码中看到类似的内容:通过中间件列表发送请求的新管道,然后发送路由。

如果这让你看起来有点不知所措也不用担心。让我们试着用以下这个例子来阐明这个概念。

处理多任务运行类

让我们来看一种场景。 比方说,你建立了一个人们可以发帖并发表评论的论坛。但是,您的用户请求您自动删除标签或在创建时在每一个内容上编辑标签。

此时你被要求做的事情如下:

  1. 用纯文本替换链接标记;
  2. 用“*”替换敏感词;
  3. 从内容中完全删除脚本标记。

可能你最终会创建类来处理这些 “tasks”。

$pipes = [
    RemoveBadWords::class
    ReplaceLinkTags::class
    RemoveScriptTags::class
];

我们要做的是将给定的“内容”传递给每个任务,然后将结果返回给下一个任务。我们可以使用pipeline来做到这一点。

<?php

public function create(Request $request)
{
    $pipes = [
        RemoveBadWords::class,
        ReplaceLinkTags::class,
        RemoveScriptTags::class
    ];
    $post = app(Pipeline::class)
        ->send($request->content)
        ->through($pipes)
        ->then(function ($content) {
            return Post::create(['content' => 'content']);
        });
    // return any type of response
}

每个“task”类应该有一个“handle”方法来执行操作。也许每个类都有统一的约束是一个不错的选择:

<?php

namespace App;

use Closure;

interface Pipe
{
    public function handle($content, Closure $next);
}

命名是个困难的事情 ¯_(ツ)_/¯

<?php

namespace App;

use Closure;

class RemoveBadWords implements Pipe
{
    public function handle($content, Closure $next)
    {
        // Here you perform the task and return the updated $content
        // to the next pipe
        return  $next($content);
    }
}

用于执行任务的方法应该接收两个参数,第一个参数是合格的对象,第二个参数是当前操作处理完后会接管的下一个闭包(匿名函数)。

您可以使用自定义方法名称而不是“handle”。然后你需要指定pipeline要使用的方法名称,比如:

app(Pipeline::class)
 ->send($content)
 ->through($pipes)
 ->via('customMethodName') // <---- This one :)
 ->then(function ($content) {
     return Post::create(['content' => $content]);
 });

最后产生的效果是什么 ?

提交的内容将会被各个$pipes 所处理, 被处理的结果将会存储下来。

$post = app(Pipeline::class)
    ->send($request->all())
    ->through($pipes)
    ->then(function ($content) {
        return Post::create(['content' => $content]);
    });

结语

记住,有很多方法可以解决这类问题。至于如何选择,就看你自己的选择了。只要知道一点,需要的时候你可以使用这个工具就可以了。我希望这个例子让你更好的了解”Laravel pipelines”,以及如何使用它们。如果你想了解或者学习更多,你可以查看Laravel的API文档laravel.com/api/5.4/Illuminate/Pip...

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://medium.com/@jeffochoa/understand...

译文地址:https://learnku.com/laravel/t/7543/pipel...

本帖已被设为精华帖!
本文为协同翻译文章,如您发现瑕疵请点击「改进」按钮提交优化建议
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 1
No_Panic

感谢 :+1:

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!