AsyncHook.php
1.1 KB
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
<?php
namespace tinymeng\tools\async;
class AsyncHook{
/**
* @var array
*/
private static $hook_list = array();
/**
* @var bool
*/
private static $hooked = false;
/**
* hook函数 fastcgi_finish_request执行
* @param callback $callback
* @param array $params
*/
public static function hook(callable $callback, $params) {
self::$hook_list[] = array('callback' => $callback, 'params' => $params);
if(self::$hooked == false) {
self::$hooked = true;
//注册一个callback当前在脚本执行完后执行
register_shutdown_function(array(__CLASS__, '__run'));
}
}
/**
* 由系统调用
* @return void
*/
private static function __run() {
fastcgi_finish_request();
if(empty(self::$hook_list)) {
return;
}
foreach(self::$hook_list as $hook) {
$callback = $hook['callback'];
$params = $hook['params'];
call_user_func_array($callback, $params);
}
}
}