diff options
Diffstat (limited to 'inc/3rdparty/PicoFarad/Router.php')
-rw-r--r-- | inc/3rdparty/PicoFarad/Router.php | 157 |
1 files changed, 0 insertions, 157 deletions
diff --git a/inc/3rdparty/PicoFarad/Router.php b/inc/3rdparty/PicoFarad/Router.php deleted file mode 100644 index b62b8e2d..00000000 --- a/inc/3rdparty/PicoFarad/Router.php +++ /dev/null | |||
@@ -1,157 +0,0 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace PicoFarad\Router; | ||
4 | |||
5 | // Load controllers: bootstrap('controllers', 'controller1', 'controller2') | ||
6 | function bootstrap() | ||
7 | { | ||
8 | $files = \func_get_args(); | ||
9 | $base_path = array_shift($files); | ||
10 | |||
11 | foreach ($files as $file) { | ||
12 | require $base_path.'/'.$file.'.php'; | ||
13 | } | ||
14 | } | ||
15 | |||
16 | // Execute a callback before each action | ||
17 | function before($value = null) | ||
18 | { | ||
19 | static $before_callback = null; | ||
20 | |||
21 | if (is_callable($value)) { | ||
22 | $before_callback = $value; | ||
23 | } | ||
24 | else if (is_callable($before_callback)) { | ||
25 | $before_callback($value); | ||
26 | } | ||
27 | } | ||
28 | |||
29 | // Execute a callback before a specific action | ||
30 | function before_action($name, $value = null) | ||
31 | { | ||
32 | static $callbacks = array(); | ||
33 | |||
34 | if (is_callable($value)) { | ||
35 | $callbacks[$name] = $value; | ||
36 | } | ||
37 | else if (isset($callbacks[$name]) && is_callable($callbacks[$name])) { | ||
38 | $callbacks[$name]($value); | ||
39 | } | ||
40 | } | ||
41 | |||
42 | // Execute an action | ||
43 | function action($name, \Closure $callback) | ||
44 | { | ||
45 | $handler = isset($_GET['action']) ? $_GET['action'] : 'default'; | ||
46 | |||
47 | if ($handler === $name) { | ||
48 | before($name); | ||
49 | before_action($name); | ||
50 | $callback(); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | // Execute an action only for POST requests | ||
55 | function post_action($name, \Closure $callback) | ||
56 | { | ||
57 | if ($_SERVER['REQUEST_METHOD'] === 'POST') { | ||
58 | action($name, $callback); | ||
59 | } | ||
60 | } | ||
61 | |||
62 | // Execute an action only for GET requests | ||
63 | function get_action($name, \Closure $callback) | ||
64 | { | ||
65 | if ($_SERVER['REQUEST_METHOD'] === 'GET') { | ||
66 | action($name, $callback); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | // Run when no action have been executed before | ||
71 | function notfound(\Closure $callback) | ||
72 | { | ||
73 | before('notfound'); | ||
74 | before_action('notfound'); | ||
75 | $callback(); | ||
76 | } | ||
77 | |||
78 | // Match a request like this one: GET /myhandler | ||
79 | function get($url, \Closure $callback) | ||
80 | { | ||
81 | find_route('GET', $url, $callback); | ||
82 | } | ||
83 | |||
84 | // Match a request like this one: POST /myhandler | ||
85 | function post($url, \Closure $callback) | ||
86 | { | ||
87 | find_route('POST', $url, $callback); | ||
88 | } | ||
89 | |||
90 | // Match a request like this one: PUT /myhandler | ||
91 | function put($url, \Closure $callback) | ||
92 | { | ||
93 | find_route('PUT', $url, $callback); | ||
94 | } | ||
95 | |||
96 | // Match a request like this one: DELETE /myhandler | ||
97 | function delete($url, \Closure $callback) | ||
98 | { | ||
99 | find_route('DELETE', $url, $callback); | ||
100 | } | ||
101 | |||
102 | // Define which callback to execute according to the URL and the HTTP verb | ||
103 | function find_route($method, $route, \Closure $callback) | ||
104 | { | ||
105 | if ($_SERVER['REQUEST_METHOD'] === $method) { | ||
106 | |||
107 | if (! empty($_SERVER['QUERY_STRING'])) { | ||
108 | $url = substr($_SERVER['REQUEST_URI'], 0, -(strlen($_SERVER['QUERY_STRING']) + 1)); | ||
109 | } | ||
110 | else { | ||
111 | $url = $_SERVER['REQUEST_URI']; | ||
112 | } | ||
113 | |||
114 | $params = array(); | ||
115 | |||
116 | if (url_match($route, $url, $params)) { | ||
117 | |||
118 | before($route); | ||
119 | \call_user_func_array($callback, $params); | ||
120 | exit; | ||
121 | } | ||
122 | } | ||
123 | } | ||
124 | |||
125 | // Parse url and find matches | ||
126 | function url_match($route_uri, $request_uri, array &$params) | ||
127 | { | ||
128 | if ($request_uri === $route_uri) return true; | ||
129 | if ($route_uri === '/' || $request_uri === '/') return false; | ||
130 | |||
131 | $route_uri = trim($route_uri, '/'); | ||
132 | $request_uri = trim($request_uri, '/'); | ||
133 | |||
134 | $route_items = explode('/', $route_uri); | ||
135 | $request_items = explode('/', $request_uri); | ||
136 | $nb_route_items = count($route_items); | ||
137 | |||
138 | if ($nb_route_items === count($request_items)) { | ||
139 | |||
140 | for ($i = 0; $i < $nb_route_items; ++$i) { | ||
141 | |||
142 | if ($route_items[$i][0] === ':') { | ||
143 | |||
144 | $params[substr($route_items[$i], 1)] = $request_items[$i]; | ||
145 | } | ||
146 | else if ($route_items[$i] !== $request_items[$i]) { | ||
147 | |||
148 | $params = array(); | ||
149 | return false; | ||
150 | } | ||
151 | } | ||
152 | |||
153 | return true; | ||
154 | } | ||
155 | |||
156 | return false; | ||
157 | } | ||