mirror of
https://github.com/slawkens/myaac.git
synced 2025-11-28 14:16:50 +01:00
Another approach to fix duplicates - priorities
Priority description: (lower number - higher priority) 1-99 Highest priority - overrides everything, even pages from database, use with caption 100 - default for pages in database 101-999 - recommended range for plugins 1000 - default value for plugins if no other specified 1001 - 9999 - no usage currently 10000 - default myaac routes
This commit is contained in:
@@ -91,34 +91,66 @@ if($logged && $account_logged && $account_logged->isLoaded()) {
|
||||
$dispatcher = FastRoute\cachedDispatcher(function (FastRoute\RouteCollector $r) {
|
||||
$routes = require SYSTEM . 'routes.php';
|
||||
|
||||
$isAlreadyDefined = [];
|
||||
|
||||
$routesFinal = [];
|
||||
foreach(getDatabasePages() as $page) {
|
||||
$isAlreadyDefined[$page] = true;
|
||||
$routesFinal[] = ['*', $page, '__database__/' . $page, true];
|
||||
$routesFinal[] = ['*', $page, '__database__/' . $page, 100];
|
||||
}
|
||||
|
||||
Plugins::clearWarnings();
|
||||
foreach (Plugins::getRoutes() as $route) {
|
||||
if(!isset($isAlreadyDefined[$route[1]])) {
|
||||
$isAlreadyDefined[$route[1]] = true;
|
||||
$routesFinal[] = [$route[0], $route[1], $route[2]];
|
||||
}
|
||||
$routesFinal[] = [$route[0], $route[1], $route[2], $route[3] ?? 1000];
|
||||
/*
|
||||
echo '<pre>';
|
||||
var_dump($route[1], $route[3], $route[2]);
|
||||
echo '/<pre>';
|
||||
*/
|
||||
}
|
||||
|
||||
foreach ($routes as $route) {
|
||||
if(!isset($isAlreadyDefined[$route[1]])) {
|
||||
if (strpos($route[2], '__redirect__') === false && strpos($route[2], '__database__') === false) {
|
||||
$routesFinal[] = [$route[0], $route[1], 'system/pages/' . $route[2]];
|
||||
}
|
||||
else {
|
||||
$routesFinal[] = [$route[0], $route[1], $route[2]];
|
||||
}
|
||||
$isAlreadyDefined[$route[1]] = true;
|
||||
if (!str_contains($route[2], '__redirect__') && !str_contains($route[2], '__database__')) {
|
||||
$routesFinal[] = [$route[0], $route[1], 'system/pages/' . $route[2], $route[3] ?? 10000];
|
||||
}
|
||||
else {
|
||||
$routesFinal[] = [$route[0], $route[1], $route[2], $route[3] ?? 10000];
|
||||
}
|
||||
}
|
||||
|
||||
// sort required for the next step (filter)
|
||||
usort($routesFinal, function ($a, $b)
|
||||
{
|
||||
// key 3 is priority
|
||||
if ($a[3] == $b[3]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ($a[3] < $b[3]) ? -1 : 1;
|
||||
});
|
||||
|
||||
// remove duplicates
|
||||
// if same route pattern, but different priority
|
||||
$routesFinal = array_filter($routesFinal, function ($a) {
|
||||
$aliases = [
|
||||
[':int', ':string', ':alphanum'],
|
||||
[':\d+', ':[A-Za-z0-9-_%+\' ]+', ':[A-Za-z0-9]+'],
|
||||
];
|
||||
|
||||
// apply aliases
|
||||
$a[1] = str_replace($aliases[0], $aliases[1], $a[1]);
|
||||
|
||||
static $duplicates = [];
|
||||
if (isset($duplicates[$a[1]])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$duplicates[$a[1]] = true;
|
||||
return true;
|
||||
});
|
||||
/*
|
||||
echo '<pre>';
|
||||
var_dump($routesFinal);
|
||||
echo '</pre>';
|
||||
die;
|
||||
*/
|
||||
foreach ($routesFinal as $route) {
|
||||
if ($route[0] === '*') {
|
||||
$route[0] = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'];
|
||||
|
||||
Reference in New Issue
Block a user