-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathroute.php
125 lines (109 loc) · 3.44 KB
/
route.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
class route {
public static $uri;
private static $method;
private static $root = '/';
static $runed = false;
public static $patterns = array(
'#any' => '[^/]+',
'#num' => '[0-9]+',
'#all' => '.*',
);
public static function __callstatic($method, $args) {
if (self::$runed) {
return;
}
if (empty(self::$method)) {
self::init();
}
$method = strtoupper($method);
if ($method != self::$method && !in_array($method, ['ANY', 'ERROR', 'ON'])) {
return;
}
$pattern = trim(array_shift($args), '\/');
$pattern = self::$root . $pattern;
if (self::uri_match($pattern, self::$uri)) {
if (is_string($args[0]) && strpos($args[0], '@') > 0) {
list($class, $action) = explode('@', $args[0]);
$object = new $class();
$args[0] = array($object, $action);
}
$return = call_user_func($args[0]);
if (is_array($return)) {
print json_encode($return);
} else {
print (string) $return;
}
self::$runed = true;
}
}
public static function init($path = null) {
if (!empty(self::$method)) {return;}
self::$uri = self::get_uri();
self::$method = empty($_POST['_METHOD']) ? $_SERVER['REQUEST_METHOD'] : $_POST['_METHOD'];
if (defined('CONTROLLER_PATH')) {
spl_autoload_register(function ($class) {
$file = CONTROLLER_PATH . $class . '.php';
if (file_exists($file)) {
include $file;
}
});
}
}
public static function auto($controller_path) {
self::init();
$uri = self::get_uri();
list($tmp, $controller, $action) = explode('/', $uri);
$controller = empty($controller) ? 'IndexController' : ucfirst($controller) . 'Controller';
$action = empty($action) ? 'index' : $action;
$file = $controller_path . $controller . '.php';
if (file_exists($file)) {
include $file;
if (is_callable(array($controller, $action))) {
$obj = new $controller();
print (string) $obj->$action();
return;
}
}
}
public static function resource($name, $controller) {
self::get('/' . $name, $controller . '@index');
self::get('/' . $name . '/add', $controller . '@add');
self::post('/' . $name, $controller . '@store');
self::get('/' . $name . '/{id:#num}', $controller . '@show');
self::get('/' . $name . '/{id:#num}/edit', $controller . '@edit');
self::post('/' . $name . '/{id:#num}', $controller . '@update');
self::get('/' . $name . '/{id:#num}/delete', $controller . '@delete');
}
public static function uri_match($pattern, $uri) {
$pattern = ($pattern == '/') ? '/' : rtrim($pattern, '\/');
$searches = array_keys(static::$patterns);
$replaces = array_values(static::$patterns);
$pattern = str_replace($searches, $replaces, $pattern);
$pattern = preg_replace("`\{(\w+)\:([^\)]+)\}`", '(?P<$1>$2)', $pattern);
if (preg_match("`^{$pattern}$`", $uri)) {
preg_match_all("`^{$pattern}$`", $uri, $matches, PREG_PATTERN_ORDER);
foreach ($matches as $key => $value) {
if (!is_int($key)) {
$_GET[$key] = $matches[$key][0];
}
}
return true;
}
}
public static function get_uri() {
$file = basename($_SERVER['PHP_SELF']);
$path = dirname($_SERVER['PHP_SELF']);
$req_uri = $_SERVER['REQUEST_URI'];
if ($path != '/' && strpos($req_uri, $path) === 0) {
$req_uri = substr($req_uri, strlen($path));
}
if (strpos($req_uri, '/?/') === 0) {
$req_uri = parse_url($req_uri, PHP_URL_QUERY);
list($req_uri) = explode('&', $req_uri);
unset($_GET[$req_uri]);
}
$uri = parse_url($req_uri, PHP_URL_PATH);
return '/' . trim($uri, '\/');
}
}