From bcb425046606c57ae8bf0b09d5af10c567f801dc Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sun, 26 Nov 2023 23:32:16 -0500 Subject: [PATCH] properly (?) sort the route URLs --- app/lib/phast/Sorting/QuickSort.inc.php | 55 +++++++++++++++++++++++++ app/lib/phast/System.inc.php | 45 ++++++++++++++++++-- 2 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 app/lib/phast/Sorting/QuickSort.inc.php diff --git a/app/lib/phast/Sorting/QuickSort.inc.php b/app/lib/phast/Sorting/QuickSort.inc.php new file mode 100644 index 0000000..4fe99fc --- /dev/null +++ b/app/lib/phast/Sorting/QuickSort.inc.php @@ -0,0 +1,55 @@ +partition($arr, $low, $high); + $this->quickSort($arr, $low, $pi - 1, $predicate); + $this->quickSort($arr, $pi + 1, $high, $predicate); + } + } + + public function Sort(array $array, mixed $predicate) + { + if (count($array) == 2) + { + if ($predicate($array[0], $array[1])) + { + return [ $array[0], $array[1] ]; + } + else + { + return [ $array[1], $array[0] ]; + } + } + $this->quickSort($array, 0, count($array), $predicate); + return $array; + } + +} + +?> \ No newline at end of file diff --git a/app/lib/phast/System.inc.php b/app/lib/phast/System.inc.php index a0e9f16..14664c2 100644 --- a/app/lib/phast/System.inc.php +++ b/app/lib/phast/System.inc.php @@ -888,6 +888,29 @@ } + private static function __ReplaceAny($str, $before, $after, $with) + { + $str2 = ""; + $inside = false; + for ($i = 0; $i < strlen($str); $i++) + { + if (substr($str, $i, strlen($before)) == $before) + { + $inside = true; + } + else if (substr($str, $i, strlen($after)) == $after) + { + $inside = false; + $str2 .= $with; + } + else if (!$inside) + { + $str2 .= $str[$i]; + } + } + return $str2; + } + /** * Starts the Phast application. @@ -969,8 +992,12 @@ $pathVars = array(); + $i = 0; $actualPage = null; - + $actualPages = array(); + $pathVarsVars = array(); + $sortedPages = null; + foreach (System::$Parser->Pages as $page) { if (!$page->Enabled) continue; @@ -1011,14 +1038,22 @@ } */ + // FIXME : find the "best match" for the given route + $qs = new QuickSort(); + $vars = System::ParsePathStr($page->FileName, System::GetVirtualPathString()); if ($vars !== false) { - $actualPage = $page; - $pathVars = $vars; - break; + $actualPages[] = $page; } } + + $sortedPages = $qs->Sort($actualPages, function($left, $right) + { + return strlen(System::__ReplaceAny($left->FileName, "{", "}" , "?")) > strlen(System::__ReplaceAny($right->FileName, "{", "}" , "?")); + }); + $actualPage = $sortedPages[0]; + $pathVars = System::ParsePathStr($actualPage->FileName, System::GetVirtualPathString()); if ($actualPage != null) { @@ -1135,6 +1170,8 @@ */ const Bottom = 3; } + + require_once("Sorting/QuickSort.inc.php"); require("Conditionals/ConditionalComparison.inc.php"); require("Conditionals/ConditionalStatement.inc.php");