<?php

class Pipeline
{ protected $passable;
protected $pipes = [];
protected $method = 'handle'; public function send($passable)
{
$this->passable = $passable; return $this;
} public function through($pipes)
{
$this->pipes = is_array($pipes) ? $pipes : func_get_args(); return $this;
} public function then(\Closure $destination)
{
$pipeline = array_reduce(
array_reverse($this->pipes), $this->carry(), $this->prepareDestination($destination)
); return $pipeline($this->passable);
} protected function prepareDestination(Closure $destination)
{
return function ($passable) use ($destination) {
return $destination($passable);
};
} protected function carry()
{
return function ($stack, $pipe) { // $stack === 下一个目标函数,$pipe == 数组项
return function ($passable) use ($stack, $pipe) {
if (is_callable($pipe)) {
// If the pipe is an instance of a Closure, we will just call it directly but
// otherwise we'll resolve the pipes out of the container and call it with
// the appropriate method and arguments, returning the results back out.
return $pipe($passable, $stack);
} elseif (!is_object($pipe)) {
// list($name, $parameters) = $this->parsePipeString($pipe);
//
// // If the pipe is a string we will parse the string and resolve the class out
// // of the dependency injection container. We can then build a callable and
// // execute the pipe function giving in the parameters that are required.
// $pipe = $this->getContainer()->make($name);
//
// $parameters = array_merge([$passable, $stack], $parameters);
$pipe = new $pipe;
$parameters = array_merge([$passable, $stack], []);
} else {
// If the pipe is already an object we'll just make a callable and pass it to
// the pipe as-is. There is no need to do any extra parsing and formatting
// since the object we're given was already a fully instantiated object.
$parameters = [$passable, $stack];
} return method_exists($pipe, $this->method)
? $pipe->{$this->method}(...$parameters)
: $pipe(...$parameters);
};
};
} } class FooOneMiddleware
{
public function handle($request, \Closure $next)
{
echo __METHOD__.":".$request.PHP_EOL;
return $next($request);
}
} class FooTwoMiddleware
{
public function handle($request, \Closure $next)
{
echo __METHOD__.":".$request.PHP_EOL;
return $next($request);
}
} /*
function array_reduce($array, $callback, $initial=null)
{
$acc = $initial;
foreach($array as $a){
$acc = $callback($acc, $a);
}
return $acc;
}
*/
{
$middleware = [
FooOneMiddleware::class,
FooTwoMiddleware::class
];
$then = function () {
return 'this is output...'.PHP_EOL;
};
$result = (new Pipeline())
->send('this is input')
->through($middleware)
->then($then);
echo $result;
}

  

Lavavel5.5源代码 - Pipeline的更多相关文章

  1. Lavavel5.5源代码 - RedisQueue是怎么实现

    队列的基本功能: 1.立即执行:yes 2.延迟执行:yes 3.保证至少执行一次:yes 4.必须执行且最多执行一次:no 用到的数据结构: list.Sorted sets 延迟执行的机制: 1. ...

  2. Lavavel5.5源代码 - 并发数控制

    app('redis')->connection('default')->funnel('key000') // 每个资源最大锁定10秒自动过期,只有60个资源(并发),在3秒内获取不到锁 ...

  3. Lavavel5.5源代码 - 限流工具

    app('redis')->connection('default')->throttle('key000') // 每60秒,只能有10个资源被获取,在3秒内获取不到锁抛出异常 -> ...

  4. jenkins2 pipeline插件的10个最佳实践

    jenkins pipeline的10个最佳实践. 文章来自:http://www.ciandcd.com文中的代码来自可以从github下载: https://github.com/ciandcd ...

  5. jenkins2 pipeline实例

    比较完整的实例,使用了maven打包,git tag,发通知邮件,等待用户deploy,deploy到nexus. 文章来自:http://www.ciandcd.com文中的代码来自可以从githu ...

  6. jenkins2 pipeline入门

    本文通过简单的pipeline的实例和详细的讲解,能够学习基本pipeline的groovy用法,然后开始实现自己的pipeline job. 翻译和修改自:https://github.com/je ...

  7. OpenStack Cinder源代码流程简析

    版权声明:本博客欢迎转载,转载时请以超链接形式标明文章原始出处!谢谢! 博客地址:http://blog.csdn.net/i_chips 一.概况 OpenStack的各个模块都有对应的client ...

  8. 浅谈管道模型(Pipeline)

    本篇和大家谈谈一种通用的设计与处理模型--Pipeline(管道). Pipeline简单介绍 Pipeline模型最早被使用在Unix操作系统中.据称,假设说Unix是计算机文明中最伟大的发明,那么 ...

  9. 【Spark Core】任务运行机制和Task源代码浅析1

    引言 上一小节<TaskScheduler源代码与任务提交原理浅析2>介绍了Driver側将Stage进行划分.依据Executor闲置情况分发任务,终于通过DriverActor向exe ...

随机推荐

  1. latex在线编辑网址

    https://www.overleaf.com/7876326cfzygjtvbrzh#/27713363/

  2. 编译时提示软件包 javax.servlet.http 不存在 import javax.servlet.http.HttpServletRequest;

    编译时错误:软件包 javax.servlet.http 不存在import javax.servlet.http.HttpServletRequest; 解决办法:把servlet-api.jar加 ...

  3. greenplum导数据

    Greenplum and Hadoop HDFS integration Step 1: Install Java on greenplum Step 2: Set JAVA and HADOOP ...

  4. 转:一个优秀windows C++程序员的知识体系

    转自:http://www.cppblog.com/weiym/archive/2012/06/10/178287.html.根据自身的经历,觉得作者总结的很好. 思考一个优秀windows C++ ...

  5. 使用 richtextbox 输出程序运行信息

    private delegate void Refresh_CallBack(Color color,string text); private void ControlsRefresh(Color ...

  6. Nginx+Tomcat在Windows下做负载均衡

    一. 为什么需要对Tomcat服务器做负载均衡 Tomcat服务器作为一个Web服务器,其并发数在300-500之间,如果有超过500的并发数便会出现Tomcat不能响应新的请求的情况,严重影响网站的 ...

  7. CSS3中REM使用详解

    px 在Web页面制作中,我们一般使用“px”来设置我们的文本,因为他比较稳定和精确.但是这种方法存在一个问题,当用户在浏览器中浏览我们制作的Web页面时,他改变了浏览器的字体大小(虽然一般人不会去改 ...

  8. netstat 和 lsof 查看网络状态

    netstat和lsof都是linux下的工具,可以用于查看系统的网络状态. netstat netstat可以打印 网络连接,路由表,接口统计数据,还有多播和masquerade连接相关的东西(不熟 ...

  9. thinkphp5设置403 404等http状态页面

    在thinkphp5中如何抛出异常状态码(比如401,403,404等),因为这些能极大的给用户以良好的体验. 因为在上线阶段,任何的系统错误信息都不能让浏览用户给看到,比如404(Not Found ...

  10. makefile中的循环控制

    GNU make的foreach函数 foreach函数仅GNU make支持: 下面的代码中使用了函数foreach和shell files=main.exe a.exe b.exe all: ec ...