Skip to content

Commit

Permalink
Added the ability to set defaults and requirements to routes
Browse files Browse the repository at this point in the history
  • Loading branch information
simba77 committed Feb 4, 2024
1 parent a608977 commit 776a9b0
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions system/src/Router/Route.php
Expand Up @@ -12,6 +12,8 @@ final class Route
private ?string $name = null;
private array $middlewares = [];
private int $priority = 0;
private array $defaults = [];
private array $requirements = [];

public function __construct(array | string $method, string $path, mixed $handler)
{
Expand Down Expand Up @@ -60,13 +62,30 @@ public function setMiddlewares(array $middlewares): Route
return $this;
}

public function setDefaults(array $defaults): Route
{
$this->defaults = $defaults;
return $this;
}

public function setRequirements(array $requirements): Route
{
$this->requirements = $requirements;
return $this;
}

public function compile(): \Symfony\Component\Routing\Route
{
$compiledRoute = new \Symfony\Component\Routing\Route($this->path, [
'_controller' => $this->handler,
'_middlewares' => $this->middlewares,
'_name' => $this->name,
]);
$compiledRoute = new \Symfony\Component\Routing\Route(
$this->path,
[
'_controller' => $this->handler,
'_middlewares' => $this->middlewares,
'_name' => $this->name,
...$this->defaults,
],
$this->requirements
);
$compiledRoute->setMethods($this->method);

return $compiledRoute;
Expand Down

0 comments on commit 776a9b0

Please sign in to comment.