Analysis of Pipeline in Laravel framework (code example)

01-28-2024

What this article brings to you is about the analysis of Pipeline in Laravel framework (code example), which has certain reference value. Friends who need it can refer to it and hope to help you. Hello, everyone, today I'd like to introduce you to the Pipeline of Laravel framework.

It is a very useful component, which can make the structure of the code very clear. Laravel's middleware mechanism is based on it.

APO programming can be easily realized through Pipeline.

Official GIT address https://github.com/illuminate ...

The following code is a simplified version of my implementation: class Pipeline. { /** * The method to call on each pipe * @var string */ protected $method = 'handle'; /** * The object being passed throw the pipeline * @var mixed */ protected $passable; /** * The array of class pipes * @var array */ protected $pipes = []; /** * Set the object being sent through the pipeline * * @param $passable * @return $this */ public function send($passable) { $this->passable = $passable; return $this; } /** * Set the method to call on the pipes * @param array $pipes * @return $this */ public function through($pipes) { $this->pipes = $pipes; return $this; } /** * @param \Closure $destination * @return mixed */ public function then(\Closure $destination) { $pipeline = array_reduce(array_reverse($this->pipes), $this->getSlice(), $destination); return $pipeline($this->passable); } /** * Get a Closure that represents a slice of the application onion * @return \Closure */ protected function getSlice() { return function($stack, $pipe){ return function ($request) use ($stack, $pipe) { return $pipe::{$this->method}($request, $stack); }; }; } }

The main logic of this kind lies in the then and getSlice methods. Generate an anonymous function that takes one parameter through array_reduce, and then execute the call.

Simple use example class ALogic { public static function handle($data, \Clourse $next) { Print "start a logic"; $ret = $next($data); Print "end a logic"; return $ret; } } class BLogic { public static function handle($data, \Clourse $next) { Print "start b logic"; $ret = $next($data); Print "end b logic"; return $ret; } } class CLogic { public static function handle($data, \Clourse $next) { Print "start c logic"; $ret = $next($data); Print "end c logic"; return $ret; } }

$pipes = [ ALogic::class, BLogic::class, CLogic::class ]; $data = "any things"; (new Pipeline())->send($data)->through($pipes)->then(function($data){ print $data; });

Running result: "Start A logic" "Start logic B" "start c logic" "any things" "end c logic" "End logic B" "end a logic"

The advantage of AOP example AOP lies in the dynamic addition of functions without affecting other levels, and it is very convenient to add or delete functions.

class IpCheck { public static function handle($data, \Clourse $next) { If ("IP invalid") {// IP is illegal. throw Exception("ip invalid"); } return $next($data); } } class StatusManage { public static function handle($data, \Clourse $next) { // exec can perform the operation of initializing the state. $ret = $next($data) // exec can save the status information. return $ret; } } $pipes = [ IpCheck::class, StatusManage::class, ]; (new pipeline ())-> send ($ data)-> through ($ pipes)-> then (function ($ data) {"execute other logic"; });

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us