<?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;
}

  

最新文章

  1. django静态文件配置
  2. Hack语言的类型系统
  3. FilterDispatcher已被标注为过时解决办法 &gt;&gt;&gt; FilterDispatcher &lt;&lt;&lt; is deprecated!
  4. python 操作消息队列
  5. iOS开发中的内存分配(堆和栈)
  6. Timed Code
  7. ubuntu server 安装
  8. .NET Mvc Razor
  9. TitleLayout——一个Android轻松实现标题栏的库
  10. [BZOJ 1095] [ZJOI 2007] 捉迷藏
  11. Python简单基础小程序
  12. 实现简单的printf函数
  13. CentOS7.5脱机安装SQL Server 2017(NEW)
  14. VMware要不要装在固态SSD上,虚拟机系统文件要不要放固态SSD上,虚拟机伤不伤固态SSD
  15. Windows系统编程之异步I/O和完成端口
  16. 如何获取控件id,包名,类名
  17. 使用Hexo + Github Pages搭建个人独立博客
  18. kibana提示&quot;[illegal_argument_exception] mapper [hits] cannot be changed from type [long] to [integer]&quot;
  19. javascript数字转大写
  20. Java使用Log4记录日志

热门文章

  1. 缩短移动开发周期的ApiCloud
  2. 获取select下拉框的value以及文本内容
  3. 网络威胁防护,Azure 靠的是它?
  4. python多重继承
  5. rel 属性&lt;small&gt;H5保留属性&lt;/small&gt;
  6. C 中重载一词中的“重”字读ZHONG4还是CHONG2?
  7. ZT acct 中文man页面(1)
  8. 如何证明CRM WebClient UI上的应用是有状态(Stateful)的
  9. HTTP 状态码 301 302
  10. note03-计算机网络