Added interface for common router methods.

This commit is contained in:
flash 2023-01-06 20:20:59 +00:00
parent 6e09904013
commit f8c6602ab9
4 changed files with 87 additions and 5 deletions

View File

@ -1 +1 @@
0.2301.50234
0.2301.62020

View File

@ -1,7 +1,7 @@
<?php
// HttpFx.php
// Created: 2022-02-15
// Updated: 2022-02-27
// Updated: 2023-01-06
namespace Index\Http;
@ -16,12 +16,13 @@ use Index\Http\Content\BencodedContent;
use Index\Http\Content\JsonContent;
use Index\Http\Content\StreamContent;
use Index\Http\Content\StringContent;
use Index\Routing\IRouter;
use Index\Routing\Router;
use Index\Routing\RoutePathNotFoundException;
use Index\Routing\RouteMethodNotSupportedException;
use Index\Serialisation\IBencodeSerialisable;
class HttpFx {
class HttpFx implements IRouter {
private Router $router;
private array $objectHandlers = [];
private array $errorHandlers = [];

81
src/Routing/IRouter.php Normal file
View File

@ -0,0 +1,81 @@
<?php
// IRouter.php
// Created: 2023-01-06
// Updated: 2023-01-06
namespace Index\Routing;
interface IRouter {
/**
* 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;
/**
* 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;
/**
* 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;
/**
* Adds a new GET route.
*
* @param string $path Request path.
* @param callable $handler Request handler.
*/
public function get(string $path, callable $handler): void;
/**
* Adds a new POST route.
*
* @param string $path Request path.
* @param callable $handler Request handler.
*/
public function post(string $path, callable $handler): void;
/**
* Adds a new DELETE route.
*
* @param string $path Request path.
* @param callable $handler Request handler.
*/
public function delete(string $path, callable $handler): void;
/**
* Adds a new PATCH route.
*
* @param string $path Request path.
* @param callable $handler Request handler.
*/
public function patch(string $path, callable $handler): void;
/**
* Adds a new PUT route.
*
* @param string $path Request path.
* @param callable $handler Request handler.
*/
public function put(string $path, callable $handler): void;
/**
* Adds a new OPTIONS route.
*
* @param string $path Request path.
* @param callable $handler Request handler.
*/
public function options(string $path, callable $handler): void;
}

View File

@ -1,7 +1,7 @@
<?php
// Router.php
// Created: 2022-01-18
// Updated: 2022-02-27
// Updated: 2023-01-06
namespace Index\Routing;
@ -10,7 +10,7 @@ use InvalidArgumentException;
/**
* Provides an application router.
*/
class Router {
class Router implements IRouter {
private RouteInfo $route;
/**