This article mainly introduces the PHP pipeline plug-in league\pipeline analysis, has a certain reference value, now share to everyone, the need for friends can refer to
Pipeline design mode
The water pipe is too long, as long as there is a broken, it will leak, and is not conducive to the complex environment bending transition use. So we all divide the pipe into a very short section of a pipe, and then maximize the size of the pipe to different, local conditions, assembled together to meet a variety of different needs.
This leads to the Pipeline design pattern, that is, the complex lengthy process (processes) cut into small processes, small tasks. Each of the minimum quantified tasks can be reused to form complex and diverse processes (processes) by assembling different small tasks.
Finally, "input" into the pipeline, according to each small task to the input operation (processing, filtering), the final output to meet the desired results.
Today we mainly study "pipeline", and by the way recommend a PHP plugin: league/pipeline
.
Gulp
For the first time know the concept of "pipe", from gulp
the use.
gulp
is based on NodeJS
the automatic task runner, she can automatically complete, and so on, Javascript
sass
less
such as file testing, inspection, merging, compression, formatting, browser automatic refresh, deployment of file generation, and listen to the file after the changes to repeat the specified steps. In the implementation, she borrowed from the Unix
operating system of the pipeline (pipe) thought, the previous level of output, directly into the back-level input, making it very simple to operate.
var gulp = require (' gulp '); var = require (' gulp-less '); var minifycss = require (' Gulp-csso '); var concat = require (' Gulp -concat '); var sourcemaps = require (' gulp-sourcemaps '); 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 ']);
The above two task
is mainly to be less
, all js
files parsing, compression, output and other process operations, and then stored in the corresponding folder; the output of each step is the input to the next operation, as if the pipeline is running in general.
Illuminatepipeline
Laravel framework of the middleware, is to use Illuminate\Pipeline
to achieve, originally wanted to write my "laravel middleware" source of interpretation, but found that there are many posts on the Internet has been expressed, so this article simply say how to use Illuminate\Pipeline
.
Write a demo
Public Function Demo (Request $request) { $pipe 1 = function ($payload, Closure $next) { $payload = $payload + 1;
return $next ($payload); }; $pipe 2 = function ($payload, Closure $next) { $payload = $payload * 3; Return $next ($payload); }; $data = $request->input (' data ', 0); $pipeline = new Pipeline (); return $pipeline ->send ($data) ->through ([$pipe 1, $pipe 2]) ->then (function ($data) { return $data; });}
For the source of the analysis, you can recommend to see this article, analysis of a very thorough:
implementation of Laravel Pipeline components Https://www.insp.top/article/realization-of-pipeline-component-for-laravel
Leaguepipeline
The simple use of the above pair gulp
and simply Illuminate\Pipeline
tells us that the "pipeline" application is quite extensive. If we let ourselves write a similar plug-in, I think it should not be difficult.
Below I take the League\Pipeline
plug-in to grilled its source code, see how to achieve.
Briefly
This package
provides a Plug and play implementation of the Pipeline Pattern. It ' s an architectural pattern which encapsulates sequential processes. When used, it allows the mix and match operation, and pipelines, to create new execution chains. The pipeline pattern is often compared to a production line, where each stage performs a certain operation on a given Payl Oad/subject. Stages can act on, manipulate, decorate, or even replace the payload.
If you find yourself passing results from one function to another to complete a series of tasks on a given subject, you mi Ght want to convert it into a pipeline.
https://pipeline.thephpleague.com/
Installing plugins
Composer require League/pipeline
Write a demo
Use league\pipeline\pipeline;//creates two closure functions $pipe1 = function ($payload) {return $payload + 1;}; $pipe 2 = function ($payload) {return $payload * 3;}; $route->map (' GET ', '/demo ', function (Serverrequestinterface $request, Responseinterface $response) use ( $service, $pipe 1, $pipe 2) {$params = $request->getqueryparams (); normal use $pipeline 1 = (new Pipeline)->pipe ($pipe 1)->pipe ($pipe 2); $callback 1 = $pipeline 1->process ($params [' data ']); $response->getbody ()->write ("
Run results
Interpreting source code
The entire plugin on these several files:
Pipelineinterface
<?phpdeclare (Strict_types=1); namespace League\pipeline;interface pipelineinterface extends stageinterface{ /** * Create a new pipeline with an appended stage. * * @return Static * /Public Function pipe (callable $operation): Pipelineinterface;} Interface stageinterface{ /** * Process the payload. * * @param mixed $payload * * @return Mixed * * Public function __invoke ($payload);}
The main interface is to use the idea of chain programming, add Pipeline "pipe", and then add a magic method to get the parameters to run.
First look at the effect of this magic method:
mixed __invoke ([ $... ] )
The __invoke () method is called automatically when an attempt is made to invoke an object in a way that invokes a function.
Such as:
<?phpclass Callableclass { function __invoke ($x) { var_dump ($x); }} $obj = new Callableclass; $obj (5); Var_dump (Is_callable ($obj));? >
return Result:
INT (5) bool (TRUE)
Pipeline
<?phpdeclare (Strict_types=1); namespace League\pipeline;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);} }
Pipeline
The main function of the core class is two:
Add and assemble each pipe "pipe";
After assembling, the water flow is carried out, the process ($payload) is executed and the output is obtained.
Processor
After picking up all kinds of pipelines, it is necessary to "divert into the canal". The plug-in provides two basic execution classes, relatively simple, directly read the code to understand.
Follow the $stages array smoothly, traverse the execution pipeline method, and then pass the result to the next pipe, let "water" layer "flow" up class Fingerscrossedprocessor implements processorinterface{ Public function process ($payload, callable ... $stages) {foreach ($stages as $stage) {$payload = $st Age ($payload); } return $payload; }}//Add an additional "filter", after each pipeline result, all need check, once satisfied then terminate, direct output result. Class Interruptibleprocessor implements processorinterface{/** * @var callable * * Private $check; Public function __construct (callable $check) {$this->check = $check; The Public function process ($payload, callable ... $stages) {$check = $this->check; foreach ($stages as $stage) {$payload = $stage ($payload); if (True!== $check ($payload)) {return $payload; }} return $payload; }}interface processorinterface{/** * Process the payload using multiple stages. * * @param mixed $payload * * @return Mixed * * Public function process ($payload, callable ... $stages);}
We can also use this interface to implement our approach to assembling pipelines and "filters".
Pipelinebuilder
Finally, a Builder is provided, and this is well understood:
Class Pipelinebuilder implements pipelinebuilderinterface{ /** * @var callable[] * * Private $stages = []; /** * @return Self */public function Add (callable $stage): Pipelinebuilderinterface { $this- >stages[] = $stage; return $this; } Public Function Build (processorinterface $processor = null): Pipelineinterface { return new Pipeline ($ Processor, ... $this->stages);} } Interface pipelinebuilderinterface{ /** * Add an stage. * * @return Self * /Public Function Add (callable $stage): Pipelinebuilderinterface; /** * Build a new Pipeline object. * /Public Function build (processorinterface $processor = null): Pipelineinterface;}
Summarize
Whether it is a horizontal understanding of different technologies or based on Laravel or some open source plugins, we can learn the general principles and methods of technology. These principles and methods are then counterproductive to our actual code development.
Recently idle to be OK, own reference Laravel to write a simple frame, also will League\Pipeline
introduce into the framework to use.
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!