Added interface for common router methods.
This commit is contained in:
parent
6e09904013
commit
f8c6602ab9
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
// HttpFx.php
|
// HttpFx.php
|
||||||
// Created: 2022-02-15
|
// Created: 2022-02-15
|
||||||
// Updated: 2022-02-27
|
// Updated: 2023-01-06
|
||||||
|
|
||||||
namespace Index\Http;
|
namespace Index\Http;
|
||||||
|
|
||||||
|
@ -16,12 +16,13 @@ use Index\Http\Content\BencodedContent;
|
||||||
use Index\Http\Content\JsonContent;
|
use Index\Http\Content\JsonContent;
|
||||||
use Index\Http\Content\StreamContent;
|
use Index\Http\Content\StreamContent;
|
||||||
use Index\Http\Content\StringContent;
|
use Index\Http\Content\StringContent;
|
||||||
|
use Index\Routing\IRouter;
|
||||||
use Index\Routing\Router;
|
use Index\Routing\Router;
|
||||||
use Index\Routing\RoutePathNotFoundException;
|
use Index\Routing\RoutePathNotFoundException;
|
||||||
use Index\Routing\RouteMethodNotSupportedException;
|
use Index\Routing\RouteMethodNotSupportedException;
|
||||||
use Index\Serialisation\IBencodeSerialisable;
|
use Index\Serialisation\IBencodeSerialisable;
|
||||||
|
|
||||||
class HttpFx {
|
class HttpFx implements IRouter {
|
||||||
private Router $router;
|
private Router $router;
|
||||||
private array $objectHandlers = [];
|
private array $objectHandlers = [];
|
||||||
private array $errorHandlers = [];
|
private array $errorHandlers = [];
|
||||||
|
|
81
src/Routing/IRouter.php
Normal file
81
src/Routing/IRouter.php
Normal 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;
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
// Router.php
|
// Router.php
|
||||||
// Created: 2022-01-18
|
// Created: 2022-01-18
|
||||||
// Updated: 2022-02-27
|
// Updated: 2023-01-06
|
||||||
|
|
||||||
namespace Index\Routing;
|
namespace Index\Routing;
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ use InvalidArgumentException;
|
||||||
/**
|
/**
|
||||||
* Provides an application router.
|
* Provides an application router.
|
||||||
*/
|
*/
|
||||||
class Router {
|
class Router implements IRouter {
|
||||||
private RouteInfo $route;
|
private RouteInfo $route;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue