php中间件怎么使用,如何在PHP中利用Pipeline 实现一个中间件功能

如何在PHP中利用Pipeline 实现一个中间件功能

发布时间:2021-02-17 12:12:25

来源:亿速云

阅读:51

作者:Leah

今天就跟大家聊聊有关如何在PHP中利用Pipeline 实现一个中间件功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

在前端里早期有一个工程打包工具gulp写法就更能体现pipelinegulp.task('css', function(){

return gulp.src('client/templates/*.less')

.pipe(less())

.pipe(minifyCSS())

.pipe(gulp.dest('build/css'))

});

gulp.task('js', function(){

return gulp.src('client/javascript/*.js')

.pipe(sourcemaps.init())

.pipe(concat('app.min.js'))

.pipe(sourcemaps.write())

.pipe(gulp.dest('build/js'))

});

gulp.task('default', [ 'html', 'css', 'js' ]);

IlluminatePipeline

Laravel 框架中的中间件,就是利用 Illuminate\Pipeline 来实现的,本来想写写我对 「Laravel 中间件」源码的解读,但发现网上已经有很多帖子都有表述了,所以本文就简单说说如何使用 Illuminate\Pipeline。public function demo(Request $request)

{

$pipe1 = function ($payload, Closure $next) {

$payload = $payload + 1;

return $next($payload);

};

$pipe2 = function ($payload, Closure $next) {

$payload = $payload * 3;

return $next($payload);

};

$data = $request->input('data', 0);

$pipeline = new Pipeline();

return $pipeline

->send($data)

->through([$pipe1, $pipe2])

->then(function ($data) {

return $data;

});

}

今天主要学习学习「Pipeline」,顺便推荐一个 PHP 插件:league/pipeline。composer require league/pipeline

使用起来也很方便use League\Pipeline\Pipeline;

class TimesTwoStage

{

public function __invoke($payload)

{

return $payload * 2;

}

}

class AddOneStage

{

public function __invoke($payload)

{

return $payload + 1;

}

}

$pipeline = (new Pipeline)

->pipe(new TimesTwoStage)

->pipe(new AddOneStage);

// Returns 21

$pipeline->process(10);

接下来我们添加FastRouter在我的项目中使用。

1ac214f879559257ee7e645305e65d72.png

上面的代码修改成这样

749b8780885798bc898345aa0a75ef81.png

我们接下来看看 RespondJson里做了什么.<?php

namespace Platapps\Middlewares;

class RespondJson

{

public function __invoke($payload)

{

header('Content-type:text/json');

return $payload;

}

}

就简单的加了个 header

我们试试把注释到一个渠道

25694f9f877add4740c3bece254d30c2.png

我们再次访问的时候就变成

c3b85d3fbe029e5949fe36e1d7a4fb5a.png

当然这是很简单的中间件,这种中间件远远不够,这里是核心代码,可以去这里看看,也比较简单。

我们最终需要修改pipe这个方法namespace League\Pipeline;

class Pipeline implements PipelineInterface

{

/**

* @var callable[]

*/

private $stages = [];

/**

* @var ProcessorInterface

*/

private $processor;

public function __construct(ProcessorInterface $processor = null, callable ...$stages)

{

$this->processor = $processor ?? new FingersCrossedProcessor;

$this->stages = $stages;

}

public function pipe(callable $stage): PipelineInterface

{

$pipeline = clone $this;

$pipeline->stages[] = $stage;

return $pipeline;

}

public function process($payload)

{

return $this->processor->process($payload, ...$this->stages);

}

public function __invoke($payload)

{

return $this->process($payload);

}

}

这么多框架里面我这里建议拿Tp6的来做参考,功能还算够用。<?php

namespace think;

use Closure;

use Exception;

use Throwable;

class Pipeline

{

protected $passable;

protected $pipes = [];

protected $exceptionHandler;

/**

* 初始数据

* @param $passable

* @return $this

*/

public function send($passable)

{

$this->passable = $passable;

return $this;

}

/**

* 调用栈

* @param $pipes

* @return $this

*/

public function through($pipes)

{

$this->pipes = is_array($pipes) ? $pipes : func_get_args();

return $this;

}

/**

* 执行

* @param Closure $destination

* @return mixed

*/

public function then(Closure $destination)

{

$pipeline = array_reduce(

array_reverse($this->pipes),

$this->carry(),

function ($passable) use ($destination) {

try {

return $destination($passable);

} catch (Throwable | Exception $e) {

return $this->handleException($passable, $e);

}

});

return $pipeline($this->passable);

}

/**

* 设置异常处理器

* @param callable $handler

* @return $this

*/

public function whenException($handler)

{

$this->exceptionHandler = $handler;

return $this;

}

protected function carry()

{

return function ($stack, $pipe) {

return function ($passable) use ($stack, $pipe) {

try {

return $pipe($passable, $stack);

} catch (Throwable | Exception $e) {

return $this->handleException($passable, $e);

}

};

};

}

/**

* 异常处理

* @param $passable

* @param $e

* @return mixed

*/

protected function handleException($passable, Throwable $e)

{

if ($this->exceptionHandler) {

return call_user_func($this->exceptionHandler, $passable, $e);

}

throw $e;

}

}

看完上述内容,你们对如何在PHP中利用Pipeline 实现一个中间件功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值