-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClass5.php
71 lines (58 loc) · 2.26 KB
/
Class5.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
65
66
67
68
69
70
71
<?php
/**
* 管道 与 面向切面编程 (可以理解为中间件)
*/
interface Milldeware {
public static function handle(Closure $next);
}
class VerfiyCsrfToekn implements Milldeware {
public static function handle(Closure $next)
{
echo '验证csrf Token <br>';
$next();
}
}
class VerfiyAuth implements Milldeware {
public static function handle(Closure $next)
{
echo '验证是否登录 <br>';
$next();
}
}
class SetCookie implements Milldeware {
public static function handle(Closure $next)
{
$next();
echo '设置cookie信息!';
}
}
$handle = function() {
echo '当前要执行的程序!';
};
$pipe_arr = [
'VerfiyCsrfToekn',
'VerfiyAuth',
'SetCookie'
];
/**
* array_reduce() 将回调函数 callback 迭代地作用到 array 数组中的每一个单元中,从而将数组简化为单一的值
*
* array_reduce ( array $array , callable $callback , mixed $initial = null ) : mixed
* - $callback(mixed $carry, mixed $item)
* $carry 携带上次迭代里的值; 如果本次迭代是第一次,那么这个值是 initial
* $item 携带了本次迭代的值。
* - $initial
* 如果指定了可选参数 initial,该参数将在处理开始前使用,或者当处理结束,数组为空时的最后一个结果
*/
$callback = array_reduce($pipe_arr,function($stack,$pipe) {
/**
* $pipe 就是 $pipe_arr 数组中每一项的 value
* $stack 迭代的执行顺序 1.$handle 2. VerfiyCsrfToekn->handle() 3.VerfiyAuth->handle()
* 因为 array_reduce() 这个函数, 只要设置了第三个参数($initial), 那么在第一次迭代数组时,会先将 $initial 的值作用于回调函数, 即 $callback 的 $carry 的值就是 $handle
*/
return function() use($stack,$pipe){
return $pipe::handle($stack);
};
},$handle);
// call_user_func(callback $callback, $mixed $parameter) callback 是被调用的回调函数,其余参数是回调函数的参数
call_user_func($callback);