-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.php
225 lines (182 loc) · 6.58 KB
/
index.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedParameterInspection */
use Gac\Routing\Exceptions\CallbackNotFound;
use Gac\Routing\Exceptions\RouteNotFoundException;
use Gac\Routing\Request;
use Gac\Routing\Routes;
use Gac\Routing\sample\HomeController;
use Gac\Routing\sample\InjectController;
use Gac\Routing\sample\InjectedClass;
use Gac\Routing\sample\Middleware;
#include_once "../Routes.php"; # IF YOU'RE NOT USING composer
#include_once "HomeController.php"; # IF YOU'RE NOT USING composer
include_once '../vendor/autoload.php'; # IF YOU'RE USING composer
if ( !class_exists('InjectedClass') ) {
require_once './InjectedClass.php';
}
if ( !class_exists('InjectController') ) {
require_once './InjectController.php';
}
if ( !class_exists('Middleware') ) {
require_once './Middleware.php';
}
if ( !class_exists('HomeController') ) {
require_once './HomeController.php';
}
$routes = new Routes();
try {
$routes->add('/', function (Request $request) {
echo json_encode([ 'message' => 'Hello World' ]);
});
// When using chained method calls either use `save()` or `add()` method at the end to indicate an end of a chain
// save() method can still be chained onto if needed, but add() can not
$routes
->prefix("/test")
->middleware([ 'decode_token' ])
->get("/t1", function () { })
->get("/t2", function () { })
->get("/t3", function () { })
->save(false) // by passing the false argument here, we keep all the previous shared data from the chain (previous prefix(es) and middlewares)
->prefix("/test2")
->middleware([ "verify_token" ])
->get("/t4", function () { })
->get("/t5", function () { })
->get("/t6", function () { })
->save() // by not passing the false argument here, we are removing all shared data from the previous chains (previous prefix(es) and middlewares)
->prefix("/test3")
->middleware([ "verify_token" ])
->get("/t7", function () { })
->get("/t8", function () { })
->get("/t9", function () { })
->add(); //using save or add at the end makes the chaining stop and allows for other independent routes to be added
$routes->add("/routes", function (Request $request) {
global $routes;
$request->send($routes->get_routes());
});
$routes->add('/test', function (Request $request) {
$request
->status(200, 'OK')
->send([ 'message' => 'Welcome' ]);
});
$routes->add("/home/test", [ HomeController::class, "home" ]);
$routes
->prefix('/user')
->middleware([ 'verify_token' ])
->route('/', [ HomeController::class, 'getUsers' ], Routes::GET)
->route('/', [ HomeController::class, 'addUser' ], Routes::POST)
->route('/', [ HomeController::class, 'updateUser' ], Routes::PATCH)
->route('/', [ HomeController::class, 'replaceUser' ], Routes::PUT)
->add('/test', [ HomeController::class, 'deleteUser' ], Routes::DELETE);
$routes->add('/test', function () {
}, [ Routes::PATCH, Routes::POST ]);
$routes->get("/test-get", function () {
echo "Hello from test-get";
});
$routes->post("/test-post", function () {
echo "Hello from test-post";
});
$routes->put("/test-put", function () {
echo "Hello from test-put";
});
$routes->patch("/test-patch", function () {
echo "Hello from test-patch";
});
$routes->delete("/test-delete", function () {
echo "Hello from test-delete";
});
$routes->add('/test/{int:userID}-{username}/{float:amount}/{bool:valid}', function (
Request $request,
int $userID,
string $username,
float $amount,
bool $valid
) {
echo 'Dynamic route here';
});
$routes->add(
'/test/{int:userID}-{username}/{float:amount}/{bool:valid}',
[ HomeController::class, 'test' ],
[ Routes::PUT ]
); # It works like this also
$routes
->middleware([
[ Middleware::class, 'verify_token' ],
[ Middleware::class, 'test' ],
'verify_token',
])
->add('/test-hello', function (Request $request) {
$request->send([ 'message' => 'Hello' ]);
});
$routes
->middleware([
'test_middleware',
'has_roles' => 'admin,user',
[ Middleware::class, 'test_method' ],
[ Middleware::class, 'has_role', 'Admin', 'Moderator', [ 'User', 'Bot' ] ],
])
->add('/testing', function (Request $request) {
$request->send([ 'msg' => 'testing' ]);
});
$routes->add('/', function (Request $request) {
echo "<pre>";
print_r([ $_REQUEST, $_FILES ]);
}, [ Routes::PATCH ]);
$routes->add('/', function (Request $request) {
echo "<pre>";
print_r([ $_REQUEST, $_FILES ]);
}, [ Routes::PUT ]);
//This $otherRoutes variable could be defined in a different file and included here only via require or include
$otherRoutes = new Routes();
$otherRoutes->prefix("/sample")
->get("/", function (Request $request) {
echo "hello";
})
->get("/sample1", function (Request $request) { })
->get("/sample2", function (Request $request) { })
->save();
$routes->append($otherRoutes->get_routes());
$routes->add("/headers", function (Request $request) {
$request->header("Content-type", "text/plain")
->header([ "foo" => "bar", "best" => "test" ])
->header((object) [ "X-Auth" => "Token {token-123}" ])
->status(201)
->send([ "message" => "hello" ]);
}, Routes::GET);
$routes->add(
'/demo',
[ HomeController::class, 'dependency_injection_test', [ new InjectedClass() ] ],
Routes::GET
);
$routes->add(
"/inject",
[ InjectController::class ]
);
#echo "<pre>";
#print_r($routes->get_routes());
#die(1);
$routes->get("/optional/{id?}", function (Request $request, $id = -999) {
echo "Optional route here with id: $id";
});
$routes->handle();
} catch ( RouteNotFoundException $ex ) {
$routes->request->status(404, 'Route not found')->send([ 'error' => [ 'message' => $ex->getMessage() ] ]);
} catch ( CallbackNotFound $ex ) {
$routes->request->status(404, 'Callback method not found')
->send([ 'error' => [ 'message' => $ex->getMessage() ] ]);
} catch ( Exception $ex ) {
$code = $ex->getCode() ?? 500;
$routes->request->status($code)->send([ 'error' => [ 'message' => $ex->getMessage() ] ]);
}
function test_route_function() : void {
echo json_encode([ 'message' => 'Welcome from test route' ]);
}
function verify_token() : void {
//Do something
}
function has_roles(string $allowedRoles) : void {
//Do something
}
function test_middleware() : void {
//Do something
}