Home > PHP Framework > Laravel > Interpretation of Laravel Pipeline

Interpretation of Laravel Pipeline

藏色散人
Release: 2021-06-23 09:29:28
forward
2190 people have browsed it

The following is the explanation of Laravel Pipeline from the laravel tutorial column. I hope it will be helpful to friends in need!

Hello everyone, today I will introduce to you the Pipeline of the Laravel framework.
It is a very easy-to-use component that can make the structure of the code very clear. Laravel's middleware mechanism is implemented based on it.

Through Pipeline, APO programming can be easily implemented.

Official GIT address

https://github.com/illuminate/pipeline

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);
            };
        };
    }

}
Copy after login

The main logic of this type lies in the then and getSlice methods. Through array_reduce, generate an anonymous function that accepts one parameter, and then execute the call.

Simple usage example

class ALogic
{
    public static function handle($data, \Clourse $next)
    {
        print "开始 A 逻辑";
        $ret = $next($data);
        print "结束 A 逻辑";
        return $ret;
    }
}

class BLogic
{
    public static function handle($data, \Clourse $next)
    {
        print "开始 B 逻辑";
        $ret = $next($data);
        print "结束 B 逻辑";
        return $ret;
    }
}

class CLogic
{
    public static function handle($data, \Clourse $next)
    {
        print "开始 C 逻辑";
        $ret = $next($data);
        print "结束 C 逻辑";
        return $ret;
    }
}
Copy after login
$pipes = [
    ALogic::class,
    BLogic::class,
    CLogic::class
];

$data = "any things";
(new Pipeline())->send($data)->through($pipes)->then(function($data){ print $data;});
Copy after login
Running result:
"开始 A 逻辑"
"开始 B 逻辑"
"开始 C 逻辑"
"any things"
"结束 C 逻辑"
"结束 B 逻辑"
"结束 A 逻辑"
Copy after login

AOP example

The advantage of AOP is that it can dynamically add functions without affecting other layers. , you can add or delete functions very conveniently.

class IpCheck
{
    public static function handle($data, \Clourse $next)
    {
        if ("IP invalid") { // IP 不合法
            throw Exception("ip invalid");
        }
        return $next($data);
    }
}

class StatusManage
{
    public static function handle($data, \Clourse $next)
    {
        // exec 可以执行初始化状态的操作
        $ret = $next($data)
        // exec 可以执行保存状态信息的操作
        return $ret;
    }
}

$pipes = [
    IpCheck::class,
    StatusManage::class,
];

(new Pipeline())->send($data)->through($pipes)->then(function($data){ "执行其它逻辑";});
Copy after login

The above is the detailed content of Interpretation of Laravel Pipeline. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template