Skip to content

Commit

Permalink
Another approach to fix duplicates - priorities
Browse files Browse the repository at this point in the history
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
  • Loading branch information
slawkens committed Jun 13, 2024
1 parent c1d4b4f commit c7a6a53
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 46 deletions.
2 changes: 2 additions & 0 deletions plugins/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"redirect_to": "account/manage"
}
},
"routes-default-priority": 1000,
"pages-default-priority": 1000,
"settings": "plugins/your-plugin-folder/settings.php",
"autoload": {
"pages": true,
Expand Down
64 changes: 48 additions & 16 deletions system/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,34 +91,66 @@
$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'];
Expand Down
6 changes: 3 additions & 3 deletions system/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
['GET', 'news/{id:int}', 'news/archive.php'],

// block access to some files
['*', 'account/base', '404.php'], // this is to block account/base.php
['*', 'forum/base', '404.php'],
['*', 'guilds/base', '404.php'],
['*', 'account/base', '404.php', 10], // this is to block account/base.php
['*', 'forum/base', '404.php', 10],
['*', 'guilds/base', '404.php', 10],

[['GET', 'POST'], 'account/password', 'account/change_password.php'],
[['GET', 'POST'], 'account/register/new', 'account/register_new.php'],
Expand Down
42 changes: 15 additions & 27 deletions system/src/Plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public static function getRoutes()
$duplicates = [];
$routes = [];
foreach(self::getAllPluginsJson() as $plugin) {
$priority = 100;
if (isset($plugin['priority'])) {
$priority = $plugin['priority'];
$routesDefaultPriority = 1000;
if (isset($plugin['routes-default-priority'])) {
$routesDefaultPriority = $plugin['routes-default-priority'];
}

$warningPreTitle = 'Plugin: ' . $plugin['name'] . ' - ';
Expand All @@ -49,7 +49,7 @@ public static function getRoutes()
}

if (!isset($info['priority'])) {
$info['priority'] = 100; // default priority
$info['priority'] = $routesDefaultPriority; // default priority taken from plugin.json
}

if (isset($info['redirect_from'])) {
Expand All @@ -68,27 +68,15 @@ public static function getRoutes()
// replace first occurrence of / in pattern if found (will be auto-added later)
removeIfFirstSlash($info['pattern']);

foreach ($routes as $id => &$route) {
if($route[1] == $info['pattern']) {
if($info['priority'] < $route[3]) {
self::$warnings[] = $warningPreTitle . "Duplicated route with lower priority: {$info['pattern']}. Disabling this route...";
continue 2;
}
else {
self::$warnings[] = $warningPreTitle . "Duplicated route with lower priority: {$route[1]} ({$route[3]}). Disabling this route...";
unset($routes[$id]);
}
}
}

$tmp = preg_replace("/\[[^)]+\]/",'', $info['pattern']);
if (!isset($duplicates[$tmp])) {
$routes[] = [$methods, $info['pattern'], $info['file'], $info['priority']];
$duplicates[$tmp] = true;
}
$routes[] = [$methods, $info['pattern'], $info['file'], $info['priority']];
}
}

$pagesDefaultPriority = 1000;
if (isset($plugin['pages-default-priority'])) {
$pagesDefaultPriority = $plugin['pages-default-priority'];
}

if (self::getAutoLoadOption($plugin, 'pages', true)) {
//
// Get all plugins/*/pages/*.php pages
Expand All @@ -99,7 +87,7 @@ public static function getRoutes()
$name = pathinfo($file, PATHINFO_FILENAME);

if (!isset($duplicates[$name])) {
$routes[] = [['get', 'post'], $name, $file, $priority];
$routes[] = [['get', 'post'], $name, $file, $pagesDefaultPriority];
$duplicates[$name] = true;
}
}
Expand All @@ -119,7 +107,7 @@ public static function getRoutes()
$name = $folderName . '/' . pathinfo($file, PATHINFO_FILENAME);

if (!isset($duplicates[$name])) {
$routes[] = [['get', 'post'], $name, $file, $priority];
$routes[] = [['get', 'post'], $name, $file, $pagesDefaultPriority];
$duplicates[$name] = true;
}
}
Expand All @@ -139,9 +127,9 @@ public static function getRoutes()

// cleanup before passing back
// priority is not needed anymore
foreach ($routes as &$route) {
unset($route[3]);
}
//foreach ($routes as &$route) {
// unset($route[3]);
//}

if ($cache->enabled()) {
$cache->set('plugins_routes', serialize($routes), 600);
Expand Down

0 comments on commit c7a6a53

Please sign in to comment.