-
Notifications
You must be signed in to change notification settings - Fork 4
/
demo.php
64 lines (55 loc) · 1.16 KB
/
demo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace TerryLin\Middleware;
use Closure;
/**
* PHP中,中间件概念的简单实现
*
*/
Class Demo
{
//响应的数据
public $response;
//绑定的中间件
private $stack = [];
/**
* 执行中间件函数
* @param mix $request 请求数据
* @param mix $response 响应数据
* @param Closure $next 把处理完的结果返回给下一个中间件
* @return Closure
*/
private function handle( $request, $response, Closure $next )
{
return $next($request,$response);
}
/**
* 绑定中间件
*/
public function bind(Closure $middleware)
{
$this->stack[]=$middleware;
return true;
}
/**
* 执行函数
* @param mix $request 请求数据
* @param mix $response 响应数据
* @return mix
*/
public function run($request,$response='')
{
$this->response = $response;
foreach ($this->stack as $key => $value)
{
$this->response = $this->handle($request,$this->response,$value);
}
return $this->response;
}
}
$demo = new Demo();
$demo->bind(
function($requset,$response){
$response = $response.$requset.'filter By middleware!';
return $response;
});
var_dump( $demo->run('someQueryParams') );