一文了解Laravel中的Pipeline(管道)

 2402

本篇文章带大家了解一下Laravel中的Pipeline(管道),聊聊管道设计范式,希望对大家有所帮助!


一文了解Laravel中的Pipeline(管道)


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

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

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

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

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

  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. class TestMiddleware
  5. {
  6.     /**
  7.      * Handle an incoming request.
  8.      *
  9.      * @param  \Illuminate\Http\Request  $request
  10.      * @param  \Closure  $next
  11.      * @return mixed
  12.      */
  13.     public function handle($request, Closure $next)
  14.     {
  15.         // Here you can add your code
  16.         return $next($request);
  17.     }
  18. }

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

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

  1. /**
  2.   * Send the given request through the middleware / router.
  3.   *
  4.   * @param  \Illuminate\Http\Request  $request
  5.   * @return \Illuminate\Http\Response
  6.   */
  7. protected function sendRequestThroughRouter($request)
  8. {
  9.     $this->app->instance('request', $request);
  10.     Facade::clearResolvedInstance('request');
  11.     $this->bootstrap();
  12.     return (new Pipeline($this->app))
  13.                     ->send($request)
  14.                     ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
  15.     ->then($this->dispatchToRouter());
  16. }

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

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


处理多任务运行类

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

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

用纯文本替换链接标记;

用“*”替换敏感词;

从内容中完全删除脚本标记。

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

  1. $pipes = [
  2.     RemoveBadWords::class
  3.     ReplaceLinkTags::class
  4.     RemoveScriptTags::class
  5. ];

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

  1. <?php
  2.  
  3. public function create(Request $request)
  4. {
  5.     $pipes = [
  6.         RemoveBadWords::class,
  7.         ReplaceLinkTags::class,
  8.         RemoveScriptTags::class
  9.     ];
  10.     $post = app(Pipeline::class)
  11.         ->send($request->content)
  12.         ->through($pipes)
  13.         ->then(function ($content) {
  14.             return Post::create(['content' => 'content']);
  15.         });
  16.     // return any type of response
  17. }

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

  1. <?php
  2.  
  3. namespace App;
  4.  
  5. use Closure;
  6.  
  7. interface Pipe
  8. {
  9.     public function handle($content, Closure $next);
  10. }

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

  1. <?php
  2.  
  3. namespace App;
  4.  
  5. use Closure;
  6.  
  7. class RemoveBadWords implements Pipe
  8. {
  9.     public function handle($content, Closure $next)
  10.     {
  11.         // Here you perform the task and return the updated $content
  12.         // to the next pipe
  13.         return  $next($content);
  14.     }
  15. }

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

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

  1. app(Pipeline::class)
  2.  ->send($content)
  3.  ->through($pipes)
  4.  ->via('customMethodName') // <---- This one :)
  5.  ->then(function ($content) {
  6.      return Post::create(['content' => $content]);
  7.  });


最后产生的效果是什么 ?

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

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

结语

记住,有很多方法可以解决这类问题。至于如何选择,就看你自己的选择了。只要知道一点,需要的时候你可以使用这个工具就可以了。我希望这个例子让你更好的了解”Laravel pipelines”,以及如何使用它们。

如果你想了解或者学习更多,你可以查看Laravel的API文档 https://laravel.com/api/5.4/Illuminate/Pipeline/Pipeline.html

原文地址:https://medium.com/@jeffochoa/understanding-laravel-pipelines-a7191f75c351

译文地址:https://learnku.com/laravel/t/7543/pipeline-pipeline-design-paradigm-in-laravel


TAG标签:
本文网址:https://www.zztuku.com/index.php/detail-13362.html
站长图库 - 一文了解Laravel中的Pipeline(管道)
申明:本文转载于《learnku》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐