管道设计模式水管太长,只要有一处坏掉就会漏水,不利于复杂环境下弯弯曲曲。因此,我们会将水管分成很短的几段,然后将管道的尺寸和作用发挥到极致,因地制宜,组装在一起,以满足各种不同的需求。由此可以得出结论,Pipeline的设计模式就是把复杂冗长的流程(进程)切割成小流程和小任务。每个最小量化的任务都可以重复使用,通过组装不同的小任务可以形成复杂多样的流程。最后将“输入”引入管道,根据每个小任务对输入进行操作(处理、过滤),最终输出满足需求的结果。可以借鉴koa的中间件机制,也就是我们常说的切洋葱思想。前期在前端有个工程打包工具gulp,可以更好的体现pipelinegulp.task('css',function(){returngulp.src('client/templates/*.less')。pipe(less()).pipe(minifyCSS()).pipe(gulp.dest('build/css'))});gulp.task('js',function(){returngulp.src('client/javascript/*.js').pipe(sourcemaps.init()).pipe(concat('app.min.js')).pipe(sourcemaps.write()).pipe(gulp.dest('build/js'))});gulp.task('默认',['html','css','js']);IlluminatePipelineLaravel框架中的中间件,使用Illuminate\Pipeline实现。本来想写一下我对《Laravel中间件》源码的解读,但是发现网上已经有很多帖子表达了,所以本文就简单说说如何使用Illuminate\Pipeline。publicfunctiondemo(Request$request){$pipe1=function($payload,Closure$next){$payload=$payload+1;返回$next($payload);};$pipe2=function($payload,Closure$next){$payload=$payload*3;返回$next($payload);};$data=$request->input('data',0);$管道=新管道();return$pipeline->send($data)->through([$pipe1,$pipe2])->then(function($data){return$data;});}今天主要研究“Pipeline”,推荐顺便说一个PHP插件:league/pipeline。composerrequireleague/pipeline使用起来也很方便useLeague\Pipeline\Pipeline;classTimesTwoStage{publicfunction__invoke($payload){return$payload*2;}}classAddOneStage{publicfunction__invoke($payload){return$payload+1;}}$pipeline=(newPipeline)->pipe(newTimesTwoStage)->pipe(newAddOneStage);//返回21$pipeline->process(10);接下来我们在我的项目使用中添加FastRouter。上面的代码修改成这样,我们来看看RespondJson中做了什么。返回$有效载荷;}}只需添加一个标题。当我们再次访问时,我们尝试向频道添加评论。当然,这是一个非常简单的中间件。这种中间件是远远不够的。这里是核心代码,大家可以去这里看看,也比较简单。我们最后需要修改管道方法命名空间League\Pipeline;classPipelineimplementsPipelineInterface{/***@varcallable[]*/private$stages=[];/***@varProcessorInterface*/private$processor;公共函数__construct(ProcessorInterface$processor=null,callable...$stages){$this->processor=$processor??新的手指交叉处理器;$this->stages=$stages;}publicfunctionpipe(callable$stage):PipelineInterface{$pipeline=clone$this;$pipeline->stages[]=$stage;返回$管道;}publicfunctionprocess($payload){return$this->processor->process($payload,...$this->stages);}公共函数__invoke($payload){return$this->process($payload);}}在这么多框架中,我建议使用Tp6作为参考,功能还不错。//+------------------------------------------------------------------命名空间思考;使用闭包;使用异常;使用Throwable;类Pipeline{protected$尚可;受保护的$pipes=[];受保护的$exceptionHandler;/***初始数据*@param$passable*@return$this*/publicfunctionsend($passable){$this->passable=$passable;返回$这个;}/***调用栈*@param$pipes*@return$this*/publicfunctionthrough($pipes){$this->pipes=is_array($pipes)?$管道:func_get_args();返回$这个;}/***执行*@paramClosure$destination*@returnmixed*/publicfunctionthen(Closure$destination){$pipeline=array_reduce(array_reverse($this->pipes),$this->carry(),function($passable)使用($destination){try{return$destination($passable);}catch(Throwable|Exception$e){return$this->handleException($passable,$e);}});返回$pipeline($this->passable);}/***设置异常处理器*@paramcallable$handler*@return$this*/publicfunctionwhenException($handler){$this->exceptionHandler=$handler;返回$这个;}保护函数进位(){返回函数($stack,$pipe){返回函数($passable)使用($stack,$pipe){try{return$pipe($passable,$stack);}catch(Throwable|Exception$e){return$this->handleException($passable,$e);}};};}/***异常处理*@param$passable*@param$e*@returnmixed*/protectedfunctionhandleException($passable,Throwable$e){if($this->exceptionHandler){returncall_user_func($this->exceptionHandler,$passable,$e);}抛出$e;}}这种写法有什么好处呢?其实好处就是当你在处理一个请求的时候,分配任务的时候,在处理的过程中,中间的每个人只需要做自己处理的请求、结果和请求。当数据到达Controller里面的时候,展现业务逻辑的时候更强大
