Add missing docblocks and return types.

This commit is contained in:
kj
2023-03-25 12:26:42 -04:00
parent 1bde430251
commit 3c8a21161f
4 changed files with 64 additions and 29 deletions

View File

@ -19,16 +19,25 @@ class Router {
private static $put = [];
private static $delete = [];
private static $last;
public static $notFoundCallback = 'Libs\Router::defaultNotFound';
public static $notFoundCallback = 'Libs\Router::defaultNotFound';
public static function defaultNotFound () {
/**
* Función callback por defectio para cuando
* no se encuentra configurada la ruta.
*
* @return void
*/
public static function defaultNotFound (): void {
header("HTTP/1.0 404 Not Found");
echo '<h2 style="text-align: center;margin: 25px 0px;">Error 404 - Página no encontrada</h2>';
}
/**
* __construct
*/
private function __construct() {}
/*
/**
* Parsea para deectar las pseudovariables (ej: {variable})
*
* @param string $path
@ -42,7 +51,7 @@ class Router {
* path - Contiene la ruta con las pseudovariables reeplazadas por expresiones regulares.
* callback - Contiene el callback en formato Namespace\Clase::Método.
*/
private static function parse(string $path, $callback) : array {
private static function parse(string $path, $callback): array {
preg_match_all('/{(\w+)}/s', $path, $matches, PREG_PATTERN_ORDER);
$paramNames = $matches[1];
@ -64,7 +73,7 @@ class Router {
}
/*
/**
* Devuelve el ruta base o raiz del proyecto sobre la que trabajará el router.
*
* Ej: Si la url del sistema está en "https://ejemplo.com/duckbrain"
@ -72,13 +81,13 @@ class Router {
*
* @return string
*/
public static function basePath() : string {
public static function basePath(): string {
if (defined('SITE_URL'))
return parse_url(SITE_URL, PHP_URL_PATH);
return str_replace($_SERVER['DOCUMENT_ROOT'], '/', ROOT_DIR);
}
/*
/**
* Redirije a una ruta relativa interna.
*
* @param string $path
@ -87,8 +96,9 @@ class Router {
* Ej: Si nuesto sistema está en "https://ejemplo.com/duckbrain"
* llamamos a Router::redirect('/docs'), entonces seremos
* redirigidos a "https://ejemplo.com/duckbrain/docs".
* @return void
*/
public static function redirect(string $path) {
public static function redirect(string $path): void {
header('Location: '.static::basePath().substr($path,1));
}
@ -103,7 +113,7 @@ class Router {
* Devuelve un enlace estático.
*/
public static function middleware($callback, int $priority = null) : Router {
public static function middleware($callback, int $priority = null): Router {
if (!isset(static::$last))
return new static();
@ -138,7 +148,7 @@ class Router {
* json - Donde se encuentran los valores JSON enviados en el body.
*
*/
private static function getReq() : Neuron {
private static function getReq(): Neuron {
$req = new Neuron();
$req->get = new Neuron($_GET);
$req->post = new Neuron($_POST);
@ -152,7 +162,7 @@ class Router {
* @return object
* Devuelve un objeto con los datos recibidos en JSON.
*/
private static function get_json() : object {
private static function get_json(): object {
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if ($contentType === "application/json") {
return json_decode(trim(file_get_contents("php://input")));
@ -172,7 +182,7 @@ class Router {
* @return Router
* Devuelve un enlace estático.
*/
public static function get(string $path, $callback) {
public static function get(string $path, $callback): Router {
static::$get[] = static::parse($path, $callback);
static::$last = ['get', count(static::$get)-1];
return new static();
@ -190,7 +200,7 @@ class Router {
* @return Router
* Devuelve un enlace estático.
*/
public static function post(string $path, $callback) : Router {
public static function post(string $path, $callback): Router {
static::$post[] = static::parse($path, $callback);
static::$last = ['post', count(static::$post)-1];
return new static();
@ -209,7 +219,7 @@ class Router {
* Devuelve un enlace estático
*/
public static function put(string $path, $callback) : Router {
public static function put(string $path, $callback): Router {
static::$put[] = static::parse($path, $callback);
static::$last = ['put', count(static::$put)-1];
return new static();
@ -227,7 +237,7 @@ class Router {
* @return static
* Devuelve un enlace estático
*/
public static function delete(string $path, $callback) : Router {
public static function delete(string $path, $callback): Router {
static::$delete[] = static::parse($path, $callback);
static::$last = ['delete', count(static::$delete)-1];
return new static();
@ -261,8 +271,9 @@ class Router {
* $req es una instancia de Neuron que tiene los datos de la petición.
*
* Si no la ruta no coincide con ninguna de las rutas configuradas, ejecutará el callback $notFoundCallback
* @return void
*/
public static function apply() {
public static function apply(): void {
$path = static::currentPath();
$routers = [];
switch ($_SERVER['REQUEST_METHOD']){ // Según el método selecciona un arreglo de routers configurados