Laravel Pipeline解讀

何止七八發表於2019-02-16

大家好,今天給大家介紹下Laravel框架的Pipeline。
它是一個非常好用的元件,能夠使程式碼的結構非常清晰。 Laravel的中介軟體機制便是基於它來實現的。

通過Pipeline,可以輕鬆實現APO程式設計。

官方GIT地址

https://github.com/illuminate…

下面的程式碼是我實現的一個簡化版本:

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

}

此類主要邏輯就在於then和getSlice方法。通過array_reduce,生成一個接受一個引數的匿名函式,然後執行呼叫。

簡單使用示例

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;
    }
}
$pipes = [
    ALogic::class,
    BLogic::class,
    CLogic::class
];

$data = "any things";
(new Pipeline())->send($data)->through($pipes)->then(function($data){ print $data;});
執行結果:
"開始 A 邏輯"
"開始 B 邏輯"
"開始 C 邏輯"
"any things"
"結束 C 邏輯"
"結束 B 邏輯"
"結束 A 邏輯"

AOP示例

AOP 的優點就在於動態的新增功能,而不對其它層次產生影響,可以非常方便的新增或者刪除功能。

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){ "執行其它邏輯";});

相關文章