Skip to content

Commit

Permalink
bugfix in router
Browse files Browse the repository at this point in the history
  • Loading branch information
amirfaramarzi committed Sep 23, 2021
1 parent fd49edb commit 7ff13f8
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,12 @@ public function group(array $parameters, Closure $callback): void

$previousGroupMiddleware = $this->currentGroupMiddleware;
if (isset($parameters['middleware'])){
if (is_array($parameters['middleware'])){
array_push($this->currentGroupMiddleware , ...$parameters['middleware']);
}

if (is_string($parameters['middleware'])){
array_push($this->currentGroupMiddleware , $parameters['middleware']);
}
$this->pushToCurrentGroupMiddleware($parameters['middleware']);
}

if (isset($parameters['withoutMiddleware'])){
if (is_array($parameters['withoutMiddleware'])){
$this->currentGroupMiddleware = array_values(array_diff($this->currentGroupMiddleware , $parameters['withoutMiddleware']));
unset($parameters['withoutMiddleware']);
} elseif (is_string($parameters['withoutMiddleware'])){
$this->currentGroupMiddleware = array_values(array_diff($this->currentGroupMiddleware , [$parameters['withoutMiddleware']]));
unset($parameters['withoutMiddleware']);
}
$this->unsetFromCurrentGroupMiddleware($parameters['withoutMiddleware']);
unset($parameters['withoutMiddleware']);
}

$callback($this);
Expand Down Expand Up @@ -115,23 +104,12 @@ protected function addRoute(string $method , string $route , array $callback): v

$previousGroupMiddleware = $this->currentGroupMiddleware;
if (isset($callback['middleware'])) {
if (is_array($callback['middleware'])){
array_push($this->currentGroupMiddleware , ...$callback['middleware']);
}

if (is_string($callback['middleware'])){
array_push($this->currentGroupMiddleware , $callback['middleware']);
}
$this->pushToCurrentGroupMiddleware($callback['middleware']);
}

if (isset($callback['withoutMiddleware'])){
if (is_array($callback['withoutMiddleware'])){
$this->currentGroupMiddleware = array_values(array_diff($this->currentGroupMiddleware , $callback['withoutMiddleware']));
unset($callback['withoutMiddleware']);
} elseif (is_string($callback['withoutMiddleware'])){
$this->currentGroupMiddleware = array_values(array_diff($this->currentGroupMiddleware , [$callback['withoutMiddleware']]));
unset($callback['withoutMiddleware']);
}
$this->unsetFromCurrentGroupMiddleware($callback['withoutMiddleware']);
unset($callback['withoutMiddleware']);
}

$callback['middleware'] = $this->currentGroupMiddleware;
Expand All @@ -140,9 +118,26 @@ protected function addRoute(string $method , string $route , array $callback): v
$this->routes[$method][] = [$route , $callback];
}

protected function pushToCurrentGroupMiddleware(string|array $middleware): void
{
if (is_array($middleware)){
array_push($this->currentGroupMiddleware , ...$middleware);
}

array_push($this->currentGroupMiddleware , $middleware);
}

protected function unsetFromCurrentGroupMiddleware(string|array $middleware): void
{
if (is_array($middleware)){
$this->currentGroupMiddleware = array_values(array_diff($this->currentGroupMiddleware , $middleware));
}

$this->currentGroupMiddleware = array_values(array_diff($this->currentGroupMiddleware , [$middleware]));
}

public function getRoutes(): array
{
return $this->routes;
}

}

0 comments on commit 7ff13f8

Please sign in to comment.