Skip to content

Commit

Permalink
fix: match should return Route class instance
Browse files Browse the repository at this point in the history
  • Loading branch information
papac committed Dec 12, 2024
1 parent 10efe0a commit cc1b019
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 41 deletions.
16 changes: 4 additions & 12 deletions src/Router/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class Route
/**
* The callback has launched if the url of the query has matched.
*
* @var callable
* @var array|string|callable
*/
private mixed $cb;
private array|string|callable $cb;

/**
* The road on the road set by the user
Expand Down Expand Up @@ -124,11 +124,7 @@ public function middleware(array|string $middleware): Route
return $this;
}

if (!isset($this->cb['middleware'])) {
$this->cb['middleware'] = $middleware;
} else {
$this->cb['middleware'] = array_merge((array) $this->cb['middleware'], $middleware);
}
$this->cb['middleware'] = !isset($this->cb['middleware']) ? $middleware : array_merge((array) $this->cb['middleware'], $middleware);

return $this;
}
Expand All @@ -142,11 +138,7 @@ public function middleware(array|string $middleware): Route
*/
public function where(array|string $where, $regex_constraint = null): Route
{
if (is_array($where)) {
$other_rule = $where;
} else {
$other_rule = [$where => $regex_constraint];
}
$other_rule = is_array($where) ? $where : [$where => $regex_constraint];

$this->with = array_merge($this->with, $other_rule);

Expand Down
60 changes: 31 additions & 29 deletions src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,7 @@ public function middleware(array|string $middlewares): Router
$collection = [];

foreach ($middlewares as $middleware) {
if (class_exists($middleware, true)) {
$collection[] = [new $middleware(), 'process'];
} else {
$collection[] = $middleware;
}
$collection[] = class_exists($middleware, true) ? [new $middleware(), 'process'] : $middleware;
}

return new Router($this->method, $this->magic_method, $this->base_route, $collection);
Expand Down Expand Up @@ -341,66 +337,72 @@ public function code(int $code, callable|array|string $cb): Router
* @param callable|string|array $cb
* @return Router
*/
public function match(array $methods, string $path, callable|string|array $cb): Router
public function match(array $methods, string $path, callable|string|array $cb): Route
{
foreach ($methods as $method) {
if ($this->method == strtoupper($method)) {
$this->pushHttpVerb(strtoupper($method), $path, $cb);
}
}
$methods = array_map('strtoupper', $methods);

return $this;
return $this->pushHttpVerb($methods, $path, $cb);
}

/**
* Add other HTTP verbs [PUT, DELETE, UPDATE, HEAD, PATCH]
*
* @param string $method
* @param string|array $methods
* @param string $path
* @param callable|array|string $cb
* @return Route
*/
private function pushHttpVerb(string $method, string $path, callable|string|array $cb): Route
private function pushHttpVerb(string|array $methods, string $path, callable|string|array $cb): Route
{
if ($this->magic_method) {
$methods = (array) $methods;

if (!$this->magic_method) {
return $this->routeLoader($methods, $path, $cb);
}

foreach ($methods as $key => $method) {
if ($this->magic_method === $method) {
$method = $this->magic_method;
$methods[$key] = $this->magic_method;
}
}

return $this->routeLoader($method, $path, $cb);
return $this->routeLoader($methods, $path, $cb);
}

/**
* Start loading a route.
*
* @param string $method
* @param string|array $method
* @param string $path
* @param Callable|string|array $cb
* @param callable|string|array $cb
* @return Route
*/
private function routeLoader(string $method, string $path, callable|string|array $cb): Route
private function routeLoader(string|array $methods, string $path, callable|string|array $cb): Route
{
$methods = (array) $methods;

$path = '/' . trim($path, '/');

// We build the original path based on the Router loader
$path = $this->base_route . $this->prefix . $path;

// We define the current route and current method
$this->current = ['path' => $path, 'method' => $method];

// We add the new route
$route = new Route($path, $cb);

$route->middleware($this->middlewares);

static::$routes[$method][] = $route;
foreach ($methods as $method) {
static::$routes[$method][] = $route;

// We define the current route and current method
$this->current = ['path' => $path, 'method' => $method];

if (
$this->auto_csrf === true
&& in_array($method, ['POST', 'DELETE', 'PUT'])
) {
$route->middleware('csrf');
if (
$this->auto_csrf === true
&& in_array($method, ['POST', 'DELETE', 'PUT'])
) {
$route->middleware('csrf');
}
}

return $route;
Expand Down

0 comments on commit cc1b019

Please sign in to comment.