Reorganize code for PSR and others code prettify.
This commit is contained in:
@ -28,7 +28,8 @@ class Router {
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function defaultNotFound (): 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>';
|
||||
}
|
||||
@ -52,7 +53,8 @@ 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, callable $callback): array {
|
||||
private static function parse(string $path, callable $callback): array
|
||||
{
|
||||
preg_match_all('/{(\w+)}/s', $path, $matches, PREG_PATTERN_ORDER);
|
||||
$paramNames = $matches[1];
|
||||
|
||||
@ -78,7 +80,8 @@ 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);
|
||||
@ -95,7 +98,8 @@ class Router {
|
||||
* redirigidos a "https://ejemplo.com/duckbrain/docs".
|
||||
* @return void
|
||||
*/
|
||||
public static function redirect(string $path): void {
|
||||
public static function redirect(string $path): void
|
||||
{
|
||||
header('Location: '.static::basePath().substr($path,1));
|
||||
exit;
|
||||
}
|
||||
@ -110,7 +114,8 @@ class Router {
|
||||
* @return static
|
||||
* Devuelve la instancia actual.
|
||||
*/
|
||||
public static function middleware(callable $callback, int $priority = null): static {
|
||||
public static function middleware(callable $callback, int $priority = null): static
|
||||
{
|
||||
if (!isset(static::$last))
|
||||
return new static();
|
||||
|
||||
@ -140,7 +145,8 @@ class Router {
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function reconfigure(callable $callback): static {
|
||||
public static function reconfigure(callable $callback): static
|
||||
{
|
||||
if (empty(static::$last))
|
||||
return new static();
|
||||
|
||||
@ -168,7 +174,8 @@ class Router {
|
||||
* @return
|
||||
* Devuelve la instancia actual.
|
||||
*/
|
||||
public static function configure(string $method, string $path, ?callable $callback = null): static {
|
||||
public static function configure(string $method, string $path, ?callable $callback = null): static
|
||||
{
|
||||
if (is_null($callback)) {
|
||||
$path = preg_quote($path, '/');
|
||||
$path = preg_replace(
|
||||
@ -201,7 +208,8 @@ class Router {
|
||||
* @return static
|
||||
* Devuelve la instancia actual.
|
||||
*/
|
||||
public static function get(string $path, callable $callback = null): static {
|
||||
public static function get(string $path, callable $callback = null): static
|
||||
{
|
||||
return static::configure('get', $path, $callback);
|
||||
}
|
||||
|
||||
@ -216,8 +224,9 @@ class Router {
|
||||
* @return static
|
||||
* Devuelve la instancia actual.
|
||||
*/
|
||||
public static function post(string $path, callable $callback = null): static {
|
||||
return static::configure('post', $path, $callback);
|
||||
public static function post(string $path, callable $callback = null): static
|
||||
{
|
||||
return static::configure('post', $path, $callback);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -232,7 +241,8 @@ class Router {
|
||||
* Devuelve la instancia actual
|
||||
*/
|
||||
|
||||
public static function put(string $path, callable $callback = null): static {
|
||||
public static function put(string $path, callable $callback = null): static
|
||||
{
|
||||
return static::configure('put', $path, $callback);
|
||||
}
|
||||
|
||||
@ -247,7 +257,8 @@ class Router {
|
||||
* @return static
|
||||
* Devuelve la instancia actual
|
||||
*/
|
||||
public static function patch(string $path, callable $callback = null): static {
|
||||
public static function patch(string $path, callable $callback = null): static
|
||||
{
|
||||
return static::configure('patch', $path, $callback);
|
||||
}
|
||||
|
||||
@ -262,7 +273,8 @@ class Router {
|
||||
* @return static
|
||||
* Devuelve la instancia actual
|
||||
*/
|
||||
public static function delete(string $path, callable $callback = null): static {
|
||||
public static function delete(string $path, callable $callback = null): static
|
||||
{
|
||||
return static::configure('delete', $path, $callback);
|
||||
}
|
||||
|
||||
@ -271,7 +283,8 @@ class Router {
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function currentPath() : string {
|
||||
public static function currentPath() : string
|
||||
{
|
||||
return preg_replace('/'.preg_quote(static::basePath(), '/').'/',
|
||||
'/', strtok($_SERVER['REQUEST_URI'], '?'), 1);
|
||||
}
|
||||
@ -296,7 +309,8 @@ class Router {
|
||||
* Si no la ruta no coincide con ninguna de las rutas configuradas, ejecutará el callback $notFoundCallback
|
||||
* @return void
|
||||
*/
|
||||
public static function apply(): void {
|
||||
public static function apply(): void
|
||||
{
|
||||
$path = static::currentPath();
|
||||
$routers = match($_SERVER['REQUEST_METHOD']) { // Según el método selecciona un arreglo de routers configurados
|
||||
'POST' => static::$post,
|
||||
|
Reference in New Issue
Block a user