properly (?) sort the route URLs

This commit is contained in:
Michael Becker 2023-11-26 23:32:16 -05:00
parent 4892cb9f26
commit bcb4250466
2 changed files with 96 additions and 4 deletions

View File

@ -0,0 +1,55 @@
<?php
namespace Phast;
class QuickSort
{
private function partition(&$arr, $low, $high)
{
$pivot = $arr[$high];
$i = ($low - 1);
for ($j = $low; $j <= $high - 1; $j++)
{
if ($arr[$j] < $pivot)
{
$i++;
list($arr[$i], $arr[$j]) = array($arr[$j], $arr[$i]);
}
}
list($arr[$i + 1], $arr[$high]) = array($arr[$high], $arr[$i + 1]);
return ($i + 1);
}
private function quickSort(&$arr, $low, $high, $predicate)
{
$low = 0;
$high = count($arr);
if ($predicate($low, $high))
{
$pi = $this->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;
}
}
?>

View File

@ -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");