<?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. gamemakerstudio:Sprite的原点

    对于某些类型的游戏,例如飞行射击游戏或者顶视角的2D游戏,通常会直接选取图片的中心做为原点.但对于<冰杖秘闻>这样的 2D ARPG 来说,会更多的从三维场景的角度来选择原点,即选择物品底 ...

  2. Object中的clone方法

      Java中对象的创建 clone顾名思义就是复制, 在Java语言中, clone方法被对象调用,所以会复制对象.所谓的复制对象,首先要分配一个和源对象同样大小的空间,在这个空间中创建一个新的对象 ...

  3. Python 基于固定 IP 来命名 ARM 虚拟机的实现

    问题描述 希望通过 Python 批量创建 ARM 虚拟机,并且在虚拟机命名时加入固定 IP 信息,方便管理维护. 问题分析 在创建 ARM 虚拟机之前,先创建固定 IP,然后获取固定 IP 地址,创 ...

  4. Flask入门 表单Flask-wtf form原生 Bootstrap渲染(七)

    (1) 原生的表单 模板页面,form表单form.html <form action="{{ url_for('/check/') }}" method='post'> ...

  5. 1.appium介绍

    appium介绍 官方网站 1.特点 appium 是一个自动化测试开源工具,支持 iOS 平台和 Android 平台上的原生应用,web应用和混合应用. “移动原生应用”是指那些用iOS或者 An ...

  6. VSCode-python 进阶配置

    VSCode-python 进阶配置 中文乱码 中文乱码,网上一堆解决方法,但是根本没有有效起作用的. 在python脚本的前面添加: # -*- coding:utf-8 -*- 并不能在控制台输出 ...

  7. SAP Fiori应用Footerbar区域按钮的高亮显示逻辑

    如果您够细心,您或许会发现有的SAP Fiori应用的footerbar区域内的按钮有高亮显示,有的则没有. 如何自己分析这两种按钮的实现原理? 还是借助Chrome Development Tool ...

  8. Jmeter启动报错:unable to access jarfile ApacheJmeter.jar error 原因:下载的src包没有这个jar包,需下载binary包

    安装好jdk并配置了环境变量,下载Jmeter包解压启动jemter.bat提示 unable to access jarfile ApacheJmeter.jar error 原因: 从官网 htt ...

  9. “标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法

    “标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法.大多数这些方法都在序列上运行,其中的序列是一个对象,其类型实现了IEnumerable<T> 接口或 IQueryable& ...

  10. 【LOJ6052】「雅礼集训 2017 Day11」DIV(杜教筛)

    点此看题面 大致题意: 求\(1\sim n\)内所有满足\(a>0\)的约数\(a+bi\)的\(a\)之和. 解题思路 首先,我们设\(x=(a+bi)(c+di)(1\le x\le n) ...