6
6
use PhpSlides \Foundation \Application ;
7
7
use PhpSlides \Interface \MapInterface ;
8
8
9
-
10
9
/**
11
10
* Class MapRoute
12
11
*
@@ -34,7 +33,6 @@ class MapRoute extends Controller implements MapInterface
34
33
*/
35
34
private static array $ method ;
36
35
37
-
38
36
/**
39
37
* Matches the given HTTP method and route against the current request URI.
40
38
*
@@ -64,32 +62,25 @@ public function match(string $method, string|array $route): bool|array
64
62
* ----------------------------------------------
65
63
*/
66
64
self ::$ request_uri = strtolower (
67
- preg_replace ("/(^\/)|(\/$)/ " , '' , Application::$ request_uri ),
65
+ preg_replace ("/(^\/)|(\/$)/ " , '' , Application::$ request_uri ),
68
66
);
69
67
self ::$ request_uri = empty (self ::$ request_uri ) ? '/ ' : self ::$ request_uri ;
70
68
71
69
self ::$ route = is_array ($ route )
72
- ? $ route
73
- : strtolower (preg_replace ("/(^\/)|(\/$)/ " , '' , $ route ));
70
+ ? $ route
71
+ : strtolower (preg_replace ("/(^\/)|(\/$)/ " , '' , $ route ));
74
72
75
73
// Firstly, resolve route with pattern
76
- if (is_array (self ::$ route ))
77
- {
78
- foreach ( self :: $ route as $ value)
79
- {
80
- if ( str_starts_with ( ' pattern: ' , $ value ))
81
- {
74
+ if (is_array (self ::$ route )) {
75
+ foreach ( self :: $ route as $ value ) {
76
+ if ( str_starts_with ( $ value, ' pattern: ' )) {
77
+ if ( $ p = $ this -> pattern ()) {
78
+ return $ p ;
79
+ }
82
80
$ pattern = true ;
83
81
}
84
82
}
85
-
86
- if ($ pattern === true )
87
- {
88
- return $ this ->pattern ();
89
- }
90
- }
91
- else if (str_starts_with ('pattern: ' , self ::$ route ))
92
- {
83
+ } elseif (str_starts_with (self ::$ route , 'pattern: ' )) {
93
84
return $ this ->pattern ();
94
85
}
95
86
@@ -101,14 +92,16 @@ public function match(string $method, string|array $route): bool|array
101
92
$ paramKey = [];
102
93
103
94
// finding if there is any {?} parameter in $route
104
- if (is_string (self ::$ route ))
105
- {
95
+ if (is_string (self ::$ route )) {
106
96
preg_match_all ('/(?<={).+?(?=})/ ' , self ::$ route , $ paramMatches );
107
97
}
108
98
109
99
// if the route does not contain any param call routing();
110
- if (empty ($ paramMatches ) || is_array (self ::$ route ))
111
- {
100
+ if (
101
+ empty ($ paramMatches ) ||
102
+ empty ($ paramMatches [0 ] ?? []) ||
103
+ is_array (self ::$ route )
104
+ ) {
112
105
/**
113
106
* ------------------------------------------------------
114
107
* | Check if $callback is a callable function
@@ -120,8 +113,7 @@ public function match(string $method, string|array $route): bool|array
120
113
}
121
114
122
115
// setting parameters names
123
- foreach ($ paramMatches [0 ] as $ key )
124
- {
116
+ foreach ($ paramMatches [0 ] as $ key ) {
125
117
$ paramKey [] = $ key ;
126
118
}
127
119
@@ -132,10 +124,8 @@ public function match(string $method, string|array $route): bool|array
132
124
$ indexNum = [];
133
125
134
126
// storing index number, where {?} parameter is required with the help of regex
135
- foreach ($ uri as $ index => $ param )
136
- {
137
- if (preg_match ('/{.*}/ ' , $ param ))
138
- {
127
+ foreach ($ uri as $ index => $ param ) {
128
+ if (preg_match ('/{.*}/ ' , $ param )) {
139
129
$ indexNum [] = $ index ;
140
130
}
141
131
}
@@ -146,22 +136,19 @@ public function match(string $method, string|array $route): bool|array
146
136
* ----------------------------------------------------------------------------------
147
137
*/
148
138
$ reqUri = explode ('/ ' , self ::$ request_uri );
149
-
150
139
/**
151
140
* ----------------------------------------------------------------------------------
152
141
* | Running for each loop to set the exact index number with reg expression this will help in matching route
153
142
* ----------------------------------------------------------------------------------
154
143
*/
155
- foreach ($ indexNum as $ key => $ index )
156
- {
144
+ foreach ($ indexNum as $ key => $ index ) {
157
145
/**
158
146
* --------------------------------------------------------------------------------
159
147
* | In case if req uri with param index is empty then return because URL is not valid for this route
160
148
* --------------------------------------------------------------------------------
161
149
*/
162
150
163
- if (empty ($ reqUri [$ index ]))
164
- {
151
+ if (empty ($ reqUri [$ index ])) {
165
152
return false ;
166
153
}
167
154
@@ -185,30 +172,27 @@ public function match(string $method, string|array $route): bool|array
185
172
$ reqUri = str_replace ('/ ' , '\\/ ' , $ reqUri );
186
173
187
174
// now matching route with regex
188
- if (preg_match ("/ $ reqUri/ " , self ::$ route ))
189
- {
175
+ if (preg_match ("/ $ reqUri/ " , self ::$ route )) {
190
176
// checks if the requested method is of the given route
191
177
if (
192
- !in_array ($ _SERVER ['REQUEST_METHOD ' ], self ::$ method ) &&
193
- !in_array ('* ' , self ::$ method )
194
- )
195
- {
178
+ !in_array ($ _SERVER ['REQUEST_METHOD ' ], self ::$ method ) &&
179
+ !in_array ('* ' , self ::$ method )
180
+ ) {
196
181
http_response_code (405 );
197
182
exit ('Method Not Allowed ' );
198
183
}
199
184
200
185
return [
201
- 'method ' => $ _SERVER ['REQUEST_METHOD ' ],
202
- 'route ' => self ::$ route ,
203
- 'params_value ' => $ req_value ,
204
- 'params ' => $ req ,
186
+ 'method ' => $ _SERVER ['REQUEST_METHOD ' ],
187
+ 'route ' => self ::$ route ,
188
+ 'params_value ' => $ req_value ,
189
+ 'params ' => $ req ,
205
190
];
206
191
}
207
192
208
193
return false ;
209
194
}
210
195
211
-
212
196
/**
213
197
* Matches the current request URI and method against the defined routes.
214
198
*
@@ -219,53 +203,44 @@ public function match(string $method, string|array $route): bool|array
219
203
*
220
204
* @return bool|array Returns an array with 'method' and 'route' keys if a match is found, otherwise false.
221
205
*/
222
- private function match_routing (): bool |array
206
+ private function match_routing (): bool |array
223
207
{
224
208
$ uri = [];
225
209
$ str_route = '' ;
226
210
227
- if (is_array (self ::$ route ))
228
- {
229
- for ($ i = 0 ; $ i < count (self ::$ route ); $ i ++)
230
- {
211
+ if (is_array (self ::$ route )) {
212
+ for ($ i = 0 ; $ i < count (self ::$ route ); $ i ++) {
231
213
$ each_route = preg_replace ("/(^\/)|(\/$)/ " , '' , self ::$ route [$ i ]);
232
214
233
215
empty ($ each_route )
234
- ? array_push ($ uri , strtolower ('/ ' ))
235
- : array_push ($ uri , strtolower ($ each_route ));
216
+ ? array_push ($ uri , strtolower ('/ ' ))
217
+ : array_push ($ uri , strtolower ($ each_route ));
236
218
}
237
- }
238
- else
239
- {
219
+ } else {
240
220
$ str_route = empty (self ::$ route ) ? '/ ' : self ::$ route ;
241
221
}
242
222
243
223
if (
244
- in_array (self ::$ request_uri , $ uri ) ||
245
- self ::$ request_uri === $ str_route
246
- )
247
- {
224
+ in_array (self ::$ request_uri , $ uri ) ||
225
+ self ::$ request_uri === $ str_route
226
+ ) {
248
227
if (
249
- !in_array ($ _SERVER ['REQUEST_METHOD ' ], self ::$ method ) &&
250
- !in_array ('* ' , haystack: self ::$ method )
251
- )
252
- {
228
+ !in_array ($ _SERVER ['REQUEST_METHOD ' ], self ::$ method ) &&
229
+ !in_array ('* ' , haystack: self ::$ method )
230
+ ) {
253
231
http_response_code (405 );
254
232
exit ('Method Not Allowed ' );
255
233
}
256
234
257
235
return [
258
- 'method ' => $ _SERVER ['REQUEST_METHOD ' ],
259
- 'route ' => self ::$ route ,
236
+ 'method ' => $ _SERVER ['REQUEST_METHOD ' ],
237
+ 'route ' => self ::$ route ,
260
238
];
261
- }
262
- else
263
- {
239
+ } else {
264
240
return false ;
265
241
}
266
242
}
267
243
268
-
269
244
/**
270
245
* Validates and matches a route pattern.
271
246
*
@@ -276,18 +251,14 @@ private function match_routing (): bool|array
276
251
*
277
252
* @return array|bool The matched pattern as an array if found, or false if no match is found.
278
253
*/
279
- private function pattern (): array |bool
254
+ private function pattern (): array |bool
280
255
{
281
- if (is_array (self ::$ route ))
282
- {
283
- foreach (self ::$ route as $ value )
284
- {
285
- if (str_starts_with ('pattern: ' , $ value ))
286
- {
256
+ if (is_array (self ::$ route )) {
257
+ foreach (self ::$ route as $ value ) {
258
+ if (str_starts_with ('pattern: ' , $ value )) {
287
259
$ matched = $ this ->validatePattern ($ value );
288
260
289
- if ($ matched )
290
- {
261
+ if ($ matched ) {
291
262
return $ matched ;
292
263
}
293
264
}
@@ -298,31 +269,28 @@ private function pattern (): array|bool
298
269
return $ this ->validatePattern (self ::$ route );
299
270
}
300
271
301
-
302
272
/**
303
273
* Validates the given pattern against the request URI and checks the request method.
304
274
*
305
275
* @param string $pattern The pattern to validate.
306
276
* @return array|bool Returns an array with the request method and route if the pattern matches, otherwise false.
307
277
*/
308
- private function validatePattern (string $ pattern ): array |bool
278
+ private function validatePattern (string $ pattern ): array |bool
309
279
{
310
- $ pattern = trim (substr ($ pattern , 8 ));
280
+ $ pattern = preg_replace ( " /(^\/)|(\/$)/ " , '' , trim (substr ($ pattern , 8 ) ));
311
281
312
- if (fnmatch ($ pattern , self ::$ request_uri ))
313
- {
282
+ if (fnmatch ($ pattern , self ::$ request_uri )) {
314
283
if (
315
- !in_array ($ _SERVER ['REQUEST_METHOD ' ], self ::$ method ) &&
316
- !in_array ('* ' , self ::$ method )
317
- )
318
- {
284
+ !in_array ($ _SERVER ['REQUEST_METHOD ' ], self ::$ method ) &&
285
+ !in_array ('* ' , self ::$ method )
286
+ ) {
319
287
http_response_code (405 );
320
288
exit ('Method Not Allowed ' );
321
289
}
322
290
323
291
return [
324
- 'method ' => $ _SERVER ['REQUEST_METHOD ' ],
325
- 'route ' => self ::$ route ,
292
+ 'method ' => $ _SERVER ['REQUEST_METHOD ' ],
293
+ 'route ' => self ::$ route ,
326
294
];
327
295
}
328
296
return false ;
0 commit comments