-
Notifications
You must be signed in to change notification settings - Fork 187
/
router.php
84 lines (72 loc) · 3.19 KB
/
router.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
<?php
use API\Commons\Error;
use API\Commons\Exceptions\AuthenticationError;
use API\Commons\Exceptions\AuthorizationError;
use API\Commons\Exceptions\ExternalServiceException;
use API\Commons\Exceptions\NotFoundException;
use API\Commons\Exceptions\UnprocessableException;
use API\Commons\Exceptions\ValidationError;
use Exceptions\ValidationError as Model_ValidationError;
use Klein\Klein;
require_once './inc/Bootstrap.php';
Bootstrap::start();
$klein = new Klein();
/**
* @param string $path
* @param string $method
* @param array $callback
*
* @return void
*/
function route( string $path, string $method, array $callback ) {
global $klein;
$klein->respond( $method, $path, function () use ( $callback ) {
$reflect = new ReflectionClass( $callback[ 0 ] );
$instance = $reflect->newInstanceArgs( func_get_args() );
$instance->respond( $callback[ 1 ] );
} );
}
$klein->onError( function ( Klein $klein, $err_msg, $err_type, Throwable $exception ) {
$klein->response()->noCache();
Log::$fileName = 'fatal_errors.txt';
try {
throw $exception;
} catch ( ValidationError|InvalidArgumentException|Model_ValidationError $e ) {
$klein->response()->code( 400 );
$klein->response()->json( ( new Error( [ $e ] ) )->render() );
} catch ( AuthenticationError $e ) {
$klein->response()->code( 401 );
$klein->response()->json( ( new Error( [ $e ] ) )->render() );
} catch ( AuthorizationError|DomainException $e ) {
$klein->response()->code( 403 );
$klein->response()->json( ( new Error( [ $e ] ) )->render() );
} catch ( NotFoundException|Exceptions\NotFoundException $e ) {
Log::doJsonLog( 'Record Not found error for URI: ' . $_SERVER[ 'REQUEST_URI' ] );
$klein->response()->code( 404 );
$klein->response()->json( ( new Error( [ $e ] ) )->render() );
} catch ( UnprocessableException $e ) {
$klein->response()->code( 422 );
$klein->response()->json( ( new Error( [ $e ] ) )->render() );
} catch ( ExternalServiceException $e ) {
$klein->response()->code( 503 );
$klein->response()->json( ( new Error( [ $e ] ) )->render() );
} catch ( PDOException $e ) {
$klein->response()->code( 503 );
// $klein->response()->json( ( new Error( [ $e ] ) )->render() );
// Utils::sendErrMailReport( $exception->getMessage() . " " . $exception->getTraceAsString(), 'Generic error' );
Log::doJsonLog( [ "error" => $exception->getMessage(), "trace" => $exception->getTrace() ] );
} catch ( Throwable $e ) {
$klein->response()->code( 500 );
// Utils::sendErrMailReport( $exception->getMessage() . " " . $exception->getTraceAsString(), 'Generic error' );
Log::doJsonLog( [ "error" => $exception->getMessage(), "trace" => $exception->getTrace() ] );
$klein->response()->json( ( new Error( [ $e ] ) )->render() );
}
} );
require './lib/Routes/api_v1_routes.php';
require './lib/Routes/api_v2_routes.php';
require './lib/Routes/api_v3_routes.php';
require './lib/Routes/gdrive_routes.php';
require './lib/Routes/oauth_routes.php';
require './lib/Routes/utils_routes.php';
Features::loadRoutes( $klein );
$klein->dispatch();