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, $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($path, $handler); } /** * 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, $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); } /** * Registers routes in an IRouteHandler implementation. * * @param IRouteHandler $handler Routes handler. */ public function register(IRouteHandler $handler): void { $handler->registerRoutes($this); } }