Passed
Branch main (166306)
by Nelson
03:35
created

Load::pageFromUrl()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 36
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
nc 6
nop 1
dl 0
loc 36
rs 9.4222
c 1
b 0
f 0
1
<?php
2
3
namespace Pin\Libs;
4
5
/**
6
 * Load
7
 * Clase con métodos estáticos para cargar archivos, helpers, vistas y manejar
8
 * el enrutamiento dinámico antiguo si es necesario.
9
 */
10
class Load
11
{
12
    public static function file($file, $extension, $base_dir, $classification, $parameters = null)
13
    {
14
        if (isset($parameters) && is_array($parameters)) {
15
            extract($parameters);
16
        }
17
        $path = $base_dir . $file . $extension;
18
        if (file_exists($path)) {
19
            require $path;
20
        } else {
21
            throw (new \Exception("$classification <b>$file</b> no existe!"));
22
        }
23
    }
24
25
    public static function lib($lib)
26
    {
27
        self::file($lib, ".php", PIN_PATH . 'libs' . DS, "Librería");
28
    }
29
30
    public static function config($config)
31
    {
32
        self::file($config, ".php", PIN_PATH . 'config' . DS, "Configuración");
33
    }
34
35
    public static function helper($helper)
36
    {
37
        self::file($helper, ".php", PIN_PATH . 'helpers' . DS, "Helper");
38
    }
39
40
    public static function view($view, $parameters = null)
41
    {
42
        self::file($view, ".phtml", PIN_PATH . 'views' . DS, "Vista", $parameters);
43
    }
44
45
    public static function partial($partial, $parameters = null)
46
    {
47
        self::file($partial, ".phtml", PIN_PATH . 'partials' . DS, "Parcial", $parameters);
48
    }
49
50
    public static function redirect($url)
51
    {
52
        $url = PUBLIC_PATH . $url;
53
        header("Location: $url", true, 301);
54
        exit;
55
    }
56
57
    // --- Funciones del router dinámico previo (compatibilidad) ---
58
    public static function pageFromUrl($url)
59
    {
60
        // Validar entrada
61
        if (!is_string($url)) {
62
            throw new \InvalidArgumentException('URL must be a string');
63
        }
64
65
        // Extraer componentes de la URL
66
        $components = array_values(array_filter(explode('/', $url)));
67
68
        // Determinar página y función
69
        $page = $components[0] ?? 'default';
70
        $function = $components[1] ?? 'index';
71
        $params = array_slice($components, 2);
72
73
        // Validar existencia de la página
74
        $page_path = PIN_PATH . 'pages' . DS . $page . '.php';
75
        if (!file_exists($page_path)) {
76
            throw new \Exception("La página <b>$page</b> no existe!");
77
        }
78
79
        require $page_path;
80
81
        // Ejecutar inicializador si existe
82
        if (function_exists('page_initializer')) {
83
            page_initializer();
84
        }
85
86
        // Determinar la función a ejecutar
87
        $function_to_call = self::determineFunctionToCall($function);
88
89
        if (empty($function_to_call)) {
90
            throw new \Exception("La función <b>$function</b> no existe en la página <b>$page</b>!");
91
        }
92
93
        return call_user_func_array($function_to_call, $params);
94
    }
95
96
    public static function determineFunctionToCall($function)
97
    {
98
        if (function_exists($function)) {
99
            return $function;
100
        }
101
102
        $request_method = strtolower($_SERVER['REQUEST_METHOD']);
103
        if (function_exists($request_method)) {
104
            return $request_method;
105
        }
106
107
        return null;
108
    }
109
}
110
111