|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace puck\helpers; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class Dispatch |
|
8
|
|
|
{ |
|
9
|
|
|
static public function init(){ |
|
|
|
|
|
|
10
|
|
|
define('NOW_TIME', $_SERVER['REQUEST_TIME']); |
|
11
|
|
|
define('REQUEST_METHOD', $_SERVER['REQUEST_METHOD']); |
|
12
|
|
|
define('IS_GET', REQUEST_METHOD == 'GET' ? true : false); |
|
13
|
|
|
define('IS_POST', REQUEST_METHOD == 'POST' ? true : false); |
|
14
|
|
|
define('IS_PUT', REQUEST_METHOD == 'PUT' ? true : false); |
|
15
|
|
|
define('IS_DELETE', REQUEST_METHOD == 'DELETE' ? true : false); |
|
16
|
|
|
define('IS_AJAX', ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ) ? true : false); |
|
17
|
|
|
define('__SELF__', strip_tags($_SERVER['REQUEST_URI'])); |
|
18
|
|
|
} |
|
19
|
|
|
static public function dispatch($path='',$app='\\admin') { |
|
|
|
|
|
|
20
|
|
|
self::init(); |
|
21
|
|
|
if($path==''){ |
|
22
|
|
|
$path=array(); |
|
23
|
|
|
}else{ |
|
24
|
|
|
$path=str_replace('-','_',$path); |
|
25
|
|
|
$path = explode('/',$path); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
if(count($path)==0){ |
|
29
|
|
|
array_push($path,'home'); |
|
30
|
|
|
array_push($path,'index'); |
|
31
|
|
|
} |
|
32
|
|
|
elseif (count($path)==1){ |
|
33
|
|
|
array_push($path,'index'); |
|
34
|
|
|
} |
|
35
|
|
|
if(!empty($path)){ |
|
36
|
|
|
$tmpAction=array_pop($path); |
|
37
|
|
|
$tmpAction=preg_replace('/\.(html|aspx|do|php|htm|h5|api)$/i', '', $tmpAction); |
|
38
|
|
|
$tmpAction=parse_name($tmpAction,1); |
|
39
|
|
|
$var['a']=$tmpAction; |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
define('ACTION_NAME',$var['a']); |
|
|
|
|
|
|
42
|
|
|
if (!preg_match('/^[A-Za-z](\w)*$/', ACTION_NAME)) { |
|
43
|
|
|
die("error action"); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
if(!empty($path)){ |
|
46
|
|
|
$tmpController=array_pop($path); |
|
47
|
|
|
$tmpController=parse_name($tmpController,1); |
|
48
|
|
|
$var['c']=$tmpController; |
|
49
|
|
|
} |
|
50
|
|
|
define('CONTROLLER_NAME',$var['c']); |
|
51
|
|
|
if (!preg_match('/^[A-Za-z](\/|\w)*$/', CONTROLLER_NAME)) { |
|
52
|
|
|
die("error controller"); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
$class=$app.'\\controllers\\'.ucfirst(CONTROLLER_NAME); |
|
55
|
|
|
if (!class_exists($class)) { |
|
56
|
|
|
not_found('this controller is can not work now!'); |
|
57
|
|
|
} |
|
58
|
|
|
$class= new $class(); |
|
59
|
|
|
if (!method_exists($class,ACTION_NAME)) { |
|
60
|
|
|
not_found(); |
|
61
|
|
|
} |
|
62
|
|
|
self::param(); |
|
63
|
|
|
self::exec($class,ACTION_NAME); |
|
64
|
|
|
} |
|
65
|
|
|
static public function exec($class,$function){ |
|
|
|
|
|
|
66
|
|
|
$method = new \ReflectionMethod($class, $function); |
|
67
|
|
|
if ($method->isPublic() && !$method->isStatic()) { |
|
68
|
|
|
$refClass = new \ReflectionClass($class); |
|
69
|
|
|
//前置方法 |
|
70
|
|
|
if ($refClass->hasMethod('_before_' . $function)) { |
|
71
|
|
|
$before = $refClass->getMethod('_before_' . $function); |
|
72
|
|
|
if ($before->isPublic()) { |
|
73
|
|
|
$before->invoke($class); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
//方法本身 |
|
77
|
|
|
$response=$method->invoke($class); |
|
78
|
|
|
//后置方法 |
|
79
|
|
|
if ($refClass->hasMethod('_after_' . $function)) { |
|
80
|
|
|
$after = $refClass->getMethod('_after_' . $function); |
|
81
|
|
|
if ($after->isPublic()) { |
|
82
|
|
|
$after->invoke($class); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
self::render($response); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
static public function param(){ |
|
|
|
|
|
|
89
|
|
|
$vars=array(); |
|
90
|
|
|
parse_str(parse_url($_SERVER['REQUEST_URI'],PHP_URL_QUERY),$vars); |
|
91
|
|
|
$_GET=$vars; |
|
92
|
|
|
} |
|
93
|
|
|
static public function render($res){ |
|
|
|
|
|
|
94
|
|
|
$response=$res; |
|
95
|
|
|
if(is_array($res)){ |
|
96
|
|
|
$response=json($res); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
echo $response; |
|
99
|
|
|
} |
|
100
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: