Skip to content

Commit

Permalink
Merge pull request #140 from armanist/master
Browse files Browse the repository at this point in the history
Restful routes issues
  • Loading branch information
andrey-smaelov committed Aug 9, 2023
2 parents 4560458 + 3619017 commit 5a7b254
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 31 deletions.
18 changes: 18 additions & 0 deletions src/Router/RouteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,22 @@ public static function setCurrentRoute(array $route)
self::$currentRoute = $route;
}

/**
* Set Routes
* @param array $routes
*/
public static function setRoutes(array $routes)
{
static::$routes = $routes;
}

/**
* Get Routes
* @return array
*/
public static function getRoutes(): array
{
return static::$routes;
}

}
72 changes: 41 additions & 31 deletions src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Router extends RouteController
* List of routes
* @var array
*/
private static $routes = [];
protected static $routes = [];

/**
* matched routes
Expand Down Expand Up @@ -109,50 +109,57 @@ public function findRoute()
$this->checkCollision();
}

$matchedRoute = current($this->matchedRoutes);
$currentRoute = $this->currentRoute();

$this->checkMethod($matchedRoute);

$matchedRoute['uri'] = $uri;
if (!$currentRoute) {
throw RouteException::incorrectMethod($this->request->getMethod());
}

self::setCurrentRoute($matchedRoute);
self::setCurrentRoute($currentRoute);

if (filter_var(config()->get(Debugger::DEBUG_ENABLED), FILTER_VALIDATE_BOOLEAN)) {
$routeInfo = [];

array_walk($matchedRoute, function ($value, $key) use (&$routeInfo) {
$routeInfo[ucfirst($key)] = json_encode($value);
});

Debugger::addToStore(Debugger::ROUTES, LogLevel::INFO, $routeInfo);
$this->collectDebugData($currentRoute);
}
}

/**
* Set Routes
* @param array $routes
* Resets the routes
*/
public static function setRoutes(array $routes)
public function resetRoutes()
{
self::$routes = $routes;
parent::$currentRoute = null;
$this->matchedRoutes = [];
}

/**
* Get Routes
* @return array
* Gets the current route
* @return array|null
*/
public static function getRoutes(): array
private function currentRoute(): ?array
{
return self::$routes;
foreach ($this->matchedRoutes as $matchedRoute) {
if ($this->checkMethod($matchedRoute)) {
return $matchedRoute;
}
}

return null;
}

/**
* Resets the routes
* Collects debug data
* @param array $currentRoute
* @return void
*/
public function resetRoutes()
private function collectDebugData(array $currentRoute)
{
parent::$currentRoute = null;
$this->matchedRoutes = [];
$routeInfo = [];

array_walk($currentRoute, function ($value, $key) use (&$routeInfo) {
$routeInfo[ucfirst($key)] = json_encode($value);
});

Debugger::addToStore(Debugger::ROUTES, LogLevel::INFO, $routeInfo);
}

/**
Expand All @@ -171,6 +178,7 @@ private function findPatternMatches(string $uri)
preg_match("/^" . $this->escape($pattern) . "$/u", $requestUri, $matches);

if (count($matches)) {
$route['uri'] = $uri;
$route['params'] = $this->routeParams($params, $matches);
$route['pattern'] = $pattern;
$this->matchedRoutes[] = $route;
Expand Down Expand Up @@ -384,17 +392,19 @@ private function checkCollision()
/**
* Checks the request method against defined route method
* @param array $matchedRoute
* @throws RouteException
* @return bool
*/
private function checkMethod(array $matchedRoute)
private function checkMethod(array $matchedRoute): bool
{
if (strpos($matchedRoute['method'], '|') !== false) {
if (!in_array($this->request->getMethod(), explode('|', $matchedRoute['method']))) {
throw RouteException::incorrectMethod($this->request->getMethod());
if (in_array($this->request->getMethod(), explode('|', $matchedRoute['method']))) {
return true;
}
} else if ($this->request->getMethod() != $matchedRoute['method']) {
throw RouteException::incorrectMethod($this->request->getMethod());
} else if ($this->request->getMethod() == $matchedRoute['method']) {
return true;
}

return false;
}

/**
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,46 @@ public function testFindRouteWithNamedParams()
$this->assertEquals('523', route_param('ref'));
}

public function testRestfulRoutes()
{
Router::setRoutes([
[
"route" => "api-task",
"method" => "POST",
"controller" => "TaskController",
"action" => "create",
"module" => "Api",
],
[
"route" => "api-task",
"method" => "GET",
"controller" => "TaskController",
"action" => "show",
"module" => "Api",
]
]);

$this->request->create('GET', 'http://testdomain.com/api-task');

$this->router->findRoute();

$this->assertEquals('GET', route_method());

$this->assertEquals('show', current_action());

$request = new Request();

$router = new Router($request, new Response());

$request->create('POST', 'http://testdomain.com/api-task');

$router->findRoute();

$this->assertEquals('POST', route_method());

$this->assertEquals('create', current_action());
}

public function testRouteIncorrectMethod()
{
Router::setRoutes([
Expand Down

0 comments on commit 5a7b254

Please sign in to comment.