1
1
<?php namespace RestExtension ;
2
2
3
3
use CodeIgniter \Database \BaseConnection ;
4
+ use CodeIgniter \Database \Exceptions \DatabaseException ;
4
5
use CodeIgniter \Events \Events ;
5
6
use Config \Database ;
6
7
use Config \OrmExtension ;
@@ -64,9 +65,9 @@ public static function preSystem() {
64
65
/*
65
66
* Append RestExtension Entities and Models to OrmExtension namespaces
66
67
*/
67
- if (!is_array (OrmExtension::$ modelNamespace ))
68
+ if (!is_array (OrmExtension::$ modelNamespace ))
68
69
OrmExtension::$ modelNamespace = [OrmExtension::$ modelNamespace ];
69
- if (!is_array (OrmExtension::$ entityNamespace ))
70
+ if (!is_array (OrmExtension::$ entityNamespace ))
70
71
OrmExtension::$ entityNamespace = [OrmExtension::$ entityNamespace ];
71
72
OrmExtension::$ modelNamespace [] = 'RestExtension\Models \\' ;
72
73
OrmExtension::$ entityNamespace [] = 'RestExtension\Entities \\' ;
@@ -79,7 +80,7 @@ public static function preSystem() {
79
80
/*
80
81
* Api Routing
81
82
*/
82
- if (self ::$ config ) {
83
+ if (self ::$ config ) {
83
84
84
85
/*
85
86
* Setup Database connection
@@ -88,9 +89,8 @@ public static function preSystem() {
88
89
89
90
$ routes = Services::routes (true );
90
91
91
- if (self ::$ config ->enableApiRouting && self ::$ database ->tableExists ('api_routes ' )) {
92
-
93
- try {
92
+ try {
93
+ if (self ::$ config ->enableApiRouting && self ::$ database ->tableExists ('api_routes ' )) {
94
94
/** @var ApiRoute $apiRoutes */
95
95
$ apiRoutes = (new ApiRouteModel ())->find ();
96
96
@@ -103,21 +103,22 @@ public static function preSystem() {
103
103
$ routes ->{$ route ->method }($ route ->from , $ route ->to );
104
104
}
105
105
}
106
- } catch (\mysqli_sql_exception $ e ) {
107
-
108
106
}
107
+ } catch (DatabaseException $ e ) {
108
+
109
+ } catch (\mysqli_sql_exception $ e ) {
109
110
110
111
}
111
112
112
113
/*
113
114
* Export TypeScript Models
114
115
*/
115
- if (isset (self ::$ config ->typescriptModelExporterRoute ) && self ::$ config ->typescriptModelExporterRoute ) {
116
+ if (isset (self ::$ config ->typescriptModelExporterRoute ) && self ::$ config ->typescriptModelExporterRoute ) {
116
117
$ routes ->get (self ::$ config ->typescriptModelExporterRoute , function ($ debug = false ) {
117
118
$ parser = ModelParser::run ();
118
119
$ parser ->generateTypeScript ($ debug );
119
120
120
- if ($ debug ) return ;
121
+ if ($ debug ) return ;
121
122
122
123
// Zip models folder
123
124
shell_exec ('cd " ' . WRITEPATH . 'tmp/" && zip -r models.zip models ' );
@@ -137,7 +138,7 @@ public static function preSystem() {
137
138
/*
138
139
* Export TypeScript API Class
139
140
*/
140
- if (isset (self ::$ config ->typescriptAPIExporterRoute ) && self ::$ config ->typescriptAPIExporterRoute ) {
141
+ if (isset (self ::$ config ->typescriptAPIExporterRoute ) && self ::$ config ->typescriptAPIExporterRoute ) {
141
142
$ routes ->get (self ::$ config ->typescriptAPIExporterRoute , function ($ debug = 0 ) {
142
143
$ parser = ApiParser::run ();
143
144
$ parser ->generateTypeScript ($ debug );
@@ -159,7 +160,7 @@ public static function preSystem() {
159
160
/*
160
161
* Export Vue API Class
161
162
*/
162
- if (isset (self ::$ config ->vueAPIExporterRoute ) && self ::$ config ->vueAPIExporterRoute ) {
163
+ if (isset (self ::$ config ->vueAPIExporterRoute ) && self ::$ config ->vueAPIExporterRoute ) {
163
164
$ routes ->get (self ::$ config ->vueAPIExporterRoute , function ($ debug = 0 ) {
164
165
$ parser = ApiParser::run ();
165
166
$ parser ->generateVue ($ debug );
@@ -181,12 +182,12 @@ public static function preSystem() {
181
182
/*
182
183
* Export Xamarin Models
183
184
*/
184
- if (isset (self ::$ config ->xamarinModelExporterRoute ) && self ::$ config ->xamarinModelExporterRoute ) {
185
+ if (isset (self ::$ config ->xamarinModelExporterRoute ) && self ::$ config ->xamarinModelExporterRoute ) {
185
186
$ routes ->get (self ::$ config ->xamarinModelExporterRoute , function ($ debug = false ) {
186
187
$ parser = ModelParser::run ();
187
188
$ parser ->generateXamarin ($ debug );
188
189
189
- if ($ debug ) return ;
190
+ if ($ debug ) return ;
190
191
191
192
// Zip models folder
192
193
shell_exec ('cd " ' . WRITEPATH . 'tmp/xamarin/" && zip -r models.zip models ' );
@@ -206,7 +207,7 @@ public static function preSystem() {
206
207
/*
207
208
* Export Xamarin API Class
208
209
*/
209
- if (isset (self ::$ config ->xamarinAPIExporterRoute ) && self ::$ config ->xamarinAPIExporterRoute
210
+ if (isset (self ::$ config ->xamarinAPIExporterRoute ) && self ::$ config ->xamarinAPIExporterRoute
210
211
&& isset (self ::$ config ->xamarinAPINamespace ) && self ::$ config ->xamarinAPINamespace ) {
211
212
$ routes ->get (self ::$ config ->xamarinAPIExporterRoute , function ($ debug = false ) {
212
213
$ parser = ApiParser::run ();
@@ -239,40 +240,40 @@ public static function preSystem() {
239
240
* @throws \Exception
240
241
*/
241
242
public static function postControllerConstructor () {
242
- if (self ::$ config ) {
243
+ if (self ::$ config ) {
243
244
244
245
$ restRequest = RestRequest::getInstance ();
245
246
$ request = Services::request ();
246
247
247
248
/*
248
249
* CLI is trusted
249
250
*/
250
- if ($ request ->isCLI ()) {
251
+ if ($ request ->isCLI ()) {
251
252
return ;
252
253
}
253
254
254
- if (self ::$ config ->enableApiRouting && self ::$ database ->tableExists ('api_routes ' )) {
255
+ if (self ::$ config ->enableApiRouting && self ::$ database ->tableExists ('api_routes ' )) {
255
256
256
257
/*
257
258
* Search for api route based on CI's matched route
258
259
*/
259
260
$ route = Services::router ()->getMatchedRoute ();
260
261
if (!$ route ) {
261
262
$ url = Services::request ()->uri ;
262
- throw new \Exception ("RestExtension: Route ( $ url) not found. Api Routes have to be store in the " .
263
+ throw new \Exception ("RestExtension: Route ( $ url) not found. Api Routes have to be store in the " .
263
264
"database to check against scopes. " );
264
265
}
265
266
$ routeFrom = $ route [0 ];
266
267
/** @var ApiRoute $apiRoute */
267
268
$ apiRoute = (new ApiRouteModel ())
268
269
->groupStart ()
269
270
->where ('from ' , $ routeFrom )
270
- ->orWhere ('from ' , '/ ' . $ routeFrom )
271
+ ->orWhere ('from ' , '/ ' . $ routeFrom )
271
272
->groupEnd ()
272
273
->where ('method ' , $ request ->getMethod ())
273
274
->find ();
274
- if (!$ apiRoute ->exists () && !$ request ->isCLI ()) {
275
- throw new \Exception ("RestExtension: Route ( $ routeFrom) not found. Api Routes have to be store in the " .
275
+ if (!$ apiRoute ->exists () && !$ request ->isCLI ()) {
276
+ throw new \Exception ("RestExtension: Route ( $ routeFrom) not found. Api Routes have to be store in the " .
276
277
"database to check against scopes. " );
277
278
}
278
279
$ restRequest ->apiRoute = $ apiRoute ;
@@ -285,9 +286,9 @@ public static function postControllerConstructor() {
285
286
/*
286
287
* Public API route can skip authorization
287
288
*/
288
- if (!$ apiRoute ->is_public ) {
289
+ if (!$ apiRoute ->is_public ) {
289
290
290
- if (!isset ($ authResponse ->authorized ) || $ authResponse ->authorized == false ) {
291
+ if (!isset ($ authResponse ->authorized ) || $ authResponse ->authorized == false ) {
291
292
292
293
/*
293
294
* Unauthorized!
@@ -296,7 +297,7 @@ public static function postControllerConstructor() {
296
297
}
297
298
}
298
299
299
- if ($ authResponse && isset ($ authResponse ->client_id )) {
300
+ if ($ authResponse && isset ($ authResponse ->client_id )) {
300
301
301
302
/*
302
303
* Authorized, go on
@@ -311,13 +312,13 @@ public static function postControllerConstructor() {
311
312
/*
312
313
* API Rate Limit
313
314
*/
314
- if (self ::$ config ->enableRateLimit && self ::$ database ->tableExists ('api_access_logs ' )) {
315
- if (self ::$ config ->defaultRateLimit > 0 ) {
315
+ if (self ::$ config ->enableRateLimit && self ::$ database ->tableExists ('api_access_logs ' )) {
316
+ if (self ::$ config ->defaultRateLimit > 0 ) {
316
317
$ lastHour = (new ApiAccessLogModel ())
317
318
->where ('client_id ' , $ authResponse ->client_id )
318
319
->where ('date > ' , date ('Y-m-d H:i:s ' , strtotime ('-1 hour ' )))
319
320
->countAllResults ();
320
- if ($ lastHour >= self ::$ config ->defaultRateLimit ) {
321
+ if ($ lastHour >= self ::$ config ->defaultRateLimit ) {
321
322
322
323
/*
323
324
* Unauthorized!
@@ -331,14 +332,14 @@ public static function postControllerConstructor() {
331
332
/*
332
333
* API Usage Reporting
333
334
*/
334
- if (self ::$ config ->enableUsageReporting && self ::$ database ->tableExists ('api_usage_reports ' )) {
335
+ if (self ::$ config ->enableUsageReporting && self ::$ database ->tableExists ('api_usage_reports ' )) {
335
336
336
337
/** @var ApiUsageReport $usageReport */
337
338
$ usageReport = (new ApiUsageReportModel ())
338
339
->where ('client_id ' , $ authResponse ->client_id )
339
340
->where ('date ' , date ('Y-m-d ' ))
340
341
->find ();
341
- if (!$ usageReport ->exists ()) {
342
+ if (!$ usageReport ->exists ()) {
342
343
$ usageReport ->client_id = $ authResponse ->client_id ;
343
344
$ usageReport ->date = date ('Y-m-d ' );
344
345
$ usageReport ->usage = 0 ;
@@ -353,16 +354,16 @@ public static function postControllerConstructor() {
353
354
/*
354
355
* API Access Log
355
356
*/
356
- if (self ::$ config ->enableAccessLog && self ::$ database ->tableExists ('api_access_logs ' )) {
357
+ if (self ::$ config ->enableAccessLog && self ::$ database ->tableExists ('api_access_logs ' )) {
357
358
358
359
$ apiAccessLog = new ApiAccessLog ();
359
- if ($ restRequest ->userId ) {
360
+ if ($ restRequest ->userId ) {
360
361
$ apiAccessLog ->user_id = $ restRequest ->userId ;
361
362
}
362
- if ($ restRequest ->clientId ) {
363
+ if ($ restRequest ->clientId ) {
363
364
$ apiAccessLog ->client_id = $ restRequest ->clientId ;
364
365
}
365
- if ($ restRequest ->apiRoute ) {
366
+ if ($ restRequest ->apiRoute ) {
366
367
$ apiAccessLog ->api_route_id = $ restRequest ->apiRoute ->id ;
367
368
}
368
369
$ apiAccessLog ->access_token = $ restRequest ->getAccessToken ();
@@ -382,19 +383,19 @@ public static function postSystem() {
382
383
/*
383
384
* CLI is trusted
384
385
*/
385
- if (Services::request ()->isCLI ())
386
+ if (Services::request ()->isCLI ())
386
387
return ;
387
388
388
389
/*
389
390
* Stop timer for benchmarking
390
391
*/
391
392
timer ('RestExtension::timer ' );
392
393
393
- if (self ::$ config ) {
394
+ if (self ::$ config ) {
394
395
395
- if (self ::$ config ->enableAccessLog && self ::$ database ->tableExists ('api_access_logs ' )) {
396
+ if (self ::$ config ->enableAccessLog && self ::$ database ->tableExists ('api_access_logs ' )) {
396
397
$ apiAccessLog = RestRequest::getInstance ()->apiAccessLog ;
397
- if ($ apiAccessLog ) {
398
+ if ($ apiAccessLog ) {
398
399
$ apiAccessLog ->milliseconds = timer ()->getElapsedTime ('RestExtension::timer ' ) * 1000 ;
399
400
$ apiAccessLog ->save ();
400
401
}
@@ -404,17 +405,17 @@ public static function postSystem() {
404
405
}
405
406
406
407
public static function exceptionHandler (Throwable $ exception ) {
407
- if (self ::$ config ) {
408
+ if (self ::$ config ) {
408
409
409
410
$ request = Services::request ();
410
411
$ restRequest = RestRequest::getInstance ();
411
412
412
- if (self ::$ config ->enableErrorLog && self ::$ database ->tableExists ('api_error_logs ' )) {
413
+ if (self ::$ config ->enableErrorLog && self ::$ database ->tableExists ('api_error_logs ' )) {
413
414
414
415
$ apiErrorLog = new ApiErrorLog ();
415
- if ($ restRequest ->userId ) $ apiErrorLog ->user_id = $ restRequest ->userId ;
416
- if ($ restRequest ->clientId ) $ apiErrorLog ->client_id = $ restRequest ->clientId ;
417
- if ($ restRequest ->apiRoute ) $ apiErrorLog ->api_route_id = $ restRequest ->apiRoute ->id ;
416
+ if ($ restRequest ->userId ) $ apiErrorLog ->user_id = $ restRequest ->userId ;
417
+ if ($ restRequest ->clientId ) $ apiErrorLog ->client_id = $ restRequest ->clientId ;
418
+ if ($ restRequest ->apiRoute ) $ apiErrorLog ->api_route_id = $ restRequest ->apiRoute ->id ;
418
419
$ apiErrorLog ->access_token = $ restRequest ->getAccessToken ();
419
420
$ apiErrorLog ->uri = current_url ();
420
421
$ apiErrorLog ->date = date ('Y-m-d H:i:s ' );
@@ -429,13 +430,13 @@ public static function exceptionHandler(Throwable $exception) {
429
430
$ apiErrorLog ->save ();
430
431
}
431
432
432
- if ($ exception instanceof UnauthorizedException || $ exception instanceof RateLimitExceededException) {
433
+ if ($ exception instanceof UnauthorizedException || $ exception instanceof RateLimitExceededException) {
433
434
434
- if (self ::$ config ->enableBlockedLog && self ::$ database ->tableExists ('api_blocked_logs ' )) {
435
+ if (self ::$ config ->enableBlockedLog && self ::$ database ->tableExists ('api_blocked_logs ' )) {
435
436
$ apiBlockedLog = new ApiBlockedLog ();
436
- if ($ restRequest ->userId ) $ apiBlockedLog ->user_id = $ restRequest ->userId ;
437
- if ($ restRequest ->clientId ) $ apiBlockedLog ->client_id = $ restRequest ->clientId ;
438
- if ($ restRequest ->apiRoute ) $ apiBlockedLog ->api_route_id = $ restRequest ->apiRoute ->id ;
437
+ if ($ restRequest ->userId ) $ apiBlockedLog ->user_id = $ restRequest ->userId ;
438
+ if ($ restRequest ->clientId ) $ apiBlockedLog ->client_id = $ restRequest ->clientId ;
439
+ if ($ restRequest ->apiRoute ) $ apiBlockedLog ->api_route_id = $ restRequest ->apiRoute ->id ;
439
440
$ apiBlockedLog ->access_token = $ restRequest ->getAccessToken ();
440
441
$ apiBlockedLog ->uri = current_url ();
441
442
$ apiBlockedLog ->date = date ('Y-m-d H:i:s ' );
@@ -446,16 +447,16 @@ public static function exceptionHandler(Throwable $exception) {
446
447
447
448
}
448
449
449
- if ($ exception instanceof RestException) {
450
+ if ($ exception instanceof RestException) {
450
451
451
452
$ response = Services::response ();
452
453
$ response ->setStatusCode ($ exception ->getCode ());
453
454
$ response ->setJSON ([
454
- 'status ' => 'ERROR ' ,
455
- 'code ' => $ exception ->getCode (),
456
- 'error ' => get_class ($ exception ),
457
- 'reason ' => $ exception ->getMessage (),
458
- 'debug ' => Data::getDebugger ()
455
+ 'status ' => 'ERROR ' ,
456
+ 'code ' => $ exception ->getCode (),
457
+ 'error ' => get_class ($ exception ),
458
+ 'reason ' => $ exception ->getMessage (),
459
+ 'debug ' => Data::getDebugger ()
459
460
]);
460
461
$ response ->send ();
461
462
return ;
0 commit comments