-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,10 +27,8 @@ final class Group | |
* @var string[] | ||
*/ | ||
private array $hosts = []; | ||
private ?string $namePrefix = null; | ||
private bool $routesAdded = false; | ||
private bool $middlewareAdded = false; | ||
private array $disabledMiddlewares = []; | ||
|
||
/** | ||
* @psalm-var list<array|callable|string>|null | ||
|
@@ -42,9 +40,24 @@ final class Group | |
*/ | ||
private $corsMiddleware = null; | ||
|
||
private function __construct( | ||
private ?string $prefix = null | ||
/** | ||
* @param array $disabledMiddlewares Excludes middleware from being invoked when action is handled. | ||
* It is useful to avoid invoking one of the parent group middleware for | ||
* a certain route. | ||
*/ | ||
public function __construct( | ||
private ?string $prefix = null, | ||
array $middlewares = [], | ||
array $hosts = [], | ||
private ?string $namePrefix = null, | ||
private array $disabledMiddlewares = [], | ||
array|callable|string|null $corsMiddleware = null | ||
) { | ||
$this->assertMiddlewares($middlewares); | ||
$this->assertHosts($hosts); | ||
$this->middlewares = $middlewares; | ||
$this->hosts = $hosts; | ||
$this->corsMiddleware = $corsMiddleware; | ||
} | ||
|
||
/** | ||
|
@@ -215,4 +228,33 @@ private function getEnabledMiddlewares(): array | |
|
||
return $this->enabledMiddlewaresCache; | ||
} | ||
|
||
/** | ||
* @psalm-assert array<string> $hosts | ||
*/ | ||
private function assertHosts(array $hosts): void | ||
{ | ||
foreach ($hosts as $host) { | ||
if (!is_string($host)) { | ||
throw new \InvalidArgumentException('Invalid $hosts provided, list of string expected.'); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @psalm-assert list<array|callable|string> $middlewares | ||
*/ | ||
private function assertMiddlewares(array $middlewares): void | ||
{ | ||
/** @var mixed $middleware */ | ||
foreach ($middlewares as $middleware) { | ||
if (is_string($middleware) || is_callable($middleware) || is_array($middleware)) { | ||
Check warning on line 251 in src/Group.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
Check warning on line 251 in src/Group.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
Check warning on line 251 in src/Group.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
Check warning on line 251 in src/Group.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
|
||
continue; | ||
} | ||
|
||
throw new \InvalidArgumentException( | ||
'Invalid $middlewares provided, list of string or array or callable expected.' | ||
); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,15 @@ | |
*/ | ||
final class Route implements Stringable | ||
{ | ||
private ?string $name = null; | ||
/** | ||
* @var string[] | ||
*/ | ||
private array $methods = []; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private array $hosts = []; | ||
private bool $override = false; | ||
private bool $actionAdded = false; | ||
|
||
/** | ||
|
@@ -32,25 +34,50 @@ final class Route implements Stringable | |
*/ | ||
private array $middlewares = []; | ||
|
||
private array $disabledMiddlewares = []; | ||
|
||
/** | ||
* @psalm-var list<array|callable|string>|null | ||
*/ | ||
private ?array $enabledMiddlewaresCache = null; | ||
|
||
/** | ||
* @var array<string,string> | ||
* @var array<string,scalar|Stringable|null> | ||
*/ | ||
private array $defaults = []; | ||
|
||
/** | ||
* @param string[] $methods | ||
* @param array|callable|string|null $action Action handler. It is a primary middleware definition that | ||
* should be invoked last for a matched route. | ||
* @param array<string,scalar|Stringable|null> $defaults Parameter default values indexed by parameter names. | ||
* @param bool $override Marks route as override. When added it will replace existing route with the same name. | ||
* @param array $disabledMiddlewares Excludes middleware from being invoked when action is handled. | ||
* It is useful to avoid invoking one of the parent group middleware for | ||
* a certain route. | ||
*/ | ||
private function __construct( | ||
private array $methods, | ||
public function __construct( | ||
array $methods, | ||
private string $pattern, | ||
private ?string $name = null, | ||
array|callable|string $action = null, | ||
array $middlewares = [], | ||
array $defaults = [], | ||
array $hosts = [], | ||
private bool $override = false, | ||
private array $disabledMiddlewares = [], | ||
) { | ||
if (empty($methods)) { | ||
throw new InvalidArgumentException('$methods cannot be empty.'); | ||
} | ||
$this->assertListOfStrings($methods, 'methods'); | ||
$this->assertMiddlewares($middlewares); | ||
$this->assertListOfStrings($hosts, 'hosts'); | ||
$this->methods = $methods; | ||
$this->middlewares = $middlewares; | ||
$this->hosts = $hosts; | ||
$this->defaults = array_map('\strval', $defaults); | ||
Check warning on line 76 in src/Route.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
|
||
if (!empty($action)) { | ||
$this->middlewares[] = $action; | ||
$this->actionAdded = true; | ||
Check warning on line 79 in src/Route.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
|
||
} | ||
} | ||
|
||
public static function get(string $pattern): self | ||
|
@@ -93,7 +120,7 @@ public static function options(string $pattern): self | |
*/ | ||
public static function methods(array $methods, string $pattern): self | ||
{ | ||
return new self($methods, $pattern); | ||
return new self(methods: $methods, pattern: $pattern); | ||
} | ||
|
||
public function name(string $name): self | ||
|
@@ -271,7 +298,7 @@ public function __toString(): string | |
$result .= implode(',', $this->methods) . ' '; | ||
} | ||
|
||
if ($this->hosts) { | ||
if (!empty($this->hosts)) { | ||
$quoted = array_map(static fn ($host) => preg_quote($host, '/'), $this->hosts); | ||
|
||
if (!preg_match('/' . implode('|', $quoted) . '/', $this->pattern)) { | ||
|
@@ -314,4 +341,37 @@ private function getEnabledMiddlewares(): array | |
|
||
return $this->enabledMiddlewaresCache; | ||
} | ||
|
||
/** | ||
* @psalm-assert array<string> $items | ||
*/ | ||
private function assertListOfStrings(array $items, string $argument): void | ||
{ | ||
foreach ($items as $item) { | ||
if (!is_string($item)) { | ||
throw new \InvalidArgumentException('Invalid $' . $argument . ' provided, list of string expected.'); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @psalm-assert list<array|callable|string> $middlewares | ||
*/ | ||
private function assertMiddlewares(array $middlewares): void | ||
{ | ||
/** @var mixed $middleware */ | ||
foreach ($middlewares as $middleware) { | ||
if (is_string($middleware)) { | ||
continue; | ||
Check warning on line 365 in src/Route.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
|
||
} | ||
|
||
if (is_callable($middleware) || is_array($middleware)) { | ||
Check warning on line 368 in src/Route.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
Check warning on line 368 in src/Route.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
Check warning on line 368 in src/Route.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
|
||
continue; | ||
} | ||
|
||
throw new \InvalidArgumentException( | ||
'Invalid $middlewares provided, list of string or array or callable expected.' | ||
); | ||
} | ||
} | ||
} |