Skip to content

Commit

Permalink
bugfix and new method
Browse files Browse the repository at this point in the history
  • Loading branch information
amirfaramarzi committed Sep 19, 2021
1 parent e030cc6 commit 156a1c3
Showing 1 changed file with 53 additions and 4 deletions.
57 changes: 53 additions & 4 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ public function delete(string $route , array $callback): void
$this->addRoute('DELETE' , $route , $callback);
}

public function head(string $route , array $callback): void
{
$this->addRoute('HEAD' , $route , $callback);
}

public function any(string $route , array $callback): void
{
$this->addRoute('POST' , $route , $callback);
$this->addRoute('GET' , $route , $callback);
$this->addRoute('PATCH' , $route , $callback);
$this->addRoute('PUT' , $route , $callback);
$this->addRoute('DELETE' , $route , $callback);
$this->addRoute('HEAD' , $route , $callback);
}

public function group(array $parameters, Closure $callback): void
{
$previousGroupPrefix = $this->currentGroupPrefix;
Expand All @@ -54,11 +69,27 @@ public function group(array $parameters, Closure $callback): void
}

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

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

if (isset($parameters['withoutMiddleware']) && is_array($parameters['withoutMiddleware'])){
foreach ($parameters['withoutMiddleware'] as $middleware){
if (($key = array_search($middleware, $this->currentGroupMiddleware)) !== false) {
unset($this->currentGroupMiddleware[$key]);
}
}
}

if (isset($parameters['withoutMiddleware']) && is_string($parameters['withoutMiddleware'])){
if (($key = array_search($parameters['withoutMiddleware'], $this->currentGroupMiddleware)) !== false) {
unset($this->currentGroupMiddleware[$key]);
}
}

$callback($this);

Expand All @@ -84,11 +115,29 @@ protected function addRoute(string $method , string $route , array $callback): v
$route = substr($route, 0 ,-1);
}

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

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

if (isset($callback['withoutMiddleware']) && is_array($callback['withoutMiddleware'])){
foreach ($callback['withoutMiddleware'] as $middleware){
if (($key = array_search($middleware, $this->currentGroupMiddleware)) !== false) {
unset($this->currentGroupMiddleware[$key]);
}
}
unset($callback['withoutMiddleware']);
}

if (isset($callback['withoutMiddleware']) && is_string($callback['withoutMiddleware'])){
if (($key = array_search($callback['withoutMiddleware'], $this->currentGroupMiddleware)) !== false) {
unset($this->currentGroupMiddleware[$key]);
}
unset($callback['withoutMiddleware']);
}

$callback['middleware'] = $this->currentGroupMiddleware;

Expand Down

0 comments on commit 156a1c3

Please sign in to comment.