|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace voku\slim; |
|
6
|
|
|
|
|
7
|
|
|
use Slim\Middleware; |
|
8
|
|
|
use Slim\Slim; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* JsonApiMiddleware - Middleware that sets a bunch of static routes for easy bootstrapping of json API's |
|
12
|
|
|
*/ |
|
13
|
|
|
class JsonApiMiddleware extends Middleware |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Sets a bunch of static API calls |
|
17
|
|
|
* |
|
18
|
|
|
* @param string $slimNameInstance |
|
19
|
|
|
*/ |
|
20
|
1 |
|
public function __construct($slimNameInstance = 'default') |
|
21
|
|
|
{ |
|
22
|
1 |
|
$app = Slim::getInstance($slimNameInstance); |
|
23
|
|
|
|
|
24
|
|
|
// Mirrors the API request |
|
25
|
1 |
|
$app->get( |
|
26
|
1 |
|
'/return', function () use ($app) { |
|
27
|
|
|
|
|
28
|
|
|
$app->render( |
|
29
|
|
|
200, |
|
30
|
|
|
[ |
|
31
|
|
|
'method' => $app->request()->getMethod(), |
|
32
|
|
|
'name' => $app->request()->get('name'), |
|
33
|
|
|
'headers' => $app->request()->headers(), |
|
34
|
|
|
'params' => $app->request()->params(), |
|
35
|
|
|
] |
|
36
|
|
|
); |
|
37
|
1 |
|
} |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
// Generic error handler |
|
41
|
1 |
|
$app->error( |
|
42
|
1 |
|
function (\Exception $e) use ($app) { |
|
43
|
|
|
|
|
44
|
|
|
$errorCode = $e->getCode() ?: 500; |
|
45
|
|
|
|
|
46
|
|
|
// Log error with the same message |
|
47
|
|
|
$message = JsonApiMiddleware::_errorType($e->getCode()) . ': ' . $e->getMessage(); |
|
48
|
|
|
$app->getLog()->error($message . ' in ' . $e->getFile() . ' at line ' . $e->getLine()); |
|
49
|
|
|
|
|
50
|
|
|
$app->render( |
|
51
|
|
|
$errorCode, |
|
52
|
|
|
[ |
|
53
|
|
|
'msg' => $message, |
|
54
|
|
|
] |
|
55
|
|
|
); |
|
56
|
1 |
|
} |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
// Not found handler (invalid routes, invalid method types) |
|
60
|
1 |
|
$app->notFound( |
|
61
|
1 |
|
function () use ($app) { |
|
62
|
|
|
$app->render( |
|
63
|
|
|
404, |
|
64
|
|
|
[ |
|
65
|
|
|
'msg' => 'Invalid route', |
|
66
|
|
|
] |
|
67
|
|
|
); |
|
68
|
1 |
|
} |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
// Handle Empty response body |
|
72
|
1 |
|
$app->hook( |
|
73
|
1 |
|
'slim.after.router', function () use ($app) { |
|
74
|
|
|
|
|
75
|
|
|
// INFO: this will allow download request to flow |
|
76
|
|
|
if ($app->response()->header('Content-Type') === 'application/octet-stream') { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if ($app->response()->body() === '') { |
|
81
|
|
|
$app->render( |
|
82
|
|
|
500, |
|
83
|
|
|
[ |
|
84
|
|
|
'msg' => 'Empty response', |
|
85
|
|
|
] |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
1 |
|
} |
|
89
|
|
|
); |
|
90
|
1 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param int $type |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
protected static function _errorType($type = 1) |
|
98
|
|
|
{ |
|
99
|
|
|
switch ($type) { |
|
100
|
|
|
default: |
|
101
|
|
|
case E_ERROR: // 1 // |
|
102
|
|
|
return 'ERROR'; |
|
103
|
|
|
case E_WARNING: // 2 // |
|
104
|
|
|
return 'WARNING'; |
|
105
|
|
|
case E_PARSE: // 4 // |
|
106
|
|
|
return 'PARSE'; |
|
107
|
|
|
case E_NOTICE: // 8 // |
|
108
|
|
|
return 'NOTICE'; |
|
109
|
|
|
case E_CORE_ERROR: // 16 // |
|
110
|
|
|
return 'CORE_ERROR'; |
|
111
|
|
|
case E_CORE_WARNING: // 32 // |
|
112
|
|
|
return 'CORE_WARNING'; |
|
113
|
|
|
case E_COMPILE_ERROR: // 64 // |
|
114
|
|
|
return 'COMPILE_ERROR'; |
|
115
|
|
|
case E_COMPILE_WARNING: // 128 // |
|
116
|
|
|
return 'COMPILE_WARNING'; |
|
117
|
|
|
case E_USER_ERROR: // 256 // |
|
118
|
|
|
return 'USER_ERROR'; |
|
119
|
|
|
case E_USER_WARNING: // 512 // |
|
120
|
|
|
return 'USER_WARNING'; |
|
121
|
|
|
case E_USER_NOTICE: // 1024 // |
|
122
|
|
|
return 'USER_NOTICE'; |
|
123
|
|
|
case E_STRICT: // 2048 // |
|
124
|
|
|
return 'STRICT'; |
|
125
|
|
|
case E_RECOVERABLE_ERROR: // 4096 // |
|
126
|
|
|
return 'RECOVERABLE_ERROR'; |
|
127
|
|
|
case E_DEPRECATED: // 8192 // |
|
128
|
|
|
return 'DEPRECATED'; |
|
129
|
|
|
case E_USER_DEPRECATED: // 16384 // |
|
130
|
|
|
return 'USER_DEPRECATED'; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Call next |
|
136
|
|
|
*/ |
|
137
|
|
|
public function call() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->next->call(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|