Skip to content

Commit

Permalink
[skip ci] Sort routes by priority (router cache init)
Browse files Browse the repository at this point in the history
Fix #54
  • Loading branch information
jcheron committed Jul 1, 2019
1 parent 979b6a7 commit 5c03cc9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
7 changes: 1 addition & 6 deletions src/Ubiquity/cache/parser/ControllerParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
use Ubiquity\utils\base\UString;
use Ubiquity\annotations\router\RouteAnnotation;
use Ubiquity\cache\ClassUtils;
use Ubiquity\utils\base\UArray;

/**
* Scans a controller to detect routes defined by annotations.
* Ubiquity\cache\parser$ControllerParser
* This class is part of Ubiquity
*
* @author jcheron <myaddressmail@gmail.com>
* @version 1.0.6
* @version 1.0.7
*
*/
class ControllerParser {
Expand Down Expand Up @@ -110,10 +109,6 @@ public function asArray() {
self::parseRouteArray ( $result, $this->controllerClass, $params, $arrayAnnotsMethod ["method"], $method, $prefix, $httpMethods );
}
}
uasort ( $result, function ($item1, $item2) {
return UArray::getRecursive ( $item2, "priority", 0 ) <=> UArray::getRecursive ( $item1, "priority", 0 );
} );
UArray::removeRecursive ( $result, "priority" );
return $result;
}

Expand Down
11 changes: 10 additions & 1 deletion src/Ubiquity/cache/traits/RouterCacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* This class is part of Ubiquity
*
* @author jcheron <myaddressmail@gmail.com>
* @version 1.0.4
* @version 1.0.5
* @property \Ubiquity\cache\system\AbstractDataCache $cache
*
*/
Expand Down Expand Up @@ -52,9 +52,18 @@ private static function parseControllerFiles(&$config, $silent = false) {
}
}
}
self::sortByPriority ( $routes ['default'] );
self::sortByPriority ( $routes ['rest'] );
return $routes;
}

protected static function sortByPriority(&$array) {
uasort ( $array, function ($item1, $item2) {
return UArray::getRecursive ( $item2, "priority", 0 ) <=> UArray::getRecursive ( $item1, "priority", 0 );
} );
UArray::removeRecursive ( $array, "priority" );
}

private static function initRouterCache(&$config, $silent = false) {
$routes = self::parseControllerFiles ( $config, $silent );
self::$cache->store ( "controllers/routes.default", "return " . UArray::asPhpArray ( $routes ["default"], "array" ) . ";", 'controllers' );
Expand Down

0 comments on commit 5c03cc9

Please sign in to comment.