chore: complete update
This commit is contained in:
@@ -2,24 +2,63 @@
|
||||
|
||||
namespace App\Framework\Router;
|
||||
|
||||
final class RouteCollection
|
||||
use App\Framework\Core\Route;
|
||||
use App\Framework\Http\Method;
|
||||
use Countable;
|
||||
use Generator;
|
||||
|
||||
final readonly class RouteCollection implements Countable
|
||||
{
|
||||
/** @var array<string, array{static: array<string, callable>, dynamic: array<array{regex: string, handler: callable}>}> */
|
||||
private array $routes;
|
||||
/**
|
||||
* @param array<string, array{static: array<string, callable>, dynamic: array<array{regex: string, handler: callable}>}> $routes
|
||||
#* @param array<string, array{path: string, method: Method, handler: callable}> $namedRoutes
|
||||
*/
|
||||
public function __construct(
|
||||
private array $routes,
|
||||
private array $namedRoutes = []
|
||||
) {}
|
||||
|
||||
public function __construct(array $routes) {
|
||||
$this->routes = $routes;
|
||||
public function getStatic(Method $method): array {
|
||||
return $this->routes[$method->value]['static'] ?? [];
|
||||
}
|
||||
|
||||
public function getStatic(string $method): array {
|
||||
return $this->routes[$method]['static'] ?? [];
|
||||
public function getDynamic(Method $method): array {
|
||||
return $this->routes[$method->value]['dynamic'] ?? [];
|
||||
}
|
||||
|
||||
public function getDynamic(string $method): array {
|
||||
return $this->routes[$method]['dynamic'] ?? [];
|
||||
public function has(Method $method): bool {
|
||||
return isset($this->routes[$method->value]);
|
||||
}
|
||||
|
||||
public function has(string $method): bool {
|
||||
return isset($this->routes[$method]);
|
||||
public function getByMethod(Method $method): array {
|
||||
|
||||
return ($this->routes[$method->value]['dynamic'] + $this->routes[$method->value]['static']) ?? [];
|
||||
}
|
||||
|
||||
public function getByName(string $name): ?Route
|
||||
{
|
||||
return $this->namedRoutes[$name] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt alle Named Routes zurück
|
||||
*
|
||||
* @return Generator<string, Route>
|
||||
*/
|
||||
public function getAllNamedRoutes(): Generator
|
||||
{
|
||||
foreach ($this->namedRoutes as $name => $route) {
|
||||
yield $name => $route;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function hasName(string $name): bool {
|
||||
return isset($this->namedRoutes[$name]);
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return count($this->routes);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user