diff --git a/src/Router.php b/src/Router.php index 9a47053..58f73d4 100644 --- a/src/Router.php +++ b/src/Router.php @@ -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; @@ -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); @@ -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;