@@ -37,16 +37,16 @@ public static function init()
37
37
38
38
DEFAULT_SOURCE_PHP_FILES : {
39
39
40
- // require recursively all php files in module's routing dir
41
- /** @var \SplFileInfo $oSplFileInfo */
42
- foreach (new \RecursiveIteratorIterator (new \RecursiveDirectoryIterator (Config::get_MVC_MODULE_CURRENT_ETC_DIR () . '/routing ' )) as $ oSplFileInfo )
40
+ // require recursively all php files in module's routing dir
41
+ /** @var \SplFileInfo $oSplFileInfo */
42
+ foreach (new \RecursiveIteratorIterator (new \RecursiveDirectoryIterator (Config::get_MVC_MODULE_CURRENT_ETC_DIR () . '/routing ' )) as $ oSplFileInfo )
43
+ {
44
+ if ('php ' === strtolower ($ oSplFileInfo ->getExtension ()))
43
45
{
44
- if ('php ' === strtolower ($ oSplFileInfo ->getExtension ()))
45
- {
46
- require_once $ oSplFileInfo ->getPathname ();
47
- }
46
+ require_once $ oSplFileInfo ->getPathname ();
48
47
}
49
48
}
49
+ }
50
50
51
51
\MVC \Event::RUN ('mvc.route.init.after ' );
52
52
}
@@ -299,7 +299,7 @@ public static function getIndexOnWildcard(string $sPath = '')
299
299
300
300
/**
301
301
* @param string $sPath
302
- * @return string
302
+ * @return string matching route path | empty
303
303
*/
304
304
public static function getPathOnPlaceholderIndex (string $ sPath = '' )
305
305
{
@@ -308,67 +308,55 @@ public static function getPathOnPlaceholderIndex(string $sPath = '')
308
308
$ iLengthPath = count ($ aPartPath );
309
309
$ aIndex = self ::getIndices ();
310
310
311
- foreach ($ aIndex as $ iKey => $ sValue )
311
+ // iterate routes
312
+ foreach ($ aIndex as $ sValue )
312
313
{
313
- $ aPartMatch = array_diff (preg_split ('@/@ ' , $ sValue , 0 , PREG_SPLIT_NO_EMPTY ), array ('* ' )); # delete "*"
314
- $ sLastCharRight = substr ($ sValue , -1 );
315
- $ bIsWildcard = ('* ' === $ sLastCharRight ) ? true : false ;
316
- $ iLengthFoo = count ($ aPartMatch );
317
-
318
- // string part before first placeholder has to match
319
- $ sStrToKColon = strtok ($ sValue , ': ' );
320
- $ sStrToKBrace = strtok ($ sValue , '{ ' );
321
- $ bPartBeforePlaceholderMatchColon = (substr ($ sPath , 0 , strlen ($ sStrToKColon )) === $ sStrToKColon ) ? true : false ;
322
- $ bPartBeforePlaceholderMatchBrace = (substr ($ sPath , 0 , strlen ($ sStrToKBrace )) === $ sStrToKBrace ) ? true : false ;
323
- $ sPart = '/ ' ;
324
-
325
- for ($ i = 0 ; $ i < $ iLengthFoo ; $ i ++)
314
+ $ aRoute = preg_split ('@/@ ' , $ sValue , 0 , PREG_SPLIT_NO_EMPTY );
315
+ $ iLengthRoute = count ($ aRoute );
316
+
317
+ // skip routes without * at the end if they are shorter than the requested path
318
+ if (($ iLengthRoute < $ iLengthPath ) && ('* ' !== current (array_reverse ($ aRoute ))))
326
319
{
327
- $ sPart .= get ( $ aPartPath [ $ i ]) . ' / ' ;
320
+ continue ;
328
321
}
329
322
330
- $ sTail = substr ($ sPath , strlen ($ sPart ));
331
-
332
- // detect placeholder route match
333
- if (
334
- (true === $ bPartBeforePlaceholderMatchColon || true === $ bPartBeforePlaceholderMatchBrace ) &&
335
- (
336
- // has exact same amount of / parts
337
- $ iLengthPath === $ iLengthFoo
338
- OR
339
- // has minimum amount of / parts and is wildcard (has tail)
340
- (true === $ bIsWildcard && $ iLengthPath >= $ iLengthFoo )
341
- )
342
- )
323
+ $ aPathParam = array ();
324
+
325
+ // compare each part of the route
326
+ foreach ($ aRoute as $ iKey => $ sPart )
343
327
{
344
- (true === $ bPartBeforePlaceholderMatchColon )
345
- ? $ aPlaceholder = preg_grep ('/:/i ' , $ aPartMatch )
346
- : false
347
- ;
348
- (true === $ bPartBeforePlaceholderMatchBrace )
349
- ? $ aPlaceholder = preg_grep ('/\{*\}/i ' , $ aPartMatch )
350
- : false
351
- ;
352
-
353
- $ aPathParam = array ();
354
-
355
- foreach ($ aPlaceholder as $ iIndex => $ sPlaceholder )
328
+ // part is wildcard; save tail, remove wildcard from array
329
+ if ('* ' === $ sPart )
356
330
{
357
- (true === $ bPartBeforePlaceholderMatchColon )
358
- ? $ sPlaceholder = substr ($ sPlaceholder , 1 )
359
- : false
360
- ;
361
- (true === $ bPartBeforePlaceholderMatchBrace )
362
- ? $ sPlaceholder = substr ($ sPlaceholder , 1 , -1 )
363
- : false
364
- ;
365
- $ aPathParam [$ sPlaceholder ] = $ aPartPath [$ iIndex ];
331
+ $ aTail = array_slice ($ aPartPath , $ iKey );
332
+ $ aPathParam ['_tail ' ] = (true === empty ($ aTail )) ? '' : implode ('/ ' , $ aTail ) . (('/ ' === (substr ($ sPath , -1 ))) ? '/ ' : '' );
333
+ unset($ aRoute [$ iKey ]);
366
334
}
367
335
368
- $ aPathParam ['_tail ' ] = $ sTail ;
369
- Request::setPathParam ($ aPathParam );
336
+ // part is a variable; save key value
337
+ $ sKey = '' ;
338
+ (': ' === (substr ($ sPart , 0 , 1 ))) ? $ sKey = str_replace (': ' , '' , $ sPart ) : false ;
339
+ ('{} ' === (substr ($ sPart , 0 , 1 ) . substr ($ sPart , -1 ))) ? $ sKey = str_replace ('} ' , '' , str_replace ('{ ' , '' , $ sPart )) : false ;
370
340
371
- return (string ) get ($ aIndex [$ iKey ], '' );
341
+ if (false === empty ($ sKey ))
342
+ {
343
+ $ aRoute [$ iKey ] = $ aPartPath [$ iKey ]; # replace variable by concrete value from path
344
+ $ aPathParam [$ sKey ] = $ aPartPath [$ iKey ]; # save PathParam
345
+ }
346
+
347
+ // add leading and/or trailing slashes if route was defined so
348
+ $ sRoute = ('/ ' === (substr ($ sValue , 0 , 1 ))) ? '/ ' : '' ;
349
+ $ sRoute .= implode ('/ ' , $ aRoute ) . (('* ' === $ sPart ) ? '/ ' . $ aPathParam ['_tail ' ] : '' ); # add tail if part is a wildcard
350
+ $ sRoute .= ('/ ' === (substr ($ sValue , -1 ))) ? '/ ' : '' ;
351
+ $ sRoute = str_replace ('// ' , '/ ' , $ sRoute );
352
+
353
+ // now check if path and route do match
354
+ if ($ sPath === $ sRoute )
355
+ {
356
+ (false === empty ($ aPathParam )) ? Request::setPathParam ($ aPathParam ) : false ;
357
+
358
+ return $ sValue ;
359
+ }
372
360
}
373
361
}
374
362
0 commit comments