ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ <?php function middleware($handlers,$arguments = []){ //函数栈 $stack = []; $result = null; foreach ($handlers as $handler) { // 每次循环之前重置,只能保存最后一个处理程序的返回值 $result = null; $generator = call_user_func_array($handler, $arguments); if ($generator instanceof \Generator) { //将协程函数入栈,为重入做准备 $stack[] = $generator; //获取协程返回参数 $yieldValue = $generator->current(); //检查是否重入函数栈 if ($yieldValue === false) { break; } } elseif ($generator !== null) { //重入协程参数 $result = $generator; } } $return = ($result !== null); //将协程函数出栈 while ($generator = array_pop($stack)) { if ($return) { $generator->send($result); } else { $generator->next(); } } } $abc = function(){ echo "this is abc start \n"; yield; echo "this is abc end \n"; }; $qwe = function (){ echo "this is qwe start \n"; $a = yield; echo $a."\n"; echo "this is qwe end \n"; }; $one = function (){ return 1; }; middleware([$abc,$qwe,$one]); /* * output * * this is abc start * this is qwe start * 1 * this is qwe end * this is abc end */ ~~~ 通过middleware()方法我们就实现了一个这样的效果 ~~~ (begin) ----------------> function() -----------------> (end) ^ ^ ^ ^ ^ ^ | | | | | | | | +------- M1() ------+ | | | +----------- ... ----------+ | +--------------- Mn() --------------+ ~~~~