Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
seting route closures refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Jun 17, 2017
1 parent e9175e7 commit c704697
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@ private function extractFolder(string $requestPath, string $folder) : string
* @throws InvalidArgumentException
* @throws UnexpectedValueException
*/
public function add($requestMethods, string $route, string $action, ?string $returnType = null, ?string $alias = null)
public function add(
$requestMethods,
string $route,
string $action,
?string $returnType = null,
?string $alias = null
) : void
{
$requestMethodsGiven = is_array($requestMethods) ? (array) $requestMethods : [0 => $requestMethods];
$returnType = $this->determineReturnType($returnType);
Expand All @@ -195,7 +201,6 @@ public function add($requestMethods, string $route, string $action, ?string $ret
*/
public function __call(string $method, array $args) : void
{

$this->checkRequestMethodIsValid($method);
$defaults = [
null,
Expand Down Expand Up @@ -254,13 +259,15 @@ private function checkRequestMethodParameterType($requestMethod) : void
*/
private function dispatcher() : FastRoute\Dispatcher
{

$this->setRouteClosures();
if ($this->cachedFile !== null) {
return $this->cachedDispatcher();
}
return $this->simpleDispatcher();
}

private function simpleDispatcher()
private function simpleDispatcher() : FastRoute\Dispatcher\GroupCountBased
{
$options = [
'routeParser' => FastRoute\RouteParser\Std::class,
Expand All @@ -277,7 +284,7 @@ private function simpleDispatcher()
return new $options['dispatcher']($routeCollector->getData());
}

private function cachedDispatcher()
private function cachedDispatcher() : FastRoute\Dispatcher\GroupCountBased
{
$options = [
'routeParser' => FastRoute\RouteParser\Std::class,
Expand Down Expand Up @@ -310,17 +317,26 @@ private function cachedDispatcher()
* @param FastRoute\RouteCollector $route
*/
private function addRoutes(FastRoute\RouteCollector $route) : void
{
$routeIndex=0;
foreach ($this->routes as $definedRoute) {
$definedRoute[3] = $definedRoute[3] ?? $this->defaultReturnType;
$routeName = 'routeClosure'.$routeIndex;
$route->addRoute(strtoupper($definedRoute[0]), $definedRoute[1], $routeName);
$routeIndex++;
}
}
private function setRouteClosures() : void
{
$routeIndex=0;
foreach ($this->routes as $definedRoute) {
$definedRoute[3] = $definedRoute[3] ?? $this->defaultReturnType;
$routeName = 'routeClosure'.$routeIndex;
[$null1, $null2, $controller, $returnType] = $definedRoute;
$returnType = Router::$translations[$returnType] ?? $this->defaultReturnType;
$this->routerClosures[$routeName] = function($args) use ($controller, $returnType) {
$this->routerClosures[$routeName]= function($args) use ($controller, $returnType) {
return ['controller' => $controller, 'returnType'=> $returnType, 'args'=> $args];
};
$route->addRoute(strtoupper($definedRoute[0]), $definedRoute[1], $routeName);
$routeIndex++;
}
}
Expand Down

0 comments on commit c704697

Please sign in to comment.