pipeline php代码,Laravel框架中Pipeline的解析(代码示例)

本文介绍了 Laravel 框架中的 Pipeline 组件及其应用示例。Pipeline 是实现清晰代码结构的有效工具,并支撑了 Laravel 的中间件机制。文章通过具体代码展示了如何使用 Pipeline 实现 AOP 编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本篇文章给大家带来的内容是关于Laravel框架中Pipeline的解析(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

大家好,今天给大家介绍下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){ "执行其它逻辑";});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值