Laravel Pipeline解读

作者 : 开心源码 本文共2125个字,预计阅读时间需要6分钟 发布时间: 2022-05-12 共146人阅读

大家好,今天给大家详情下Laravel框架的Pipeline。
它是一个非常好用的组件,能够使代码的结构非常清晰。 Laravel的中间件机制便是基于它来实现的。

通过Pipeline,可以轻松实现APO编程。

官方GIT地址

illuminate/pipeline

下面的代码是我实现的一个简化版本:

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){ "执行其它逻辑";});

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Laravel Pipeline解读

发表回复