Lavavel5.5源代码 - Pipeline
2024-09-04 17:38:51
<?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;
}
最新文章
- django静态文件配置
- Hack语言的类型系统
- FilterDispatcher已被标注为过时解决办法 >;>;>; FilterDispatcher <;<;<; is deprecated!
- python 操作消息队列
- iOS开发中的内存分配(堆和栈)
- Timed Code
- ubuntu server 安装
- .NET Mvc Razor
- TitleLayout——一个Android轻松实现标题栏的库
- [BZOJ 1095] [ZJOI 2007] 捉迷藏
- Python简单基础小程序
- 实现简单的printf函数
- CentOS7.5脱机安装SQL Server 2017(NEW)
- VMware要不要装在固态SSD上,虚拟机系统文件要不要放固态SSD上,虚拟机伤不伤固态SSD
- Windows系统编程之异步I/O和完成端口
- 如何获取控件id,包名,类名
- 使用Hexo + Github Pages搭建个人独立博客
- kibana提示";[illegal_argument_exception] mapper [hits] cannot be changed from type [long] to [integer]";
- javascript数字转大写
- Java使用Log4记录日志
热门文章
- 缩短移动开发周期的ApiCloud
- 获取select下拉框的value以及文本内容
- 网络威胁防护,Azure 靠的是它?
- python多重继承
- rel 属性<;small>;H5保留属性<;/small>;
- C 中重载一词中的“重”字读ZHONG4还是CHONG2?
- ZT acct 中文man页面(1)
- 如何证明CRM WebClient UI上的应用是有状态(Stateful)的
- HTTP 状态码 301 302
- note03-计算机网络