1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace puck\helpers; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class Dispatch |
8
|
|
|
{ |
9
|
|
|
static $path; |
|
|
|
|
10
|
|
|
static $ext; |
|
|
|
|
11
|
|
|
static public function init() { |
|
|
|
|
12
|
|
|
if (!IS_CLI) { |
13
|
|
|
define('NOW_TIME', $_SERVER['REQUEST_TIME']); |
14
|
|
|
define('REQUEST_METHOD', $_SERVER['REQUEST_METHOD']); |
15
|
|
|
define('IS_GET', REQUEST_METHOD == 'GET' ? true : false); |
16
|
|
|
define('IS_POST', REQUEST_METHOD == 'POST' ? true : false); |
17
|
|
|
define('IS_PUT', REQUEST_METHOD == 'PUT' ? true : false); |
18
|
|
|
define('IS_DELETE', REQUEST_METHOD == 'DELETE' ? true : false); |
19
|
|
|
define('IS_AJAX', ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) ? true : false); |
20
|
|
|
define('__SELF__', strip_tags($_SERVER['REQUEST_URI'])); |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
static public function dispatch($path='', $app='\\admin') { |
|
|
|
|
24
|
|
|
self::init(); |
25
|
|
|
self::$path=$path; |
26
|
|
|
if ($path == '') { |
27
|
|
|
$path=array(); |
28
|
|
|
} else { |
29
|
|
|
$path=str_replace('-', '_', $path); |
30
|
|
|
$path=explode('/', $path); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (count($path) == 0) { |
34
|
|
|
array_push($path, 'home'); |
35
|
|
|
array_push($path, 'index'); |
36
|
|
|
} elseif (count($path) == 1) { |
37
|
|
|
array_push($path, 'index'); |
38
|
|
|
} |
39
|
|
|
if (!empty($path)) { |
40
|
|
|
$tmpAction=array_pop($path); |
41
|
|
|
$tmpArray=explode(".",$tmpAction); |
42
|
|
|
self::$ext=$tmpArray[1]??""; |
43
|
|
|
$tmpAction=preg_replace('/\.(html|aspx|do|php|htm|h5|api|json|xml)$/i', '', $tmpAction); |
44
|
|
|
$tmpAction=parse_name($tmpAction, 1); |
45
|
|
|
$var['a']=$tmpAction; |
|
|
|
|
46
|
|
|
} |
47
|
|
|
define('ACTION_NAME', $var['a']); |
|
|
|
|
48
|
|
|
if (!preg_match('/^[A-Za-z](\w)*$/', ACTION_NAME)) { |
49
|
|
|
die("error action"); |
50
|
|
|
} |
51
|
|
|
if (!empty($path)) { |
52
|
|
|
$tmpController=array_pop($path); |
53
|
|
|
$tmpController=parse_name($tmpController, 1); |
54
|
|
|
$var['c']=$tmpController; |
55
|
|
|
} |
56
|
|
|
define('CONTROLLER_NAME', $var['c']); |
57
|
|
|
if (!preg_match('/^[A-Za-z](\/|\w)*$/', CONTROLLER_NAME)) { |
58
|
|
|
die("error controller"); |
59
|
|
|
} |
60
|
|
|
$class=$app.'\\controllers\\'.ucfirst(CONTROLLER_NAME); |
61
|
|
|
if (!class_exists($class)) { |
62
|
|
|
not_found('this controller is can not work now!'); |
63
|
|
|
} |
64
|
|
|
$class=new $class(); |
65
|
|
|
if (!method_exists($class, ACTION_NAME)) { |
66
|
|
|
not_found(); |
67
|
|
|
} |
68
|
|
|
self::param(); |
69
|
|
|
self::exec($class, ACTION_NAME); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $function |
74
|
|
|
*/ |
75
|
|
|
static public function exec($class, $function) { |
|
|
|
|
76
|
|
|
$method=new \ReflectionMethod($class, $function); |
77
|
|
|
if ($method->isPublic() && !$method->isStatic()) { |
78
|
|
|
$refClass=new \ReflectionClass($class); |
79
|
|
|
//前置方法 |
80
|
|
|
if ($refClass->hasMethod('_before_'.$function)) { |
81
|
|
|
$before=$refClass->getMethod('_before_'.$function); |
82
|
|
|
if ($before->isPublic()) { |
83
|
|
|
$before->invoke($class); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
//方法本身 |
87
|
|
|
$response=$method->invoke($class); |
88
|
|
|
//后置方法 |
89
|
|
|
if ($refClass->hasMethod('_after_'.$function)) { |
90
|
|
|
$after=$refClass->getMethod('_after_'.$function); |
91
|
|
|
if ($after->isPublic()) { |
92
|
|
|
$after->invoke($class); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
self::render($response); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
static public function param() { |
|
|
|
|
99
|
|
|
if (!IS_CLI) { |
100
|
|
|
$vars=array(); |
101
|
|
|
parse_str(parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY), $vars); |
102
|
|
|
$_GET=$vars; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
static public function render($res) { |
|
|
|
|
107
|
|
|
$response=$res; |
108
|
|
|
$renderType=self::$ext; |
109
|
|
|
|
110
|
|
|
if($renderType=='json'){ |
111
|
|
|
$response=json($res); |
|
|
|
|
112
|
|
|
}elseif ($renderType=='xml'){ |
113
|
|
|
//todo:: 支持xml格式化输出 |
114
|
|
|
$response='don\'t support xml now!'; |
115
|
|
|
}elseif ($renderType==""){ |
116
|
|
|
if(is_array($res)||is_object($res)){ |
117
|
|
|
$response=json($res); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
echo $response; |
122
|
|
|
} |
123
|
|
|
} |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.