国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

PHP Pipeline 實現中間件的示例代碼

瀏覽:94日期:2022-09-10 16:11:52

Pipeline 設計模式

水管太長,只要有一處破了,就會漏水了,而且不利于復雜環境彎曲轉折使用。所以我們都會把水管分成很短的一節一節管道,然后最大化的讓管道大小作用不同,因地制宜,組裝在一起,滿足各種各樣的不同需求。

由此得出 Pipeline 的設計模式,就是將復雜冗長的流程 (processes) 截成各個小流程,小任務。每個最小量化的任務就可以復用,通過組裝不同的小任務,構成復雜多樣的流程 (processes)。

最后將「輸入」引入管道,根據每個小任務對輸入進行操作 (加工、過濾),最后輸出滿足需要的結果。

你可以拿koa的中間件機制來做參考 ,也就是我們常說的削洋蔥思路

PHP Pipeline 實現中間件的示例代碼

在前端里早期有一個工程打包工具gulp寫法就更能體現pipeline

gulp.task(’css’, function(){ return gulp.src(’client/templates/*.less’) .pipe(less()) .pipe(minifyCSS()) .pipe(gulp.dest(’build/css’))});gulp.task(’js’, function(){ return gulp.src(’client/javascript/*.js’) .pipe(sourcemaps.init()) .pipe(concat(’app.min.js’)) .pipe(sourcemaps.write()) .pipe(gulp.dest(’build/js’))});gulp.task(’default’, [ ’html’, ’css’, ’js’ ]);

IlluminatePipeline

Laravel 框架中的中間件,就是利用 IlluminatePipeline 來實現的,本來想寫寫我對 「Laravel 中間件」源碼的解讀,但發現網上已經有很多帖子都有表述了,所以本文就簡單說說如何使用 IlluminatePipeline。

public function demo(Request $request){ $pipe1 = function ($payload, Closure $next) { $payload = $payload + 1; return $next($payload); }; $pipe2 = function ($payload, Closure $next) { $payload = $payload * 3; return $next($payload); }; $data = $request->input(’data’, 0); $pipeline = new Pipeline(); return $pipeline ->send($data) ->through([$pipe1, $pipe2]) ->then(function ($data) { return $data; });}

今天主要學習學習「Pipeline」,順便推薦一個 PHP 插件:league/pipeline。

composer require league/pipeline

使用起來也很方便

use LeaguePipelinePipeline;class TimesTwoStage{ public function __invoke($payload) { return $payload * 2; }}class AddOneStage{ public function __invoke($payload) { return $payload + 1; }}$pipeline = (new Pipeline) ->pipe(new TimesTwoStage) ->pipe(new AddOneStage);// Returns 21$pipeline->process(10);

接下來我們添加FastRouter在我的項目中使用。

PHP Pipeline 實現中間件的示例代碼

上面的代碼修改成這樣

PHP Pipeline 實現中間件的示例代碼

我們接下來看看 RespondJson 里做了什么.

<?phpnamespace PlatappsMiddlewares;class RespondJson{ public function __invoke($payload) { header(’Content-type:text/json’); return $payload; }}

就簡單的加了個 header

我們試試把注釋到一個渠道

PHP Pipeline 實現中間件的示例代碼

我們再次訪問的時候就變成

PHP Pipeline 實現中間件的示例代碼

當然這是很簡單的中間件,這種中間件遠遠不夠,這里是核心代碼,可以去這里看看,也比較簡單。

我們最終需要修改pipe這個方法

namespace LeaguePipeline;class Pipeline implements PipelineInterface{ /** * @var callable[] */ private $stages = []; /** * @var ProcessorInterface */ private $processor; public function __construct(ProcessorInterface $processor = null, callable ...$stages) { $this->processor = $processor ?? new FingersCrossedProcessor; $this->stages = $stages; } public function pipe(callable $stage): PipelineInterface { $pipeline = clone $this; $pipeline->stages[] = $stage; return $pipeline; } public function process($payload) { return $this->processor->process($payload, ...$this->stages); } public function __invoke($payload) { return $this->process($payload); }}

這么多框架里面我這里建議拿Tp6的來做參考,功能還算夠用。

<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: yunwuxin <[email protected]>// +----------------------------------------------------------------------namespace think;use Closure;use Exception;use Throwable;class Pipeline{ protected $passable; protected $pipes = []; protected $exceptionHandler; /** * 初始數據 * @param $passable * @return $this */ public function send($passable) { $this->passable = $passable; return $this; } /** * 調用棧 * @param $pipes * @return $this */ public function through($pipes) { $this->pipes = is_array($pipes) ? $pipes : func_get_args(); return $this; } /** * 執行 * @param Closure $destination * @return mixed */ public function then(Closure $destination) { $pipeline = array_reduce( array_reverse($this->pipes), $this->carry(), function ($passable) use ($destination) {try { return $destination($passable);} catch (Throwable | Exception $e) { return $this->handleException($passable, $e);} }); return $pipeline($this->passable); } /** * 設置異常處理器 * @param callable $handler * @return $this */ public function whenException($handler) { $this->exceptionHandler = $handler; return $this; } protected function carry() { return function ($stack, $pipe) { return function ($passable) use ($stack, $pipe) {try { return $pipe($passable, $stack);} catch (Throwable | Exception $e) { return $this->handleException($passable, $e);} }; }; } /** * 異常處理 * @param $passable * @param $e * @return mixed */ protected function handleException($passable, Throwable $e) { if ($this->exceptionHandler) { return call_user_func($this->exceptionHandler, $passable, $e); } throw $e; }}

這種寫法有什么好?

其實就好就好在,你在處理一個請求的過程中,分配任務的時候,在處理的過程,每個中間的人,只要做自己處理的請求和結果還有請求即可。讓當數據到達Controller里的時候,顯示業務邏輯的時候更加強大

到此這篇關于PHP Pipeline 實現中間件的示例代碼的文章就介紹到這了,更多相關PHP Pipeline 中間件內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: PHP
相關文章:
主站蜘蛛池模板: 免费看一级毛片欧美 | 乱淫网站 | a级一级毛片| 欧美激情视频一级视频一级毛片 | 欧美另类性视频 | 欧美日韩亚洲v在线观看 | 99精品视频在线观看免费播放 | 欧美一区二区三区高清视频 | 亚洲国产tv| 国产一区二区亚洲精品天堂 | 欧美黄色高清 | 一级无毛片 | 夜夜春夜夜夜夜猛噜噜噜噜噜 | 国产高清专区 | 亚洲精彩视频在线观看 | 欧美亚洲国产激情一区二区 | 欧美亚洲在线观看 | 成人满18在线观看网站免费 | 国产在线观看91精品一区 | 国产在线一区二区三区欧美 | 美国一级欧美三级 | 国产精品国三级国产aⅴ | 97久久精品午夜一区二区 | 免费一级毛片女人图片 | www.久久久 | 久久久综合网 | 欧美又粗又硬又大久久久 | 久久免费小视频 | 中文字幕日韩精品亚洲七区 | 久久国产免费一区二区三区 | 亚欧在线一线 | 亚洲精品无码不卡在线播放he | 欧美一级毛片久久精品 | 国产成人综合久久精品亚洲 | 91久久精品 | 欧美一级特黄特黄做受 | 欧美一区二区三区高清视频 | 欧美午夜视频一区二区三区 | 午夜亚洲精品 | 亚洲第一大网站 | 亚洲欧美日韩在线一区二区三区 |