-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.php
executable file
·206 lines (167 loc) · 5.11 KB
/
rest.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
class RestRequest {
public $params = null;
public $headers = null;
public function method() {
return $_SERVER['REQUEST_METHOD'];
}
public function isGet() { return $this->method() == 'GET'; }
public function isPost() { return $this->method() == 'POST'; }
public function isPut() { return $this->method() == 'PUT'; }
public function isDelete() { return $this->method() == 'DELETE'; }
public function isHead() { return $this->method() == 'HEAD'; }
public function params() {
if (!is_null($this->params))
return $this->params;
$this->params = array_merge($_GET, $this->getBodyParams());
return $this->params;
}
public function headers() {
if (!is_null($this->headers))
return $this->headers;
$this->headers = getallheaders();
return $this->headers;
}
public function header($name) {
$headers = $this->headers();
return isset($headers[$name]) ? $headers[$name] : '';
}
public function path() {
return parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
}
public function relativePath() {
$request_uri = $this->path();
$script_dir = dirname($_SERVER['SCRIPT_NAME']);
if (strncmp($script_dir, $request_uri, strlen($script_dir)) === 0) {
return substr($request_uri, strlen($script_dir));
} else {
return $request_uri;
}
}
private function getBodyParams() {
if ($this->isGet() || $this->isHead()) {
return array();
} else if ($this->isPost()) {
return $_POST;
} else {
parse_str(file_get_contents('php://input'), $result);
return $result;
}
}
}
class RestService {
private static $mime_types = array(
'html' => 'text/html',
'json' => 'application/json',
'plain' => 'text/plain',
'xhtml' => 'application/xhtml+xml',
'xml' => 'application/xml',
);
protected $request = null;
public function setRequest(RestRequest $request) {
$this->request = $request;
}
public function status($status) {
header($_SERVER['SERVER_PROTOCOL'] . ' ' . $status);
}
public function notFound() {
$this->status(404);
$this->contentType('plain');
return "Not found";
}
public function unauthorized() {
$this->status(401);
$this->contentType('plain');
return "Unauthorized";
}
public function notAllowed() {
$this->status(405);
$this->contentType('plain');
return "Not allowed";
}
public function notImplemented() {
$this->status(501);
$this->contentType('plain');
return 'Not implemented';
}
public function contentType($type, $encoding='utf-8') {
// TODO: Output encoding
if (isset(RestService::$mime_types[$type]))
$type = RestService::$mime_types[$type];
header('Content-Type: ' . $type);
}
}
class RestRoute {
private $method;
private $path;
private $compiled;
private $keys;
public function __construct($method, $path) {
$this->method = $method;
$this->path = $path;
$this->compile();
}
public function match($method, $path) {
if (strcasecmp($this->method, $method) !== 0)
return false;
if (!preg_match($this->compiled, $path, $groups))
return false;
if (empty($this->keys))
return array();
array_shift($groups);
return array_combine($this->keys, $groups);
}
private function compile() {
$this->keys = array();
$parts = explode('/', $this->path);
foreach ($parts as $key => $part) {
if (strlen($part) == 0) continue;
if ($part[0] == ':') {
$parts[$key] = '([^/]+)';
$this->keys[] = substr($part, 1);
} else {
$parts[$key] = preg_quote($part, '@');
}
}
$this->compiled = '@^' . join('/', $parts) . '$@';
}
}
class RestDispatcher {
private $routes = array();
private $service;
public function __construct(RestService $service) {
$this->service = $service;
}
public function addRoute($method, $path, $function) {
$this->routes[] = array(new RestRoute($method, $path), $function);
if (strtoupper($method) == 'GET') {
$this->routes[] = array(new RestRoute('HEAD', $path), $function);
}
}
public function dispatch() {
$request = new RestRequest();
if (($route = $this->findMatchingRoute($request)) === false) {
echo $this->service->notFound();
return false;
}
list($route, $function, $route_params) = $route;
$params = array_merge($request->params(), $route_params);
$this->service->setRequest($request);
$result = $this->service->$function($params);
if (!$request->isHead())
echo $result;
return true;
}
private function findMatchingRoute(RestRequest $request) {
$method = $request->method();
$path = $request->relativePath();
foreach ($this->routes as $route) {
list($route, $function) = $route;
if (($route_params = $route->match($method, $path)) !== false) {
return array($route, $function, $route_params);
}
}
return false;
}
}
?>