1
- <?php
1
+ <?php declare (strict_types= 1 );
2
2
3
3
namespace PhpSlides \Traits \Resources ;
4
4
@@ -19,18 +19,16 @@ trait ApiResources
19
19
20
20
protected static ?array $ apiMiddleware = null ;
21
21
22
- protected function __route (): void
22
+ protected function __route (): void
23
23
{
24
24
$ match = new MapRoute ();
25
25
self ::$ map_info = $ match ->match (
26
- self ::$ route ['r_method ' ] ?? 'dynamic ' ,
27
- self ::$ route ['url ' ]
26
+ self ::$ route ['r_method ' ] ?? 'dynamic ' ,
27
+ self ::$ route ['url ' ] ?? ''
28
28
);
29
29
30
- if (self ::$ map_info )
31
- {
32
- if (self ::$ apiMiddleware !== null )
33
- {
30
+ if (self ::$ map_info ) {
31
+ if (self ::$ apiMiddleware !== null ) {
34
32
$ this ->__api_middleware ();
35
33
}
36
34
@@ -39,41 +37,37 @@ protected function __route (): void
39
37
}
40
38
}
41
39
42
- protected function __routeSelection (?Request $ request = null )
40
+ protected function __routeSelection (?Request $ request = null )
43
41
{
44
42
$ info = self ::$ map_info ;
45
43
$ route = self ::$ route ?? self ::$ apiMap ;
46
44
47
45
$ method = $ _SERVER ['REQUEST_METHOD ' ];
48
- $ controller = $ route ['controller ' ];
46
+ $ controller = $ route ['controller ' ] ?? '' ;
49
47
50
- if (!class_exists ($ controller ))
51
- {
48
+ if (!class_exists ($ controller )) {
52
49
throw new Exception (
53
- "Api controller class ` $ controller` does not exist. "
50
+ "Api controller class ` $ controller` does not exist. "
54
51
);
55
52
}
56
53
$ params = $ info ['params ' ] ?? null ;
57
54
58
- if (!class_exists ($ controller ))
59
- {
55
+ if (!class_exists ($ controller )) {
60
56
throw new Exception (
61
- "Api controller class does not exist: ` $ controller` "
57
+ "Api controller class does not exist: ` $ controller` "
62
58
);
63
59
}
64
60
$ cc = new $ controller ();
65
61
66
62
$ r_method = '' ;
67
63
$ method = strtoupper ($ _SERVER ['REQUEST_METHOD ' ]);
68
64
69
- if (isset ($ route ['c_method ' ]))
70
- {
65
+ if (isset ($ route ['c_method ' ])) {
71
66
$ r_method = $ route ['c_method ' ];
72
67
goto EXECUTE ;
73
68
}
74
69
75
- switch ($ method )
76
- {
70
+ switch ($ method ) {
77
71
case 'GET ' :
78
72
global $ r_method ;
79
73
$ r_method = $ params === null ? 'index ' : 'show ' ;
@@ -96,12 +90,9 @@ protected function __routeSelection (?Request $request = null)
96
90
break ;
97
91
98
92
default :
99
- if (method_exists ($ cc , '__default ' ))
100
- {
93
+ if (method_exists ($ cc , '__default ' )) {
101
94
$ r_method = '__default ' ;
102
- }
103
- else
104
- {
95
+ } else {
105
96
http_response_code (405 );
106
97
self ::log ();
107
98
exit ('Request method not allowed. ' );
@@ -110,15 +101,12 @@ protected function __routeSelection (?Request $request = null)
110
101
}
111
102
112
103
EXECUTE :
113
- if ($ cc instanceof ApiController)
114
- {
115
- if ($ request === null )
116
- {
104
+ if ($ cc instanceof ApiController) {
105
+ if ($ request === null ) {
117
106
$ request = new Request ($ params );
118
107
}
119
108
120
- if (method_exists ($ cc , $ r_method ))
121
- {
109
+ if (method_exists ($ cc , $ r_method )) {
122
110
$ response = $ cc ->$ r_method ($ request );
123
111
}
124
112
@@ -127,64 +115,53 @@ protected function __routeSelection (?Request $request = null)
127
115
128
116
self ::log ();
129
117
return $ response ;
130
- }
131
- else
132
- {
118
+ } else {
133
119
throw new Exception (
134
- 'Api controller class must implements `ApiController` '
120
+ 'Api controller class must implements `ApiController` '
135
121
);
136
122
}
137
123
}
138
124
139
- protected function __api_middleware (): void
125
+ protected function __api_middleware (): void
140
126
{
141
127
$ middleware = self ::$ apiMiddleware ?? [];
142
128
$ response = '' ;
143
129
144
- $ params = self ::$ map_info ['params ' ];
130
+ $ params = self ::$ map_info ['params ' ] ?? null ;
145
131
$ request = new Request ($ params );
146
132
147
- for ($ i = 0 ; $ i < count ((array ) $ middleware ); $ i ++)
148
- {
133
+ for ($ i = 0 ; $ i < count ((array ) $ middleware ); $ i ++) {
149
134
$ middlewares = (new FileLoader ())
150
- ->load (__DIR__ . '/../../Config/middleware.php ' )
151
- ->getLoad ();
135
+ ->load (__DIR__ . '/../../Config/middleware.php ' )
136
+ ->getLoad ();
152
137
153
- if (array_key_exists ($ middleware [$ i ], $ middlewares ))
154
- {
138
+ if (array_key_exists ($ middleware [$ i ], $ middlewares )) {
155
139
$ middleware = $ middlewares [$ middleware [$ i ]];
156
- }
157
- else
158
- {
140
+ } else {
159
141
self ::log ();
160
142
throw new Exception (
161
- 'No Registered Middleware as ` ' . $ middleware [$ i ] . '` '
143
+ 'No Registered Middleware as ` ' . $ middleware [$ i ] . '` '
162
144
);
163
145
}
164
146
165
- if (!class_exists ($ middleware ))
166
- {
147
+ if (!class_exists ($ middleware )) {
167
148
self ::log ();
168
149
throw new Exception (
169
- "Middleware class does not exist: ` {$ middleware }` "
150
+ "Middleware class does not exist: ` {$ middleware }` "
170
151
);
171
152
}
172
153
$ mw = new $ middleware ();
173
154
174
- if ($ mw instanceof MiddlewareInterface)
175
- {
176
- $ next = function (Request $ request )
177
- {
155
+ if ($ mw instanceof MiddlewareInterface) {
156
+ $ next = function (Request $ request ) {
178
157
return $ this ->__routeSelection ($ request );
179
158
};
180
159
181
160
$ response = $ mw ->handle ($ request , $ next );
182
- }
183
- else
184
- {
161
+ } else {
185
162
self ::log ();
186
163
throw new Exception (
187
- 'Middleware class must implements `MiddlewareInterface` '
164
+ 'Middleware class must implements `MiddlewareInterface` '
188
165
);
189
166
}
190
167
}
@@ -194,31 +171,28 @@ protected function __api_middleware (): void
194
171
exit ();
195
172
}
196
173
197
- protected function __api_map (?Request $ request = null ): void
174
+ protected function __api_map (?Request $ request = null ): void
198
175
{
199
176
$ map = self ::$ apiMap ;
200
- $ base_url = $ map ['base_url ' ];
201
- $ controller = $ map ['controller ' ];
177
+ $ base_url = $ map ['base_url ' ] ?? '' ;
178
+ $ controller = $ map ['controller ' ] ?? '' ;
202
179
203
- foreach ($ map as $ route => $ method )
204
- {
180
+ foreach ($ map as $ route => $ method ) {
205
181
$ r_method = $ method [0 ];
206
182
$ c_method = $ method [1 ];
207
183
$ url = $ base_url . trim ($ route , '/ ' );
208
184
209
185
self ::$ apiMap = [
210
- 'controller ' => $ controller ,
211
- 'c_method ' => trim ($ c_method , '@ ' ),
212
- 'url ' => $ base_url
186
+ 'controller ' => $ controller ,
187
+ 'c_method ' => trim ($ c_method , '@ ' ),
188
+ 'url ' => $ base_url
213
189
];
214
190
215
191
$ match = new MapRoute ();
216
192
self ::$ map_info = $ match ->match ($ r_method , $ url );
217
193
218
- if (self ::$ map_info )
219
- {
220
- if (self ::$ apiMiddleware !== null )
221
- {
194
+ if (self ::$ map_info ) {
195
+ if (self ::$ apiMiddleware !== null ) {
222
196
$ this ->__api_middleware ();
223
197
}
224
198
0 commit comments