properly (?) sort the route URLs
This commit is contained in:
parent
4892cb9f26
commit
bcb4250466
55
app/lib/phast/Sorting/QuickSort.inc.php
Normal file
55
app/lib/phast/Sorting/QuickSort.inc.php
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
@ -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.
|
* Starts the Phast application.
|
||||||
@ -969,7 +992,11 @@
|
|||||||
|
|
||||||
$pathVars = array();
|
$pathVars = array();
|
||||||
|
|
||||||
|
$i = 0;
|
||||||
$actualPage = null;
|
$actualPage = null;
|
||||||
|
$actualPages = array();
|
||||||
|
$pathVarsVars = array();
|
||||||
|
$sortedPages = null;
|
||||||
|
|
||||||
foreach (System::$Parser->Pages as $page)
|
foreach (System::$Parser->Pages as $page)
|
||||||
{
|
{
|
||||||
@ -1011,15 +1038,23 @@
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// FIXME : find the "best match" for the given route
|
||||||
|
$qs = new QuickSort();
|
||||||
|
|
||||||
$vars = System::ParsePathStr($page->FileName, System::GetVirtualPathString());
|
$vars = System::ParsePathStr($page->FileName, System::GetVirtualPathString());
|
||||||
if ($vars !== false)
|
if ($vars !== false)
|
||||||
{
|
{
|
||||||
$actualPage = $page;
|
$actualPages[] = $page;
|
||||||
$pathVars = $vars;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$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)
|
if ($actualPage != null)
|
||||||
{
|
{
|
||||||
foreach ($pathVars as $key => $value)
|
foreach ($pathVars as $key => $value)
|
||||||
@ -1136,6 +1171,8 @@
|
|||||||
const Bottom = 3;
|
const Bottom = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
require_once("Sorting/QuickSort.inc.php");
|
||||||
|
|
||||||
require("Conditionals/ConditionalComparison.inc.php");
|
require("Conditionals/ConditionalComparison.inc.php");
|
||||||
require("Conditionals/ConditionalStatement.inc.php");
|
require("Conditionals/ConditionalStatement.inc.php");
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user