This repository was archived by the owner on Jul 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnatch.php
More file actions
104 lines (85 loc) · 2.49 KB
/
snatch.php
File metadata and controls
104 lines (85 loc) · 2.49 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
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
<?php
/*
Snatch PHP router 1.0
Matvey Pravosudov
github.com/oxyberg/snatch
*/
class Snatch {
const version = '1.0';
protected
$routes = array(),
$shared = array(),
$wildcards = array(
'(:any)' => '([a-zA-Z0-9\.\-_%]+)',
'(:num)' => '([0-9]+)',
'(:all)' => '(.*)'
),
$error;
// register new routes
public function __call($verb, $args) {
if (in_array($verb, ['get', 'post', 'put', 'delete'])) {
$this->match($verb, $args[0], $args[1]);
}
}
// register 404 page
public function missing($callback) {
$this->error = ($callback);
}
// set route
public function match($methods, $pattern, $callback) {
foreach (explode('|', $methods) as $method) {
$pattern = str_replace(array_keys($this->wildcards), array_values($this->wildcards), $pattern);
$this->routes[$method]['#^' . '/' . trim($pattern, '/') . '$#'] = $callback;
}
}
// add new wildcard
public function wildcard($name, $pattern = false) {
if ($pattern) $this->wildcards['(:' . $name . ')'] = $pattern;
else if (is_array($name) && ! $pattern) {
foreach ($name as $key => $value) {
$this->wildcards['(:' . $key . ')'] = $value;
}
}
}
// run router
public function run () {
// get request method
$method = strtolower($_SERVER['REQUEST_METHOD']);
// handle all routes
$handled = 0;
if ($this->routes[$method]) $handled = $this->handle($this->routes[$method]);
// 404
if ($handled === 0) {
if (is_callable($this->error)) call_user_func($this->error);
else header('HTTP/1.1 404 Not Found');
}
}
// handle request
private function handle($routes) {
$handled = 0;
$base = $this->current();
foreach ($routes as $pattern => $callback) {
if (preg_match_all($pattern, $base, $matches)) {
$params = array_map(function ($match) {
$var = explode('/', trim($match, '/'));
return isset($var[0]) ? $var[0] : null;
}, array_slice($matches[0], 1));
call_user_func_array($callback, $params);
$handled++;
}
}
return $handled;
}
// return current url
public function current() {
$uri = $_SERVER['REQUEST_URI'];
// remove rewrite basepath (= allows one to run the router in a subfolder)
$basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
$uri = substr($uri, strlen($basepath));
// don't take query params into account on the URL
if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?'));
// remove trailing slash + enforce a slash at the start
$uri = '/' . trim($uri, '/');
return $uri;
}
}