Skip to content

Commit 3058e48

Browse files
committed
update
1 parent 00dfd30 commit 3058e48

File tree

2 files changed

+49
-61
lines changed

2 files changed

+49
-61
lines changed

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# @see application/config/util/_myMVC.php
2-
3.2.x
2+
3.2.7

application/library/MVC/Route.php

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ public static function init()
3737

3838
DEFAULT_SOURCE_PHP_FILES: {
3939

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()))
4345
{
44-
if ('php' === strtolower($oSplFileInfo->getExtension()))
45-
{
46-
require_once $oSplFileInfo->getPathname();
47-
}
46+
require_once $oSplFileInfo->getPathname();
4847
}
4948
}
49+
}
5050

5151
\MVC\Event::RUN('mvc.route.init.after');
5252
}
@@ -299,7 +299,7 @@ public static function getIndexOnWildcard(string $sPath = '')
299299

300300
/**
301301
* @param string $sPath
302-
* @return string
302+
* @return string matching route path | empty
303303
*/
304304
public static function getPathOnPlaceholderIndex(string $sPath = '')
305305
{
@@ -308,67 +308,55 @@ public static function getPathOnPlaceholderIndex(string $sPath = '')
308308
$iLengthPath = count($aPartPath);
309309
$aIndex = self::getIndices();
310310

311-
foreach ($aIndex as $iKey => $sValue)
311+
// iterate routes
312+
foreach ($aIndex as $sValue)
312313
{
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))))
326319
{
327-
$sPart.= get($aPartPath[$i]) . '/';
320+
continue;
328321
}
329322

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)
343327
{
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)
356330
{
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]);
366334
}
367335

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;
370340

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+
}
372360
}
373361
}
374362

0 commit comments

Comments
 (0)