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
|
<?php
namespace PicoFarad\Router;
// Load controllers: bootstrap('controllers', 'controller1', 'controller2')
function bootstrap()
{
$files = \func_get_args();
$base_path = array_shift($files);
foreach ($files as $file) {
require $base_path.'/'.$file.'.php';
}
}
// Execute a callback before each action
function before($value = null)
{
static $before_callback = null;
if (is_callable($value)) {
$before_callback = $value;
}
else if (is_callable($before_callback)) {
$before_callback($value);
}
}
// Execute a callback before a specific action
function before_action($name, $value = null)
{
static $callbacks = array();
if (is_callable($value)) {
$callbacks[$name] = $value;
}
else if (isset($callbacks[$name]) && is_callable($callbacks[$name])) {
$callbacks[$name]($value);
}
}
// Execute an action
function action($name, \Closure $callback)
{
$handler = isset($_GET['action']) ? $_GET['action'] : 'default';
if ($handler === $name) {
before($name);
before_action($name);
$callback();
}
}
// Execute an action only for POST requests
function post_action($name, \Closure $callback)
{
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
action($name, $callback);
}
}
// Execute an action only for GET requests
function get_action($name, \Closure $callback)
{
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
action($name, $callback);
}
}
// Run when no action have been executed before
function notfound(\Closure $callback)
{
before('notfound');
before_action('notfound');
$callback();
}
// Match a request like this one: GET /myhandler
function get($url, \Closure $callback)
{
find_route('GET', $url, $callback);
}
// Match a request like this one: POST /myhandler
function post($url, \Closure $callback)
{
find_route('POST', $url, $callback);
}
// Match a request like this one: PUT /myhandler
function put($url, \Closure $callback)
{
find_route('PUT', $url, $callback);
}
// Match a request like this one: DELETE /myhandler
function delete($url, \Closure $callback)
{
find_route('DELETE', $url, $callback);
}
// Define which callback to execute according to the URL and the HTTP verb
function find_route($method, $route, \Closure $callback)
{
if ($_SERVER['REQUEST_METHOD'] === $method) {
if (! empty($_SERVER['QUERY_STRING'])) {
$url = substr($_SERVER['REQUEST_URI'], 0, -(strlen($_SERVER['QUERY_STRING']) + 1));
}
else {
$url = $_SERVER['REQUEST_URI'];
}
$params = array();
if (url_match($route, $url, $params)) {
before($route);
\call_user_func_array($callback, $params);
exit;
}
}
}
// Parse url and find matches
function url_match($route_uri, $request_uri, array &$params)
{
if ($request_uri === $route_uri) return true;
if ($route_uri === '/' || $request_uri === '/') return false;
$route_uri = trim($route_uri, '/');
$request_uri = trim($request_uri, '/');
$route_items = explode('/', $route_uri);
$request_items = explode('/', $request_uri);
$nb_route_items = count($route_items);
if ($nb_route_items === count($request_items)) {
for ($i = 0; $i < $nb_route_items; ++$i) {
if ($route_items[$i][0] === ':') {
$params[substr($route_items[$i], 1)] = $request_items[$i];
}
else if ($route_items[$i] !== $request_items[$i]) {
$params = array();
return false;
}
}
return true;
}
return false;
}
|