@ -165,7 +165,7 @@ class Router
* Add a route to the Router
*
* @param string $route
* @param null $routeConfig
* @param mixed $routeConfig
* @param int $priority
*/
public function addRoute(string $route, $routeConfig = null, int $priority = Priority::NORMAL)
@ -174,14 +174,19 @@ class Router
if (is_null($routeConfig))
$routeConfig = ['callable' => [$this, 'defaultCallable']];
// Select the category
$category = is_array($routeConfig) & & isset($routeConfig['category']) ? $routeConfig['category'] : 'default';
if (!isset($this->routes[$category]))
$this->routes[$category] = [];
// Convert wildcards to Regex
$route = str_replace([':any',':num'], ['[^/]+', '[0-9]+'], $route);
if (!isset($this->routes[$priority]))
$this->routes[$priority] = [];
if (!isset($this->routes[$category][$ priority]))
$this->routes[$category][$ priority] = [];
if (!isset($this->routes[$priority][$route]))
$this->routes[$priority][$route] = $routeConfig;
if (!isset($this->routes[$category][$ priority][$route]))
$this->routes[$category][$ priority][$route] = $routeConfig;
Logger::log('Route added with ' . Priority::getPriority($priority) . ": '" . $route."'");
}
@ -189,30 +194,33 @@ class Router
/**
* Removes a route from the array based on the given route.
*
* @param $route string The route to remove
* @param int $priority
* @param string $route The route to remove
* @param string $category The category to remove it from (defaults to 'default')
* @param int $priority The priority to remove it from (defaults to Priority::NORMAL)
*/
public function removeRoute(string $route, int $priority = Priority::NORMAL)
public function removeRoute(string $route, string $category = 'default', int $priority = Priority::NORMAL)
{
if (!isset($this->routes[$priority][$route]))
if (!isset($this->routes[$category][$ priority][$route]))
return;
unset($this->routes[$priority][$route]);
unset($this->routes[$category][$ priority][$route]);
Logger::log('Route removed: '.$route);
}
/**
* @param string $path
* @param array $routes Optional routes for not using the default
* @return mixed
* @throws NotFoundException
* @throws RouterException
* @throws HaltException
* @param string $path The string to route using Router
* @param string $category The category of routes to search in (defaults to 'default')
* @param array $routes Alternative routes if using global routes is not desired
* @return mixed The output of the callable
* @throws NotFoundException Thrown if route failed to deliver
* @throws RouterException Thrown if things fatally break
* @throws HaltException Thrown if route results in an unauthorised request
*/
public function route(string $path, array $routes = [])
public function route(string $path, string $category = 'default', array $routes = [])
{
// Select the routes to use
$routes = empty($routes) ? $this->routes : $routes;
$globalRoutes = isset($this->routes[$category]) ? $this->routes[$category] : [];
$routes = empty($routes) ? $globalRoutes : $routes;
// Check all the provided custom paths, ordered by priority
for ($i=Priority::getHighestPriority(); $i< =Priority::getLowestPriority(); $i++) {
@ -433,13 +441,17 @@ class Router
/**
* Returns an array with all the routes.
*
* @param string $category
* @param int $priority
* @return array
* @codeCoverageIgnore
*/
public function getRoutes(int $priority = Priority::NORMAL): array
public function getRoutes(string $category = 'default', int $priority = Priority::NORMAL): array
{
return $this->routes[$priority];
if (isset($this->routes[$category][$priority]))
return $this->routes[$category][$priority];
return [];
}
/**
@ -448,7 +460,7 @@ class Router
* @return string|null
* @codeCoverageIgnore
*/
public function getCurrentRoute()
public function getCurrentRoute(): ?string
{
return $this->route;
}
@ -459,7 +471,7 @@ class Router
* @return null|array
* @codeCoverageIgnore
*/
public function getCurrentMatches()
public function getCurrentMatches(): ?array
{
return $this->matches;
}
@ -470,7 +482,7 @@ class Router
* @return View|null
* @codeCoverageIgnore
*/
public function getCurrentView()
public function getCurrentView(): ?View
{
return $this->view;
}
@ -481,7 +493,7 @@ class Router
* @return Controller|null
* @codeCoverageIgnore
*/
public function getCurrentController()
public function getCurrentController(): ?Controller
{
return $this->controller;
}