Skip to content

Commit

Permalink
=
Browse files Browse the repository at this point in the history
  • Loading branch information
arikaim-repository committed Mar 28, 2021
1 parent 00a8e42 commit 8959363
Showing 1 changed file with 10 additions and 37 deletions.
47 changes: 10 additions & 37 deletions Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,7 @@ public function addMiddleware(string $method, string $pattern, string $middlewar
{
return $this->adapter->addMiddleware($method,$pattern,$middlewareClass);
}

/**
* Resolve middlewares
*
* @param string $handlerClass
* @return array|null
*/
protected function resolveMiddlewares(string $handlerClass): ?array
{
if (\class_exists($handlerClass) == false) {
return null;
}

$controller = new $handlerClass();
$middlewares = $controller->getMiddlewares();

return (count($middlewares) > 0) ? $middlewares : null;
}


/**
* Set routes status
*
Expand Down Expand Up @@ -166,8 +148,6 @@ public function saveTemplateRoute(
return false;
}

$middlewares = $this->resolveMiddlewares($handlerClass);

$route = [
'method' => 'GET',
'pattern' => $pattern,
Expand All @@ -177,8 +157,7 @@ public function saveTemplateRoute(
'type' => $type,
'page_name' => $pageName,
'template_name' => $templateName,
'redirect_url' => $redirectUrl,
'middlewares' => (empty($middlewares) == false) ? \json_encode($middlewares) : null
'redirect_url' => $redirectUrl
];

$this->cache->delete('routes.list');
Expand Down Expand Up @@ -234,7 +213,7 @@ public function addHomePageRoute(
* @param string $handlerClass
* @param string $handlerMethod
* @param string $extension
* @param string $pageName
* @param string|null $pageName
* @param integer $auth
* @param string|null $name
* @param boolean $withLanguage
Expand All @@ -246,7 +225,7 @@ public function addPageRoute(
string $handlerClass,
string $handlerMethod,
string $extension,
string $pageName,
?string $pageName,
?string $auth = null,
?string $name = null,
bool $withLanguage = true,
Expand All @@ -267,8 +246,6 @@ public function addPageRoute(
}

$pattern = ($withLanguage == true) ? $pattern . $languagePattern : $pattern;
$middlewares = $this->resolveMiddlewares($handlerClass);

$route = [
'method' => 'GET',
'pattern' => $pattern,
Expand All @@ -279,8 +256,7 @@ public function addPageRoute(
'extension_name' => $extension,
'page_name' => $pageName,
'name' => $name,
'regex' => null,
'middlewares' => (empty($middlewares) == false) ? \json_encode($middlewares) : null
'regex' => null
];

$this->cache->delete('routes.list');
Expand Down Expand Up @@ -334,8 +310,6 @@ public function addApiRoute(
return false;
}

$middlewares = $this->resolveMiddlewares($handlerClass);

$route = [
'method' => $method,
'pattern' => $pattern,
Expand All @@ -344,8 +318,7 @@ public function addApiRoute(
'auth' => $auth,
'type' => $type,
'regex' => null,
'extension_name' => $extension,
'middlewares' => (empty($middlewares) == false) ? \json_encode($middlewares) : null
'extension_name' => $extension
];

$this->cache->delete('routes.list');
Expand Down Expand Up @@ -457,7 +430,7 @@ public function getRoutes(array $filter = [])
public function getAllRoutes()
{
$routes = $this->cache->fetch('routes.list');
if (\is_array($routes) == false) {
if ($routes === false) {
$routes = $this->getRoutes(['status' => 1]);
$this->cache->save('routes.list',$routes,Self::$cacheSaveTime);
}
Expand All @@ -472,11 +445,11 @@ public function getAllRoutes()
* @param int|null $type
* @return array
*/
public function searchRoutes(string $method, $type = null)
public function searchRoutes(string $method, $type = null): array
{
$cacheItemkey = 'routes.list.' . $method . '.' . $type ?? 'all';
$cacheItemkey = 'routes.list.' . $method . '.' . ($type ?? 'all');
$routes = $this->cache->fetch($cacheItemkey);
if (\is_array($routes) == false) {
if ($routes === false) {
$routes = $this->adapter->searchRoutes($method,$type);
$this->cache->save($cacheItemkey,$routes,Self::$cacheSaveTime);
}
Expand Down

0 comments on commit 8959363

Please sign in to comment.