index/src/Routing/Router.php

133 lines
3.5 KiB
PHP
Raw Normal View History

2022-09-13 13:13:11 +00:00
<?php
// Router.php
// Created: 2022-01-18
// Updated: 2022-02-27
namespace Index\Routing;
use InvalidArgumentException;
/**
* Provides an application router.
*/
class Router {
private RouteInfo $route;
/**
* Constructs a Router object.
*/
public function __construct() {
$this->route = new RouteInfo;
}
/**
* Resolves a request method and uri.
*
* @param string $method Request method.
* @param string $path Request path.
* @param array $args Arguments to be passed on to the callables.
* @return RouteCallable A collection of callables representing the route.
*/
public function resolve(string $method, string $path, array $args = []): RouteCallable {
$method = strtolower($method);
if($method === 'head')
$method = 'get';
return $this->route->resolve($method, trim($path, '/'), $args);
}
/**
* Apply middleware functions to a path.
*
* @param string $path Path to apply the middleware to.
* @param callable $handler Middleware function.
*/
public function use(string $path, callable $handler): void {
$this->route->addMiddleware(trim($path, '/'), $handler);
}
/**
* Merges another router with this one with possibility of changing its root.
*
* @param string $path Base path to use.
* @param Router $router Router object to inherit from.
*/
public function merge(string $path, Router $router): void {
$this->route->mergeRoute(trim($path, '/'), $router->route);
}
/**
* Adds a new route.
*
* @param string $method Request method.
* @param string $path Request path.
* @param callable $handler Request handler.
*/
public function add(string $method, string $path, callable $handler): void {
if(empty($method))
throw new InvalidArgumentException('$method may not be empty.');
$this->route->addMethod($method, trim($path, '/'), $handler);
}
/**
* Adds a new GET route.
*
* @param string $path Request path.
* @param callable $handler Request handler.
*/
public function get(string $path, callable $handler): void {
$this->add('get', $path, $handler);
}
/**
* Adds a new POST route.
*
* @param string $path Request path.
* @param callable $handler Request handler.
*/
public function post(string $path, callable $handler): void {
$this->add('post', $path, $handler);
}
/**
* Adds a new DELETE route.
*
* @param string $path Request path.
* @param callable $handler Request handler.
*/
public function delete(string $path, callable $handler): void {
$this->add('delete', $path, $handler);
}
/**
* Adds a new PATCH route.
*
* @param string $path Request path.
* @param callable $handler Request handler.
*/
public function patch(string $path, callable $handler): void {
$this->add('patch', $path, $handler);
}
/**
* Adds a new PUT route.
*
* @param string $path Request path.
* @param callable $handler Request handler.
*/
public function put(string $path, callable $handler): void {
$this->add('put', $path, $handler);
}
/**
* Adds a new OPTIONS route.
*
* @param string $path Request path.
* @param callable $handler Request handler.
*/
public function options(string $path, callable $handler): void {
$this->add('options', $path, $handler);
}
}