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

本篇文章帶大家了解一下laravel中的pipeline(管道),聊聊管道設計范式,希望對大家有所幫助!

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

總的來說,通過使用 laravel 中的管道,你能夠流暢地在若干個類之間傳遞一個對象,從而執行一個任意類型的任務,一旦所有的任務都被執行完,就會將結果值返回。

接下來,你能了解到更多關于 Laravel pipelines 的知識。

關于管道是運行的方式,最明顯的范例其實就在框架本身最常用的一個組件當中,沒錯,我說的就是中間件。

中間件為過濾進入應用的 HTTP 請求提供了一個便利的機制。

一個基本的中間件應該是這個樣子的:

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

這些「中間件」實際上就是管道,請求經由這里發送,從而執行任何需要的任務。在這里,你可以檢查請求是否是一個 HTTP 請求,是否是一個 JSON 請求,是否存在已認證的用戶信息等等。

如果你想快速的查看IlluminateFoundationHttpKernel 類, 你將看到如何使用 Pipeline 類的新實例來執行中間件。

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

你可以在代碼中看到類似的內容:通過中間件列表發送請求的新管道,然后發送路由。

如果這讓你看起來有點不知所措也不用擔心。讓我們試著用以下這個例子來闡明這個概念。

處理多任務運行類

讓我們來看一種場景。 比方說,你建立了一個人們可以發帖并發表評論的論壇。但是,您的用戶請求您自動刪除標簽或在創建時在每一個內容上編輯標簽。

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

  • 用純文本替換鏈接標記;

  • 用“*”替換敏感詞;

  • 從內容中完全刪除腳本標記。

可能你最終會創建類來處理這些 “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-&gt;content) ????????-&gt;through($pipes) ????????-&gt;then(function?($content)?{ ????????????return?Post::create(['content'?=&gt;?'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) ?-&gt;send($content) ?-&gt;through($pipes) ?-&gt;via('customMethodName')?//?then(function?($content)?{ ?????return?Post::create(['content'?=&gt;?$content]); ?});

最后產生的效果是什么 ?

提交的內容將會被各個$pipes 所處理, 被處理的結果將會存儲下來。

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

結語

記住,有很多方法可以解決這類問題。至于如何選擇,就看你自己的選擇了。只要知道一點,需要的時候你可以使用這個工具就可以了。我希望這個例子讓你更好的了解”Laravel pipelines”,以及如何使用它們。如果你想了解或者學習更多,你可以查看Laravel的API文檔laravel.com/api/5.4/Illuminate/Pip…

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

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

【相關推薦:laravel.com/api/5.4/Illuminate/Pip…

? 版權聲明
THE END
喜歡就支持一下吧
點贊14 分享