@@ -2,60 +2,60 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | function load_file($file, $extension, $base_dir, $classification, $parameters = null) |
| 4 | 4 | { |
| 5 | - if (isset($parameters) && is_array($parameters)) { |
|
| 6 | - extract($parameters); |
|
| 7 | - } |
|
| 8 | - if (file_exists($base_dir . $file . $extension)) { |
|
| 9 | - require $base_dir . $file . $extension; |
|
| 10 | - } else { |
|
| 11 | - throw (new Exception("$classification <b>$file</b> no existe!")); |
|
| 12 | - } |
|
| 5 | + if (isset($parameters) && is_array($parameters)) { |
|
| 6 | + extract($parameters); |
|
| 7 | + } |
|
| 8 | + if (file_exists($base_dir . $file . $extension)) { |
|
| 9 | + require $base_dir . $file . $extension; |
|
| 10 | + } else { |
|
| 11 | + throw (new Exception("$classification <b>$file</b> no existe!")); |
|
| 12 | + } |
|
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | function load_lib($lib) |
| 16 | 16 | { |
| 17 | - load_file($lib, ".php", PIN_PATH . 'libs' . DS, "Librería"); |
|
| 17 | + load_file($lib, ".php", PIN_PATH . 'libs' . DS, "Librería"); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | function load_config($config) |
| 21 | 21 | { |
| 22 | - load_file($config, ".php", PIN_PATH . 'config' . DS, "Configuración"); |
|
| 22 | + load_file($config, ".php", PIN_PATH . 'config' . DS, "Configuración"); |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | |
| 26 | 26 | function load_helper($helper) |
| 27 | 27 | { |
| 28 | - load_file($helper, ".php", PIN_PATH . 'helpers' . DS, "Helper"); |
|
| 28 | + load_file($helper, ".php", PIN_PATH . 'helpers' . DS, "Helper"); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | function load_view($view, array $parameters = null) |
| 32 | 32 | { |
| 33 | - load_file($view, ".phtml", PIN_PATH . 'views' . DS, "Vista", $parameters); |
|
| 33 | + load_file($view, ".phtml", PIN_PATH . 'views' . DS, "Vista", $parameters); |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | function load_partial($partial, array $parameters = null) |
| 37 | 37 | { |
| 38 | - load_file($partial, ".phtml", PIN_PATH . 'partials'. DS , "Parcial", $parameters); |
|
| 38 | + load_file($partial, ".phtml", PIN_PATH . 'partials'. DS , "Parcial", $parameters); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | function redirect_to($url) |
| 42 | 42 | { |
| 43 | - $url = PUBLIC_PATH . $url; |
|
| 44 | - header("Location: $url", true, 301); |
|
| 43 | + $url = PUBLIC_PATH . $url; |
|
| 44 | + header("Location: $url", true, 301); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | //implementar autoloader para clases |
| 48 | 48 | //espera que las clases se definan en PascalCase y los archivos |
| 49 | 49 | //usen snake_case. Ejemplo class QueryBuilder, archivo query_builder.php |
| 50 | 50 | spl_autoload_register(function($className){ |
| 51 | - $file_name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $className)); |
|
| 52 | - $file = PIN_PATH . 'libs' . DS . $file_name . '.php'; |
|
| 53 | - if (file_exists($file)) { |
|
| 54 | - require_once $file; |
|
| 55 | - return; |
|
| 56 | - } else { |
|
| 57 | - throw new Exception("$className no existe en $file", 1); |
|
| 58 | - } |
|
| 51 | + $file_name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $className)); |
|
| 52 | + $file = PIN_PATH . 'libs' . DS . $file_name . '.php'; |
|
| 53 | + if (file_exists($file)) { |
|
| 54 | + require_once $file; |
|
| 55 | + return; |
|
| 56 | + } else { |
|
| 57 | + throw new Exception("$className no existe en $file", 1); |
|
| 58 | + } |
|
| 59 | 59 | }); |
| 60 | 60 | |
| 61 | 61 | |
@@ -79,54 +79,54 @@ discard block |
||
| 79 | 79 | |
| 80 | 80 | if (error_reporting() !== 0) { |
| 81 | 81 | echo "<div style='padding: 40px;'>"; |
| 82 | - echo "<h1>Fatal error</h1>"; |
|
| 82 | + echo "<h1>Fatal error</h1>"; |
|
| 83 | 83 | echo "<p>Uncaught exception: '" . get_class($exception) . "'</p>"; |
| 84 | 84 | echo "<p>Message: '" . $exception->getMessage() . "'</p>"; |
| 85 | 85 | echo "<p>Stack trace:<pre>" . $exception->getTraceAsString() . "</pre></p>"; |
| 86 | 86 | echo "<p>Thrown in '" . $exception->getFile() . "' on line " . $exception->getLine() . "</p>"; |
| 87 | - echo "</div>"; |
|
| 87 | + echo "</div>"; |
|
| 88 | 88 | } |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | |
| 92 | 92 | function load_page_from_url($url) |
| 93 | 93 | { |
| 94 | - $content = explode('/', $url); |
|
| 94 | + $content = explode('/', $url); |
|
| 95 | 95 | //quitar el elemento inicial vacio |
| 96 | 96 | array_shift($content); |
| 97 | 97 | |
| 98 | - $page = !empty($content[0]) ? trim($content[0]) : 'default'; |
|
| 99 | - array_shift($content); |
|
| 98 | + $page = !empty($content[0]) ? trim($content[0]) : 'default'; |
|
| 99 | + array_shift($content); |
|
| 100 | 100 | |
| 101 | - if (file_exists(PIN_PATH . 'pages' . DS . $page . '.php')) { |
|
| 102 | - require PIN_PATH . 'pages' . DS . $page . '.php'; |
|
| 103 | - } else { |
|
| 104 | - throw (new Exception("La página <b>$page</b> no existe!")); |
|
| 105 | - } |
|
| 101 | + if (file_exists(PIN_PATH . 'pages' . DS . $page . '.php')) { |
|
| 102 | + require PIN_PATH . 'pages' . DS . $page . '.php'; |
|
| 103 | + } else { |
|
| 104 | + throw (new Exception("La página <b>$page</b> no existe!")); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - $function_to_be_load = !empty($content[0]) ? trim($content[0]) : 'index'; |
|
| 108 | - array_shift($content); |
|
| 107 | + $function_to_be_load = !empty($content[0]) ? trim($content[0]) : 'index'; |
|
| 108 | + array_shift($content); |
|
| 109 | 109 | |
| 110 | - if (function_exists("page_initializer")) { |
|
| 111 | - call_user_func("page_initializer"); |
|
| 112 | - } |
|
| 110 | + if (function_exists("page_initializer")) { |
|
| 111 | + call_user_func("page_initializer"); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - $request_method = strtolower($_SERVER['REQUEST_METHOD']); |
|
| 115 | - $api_function_to_be_load = $function_to_be_load; |
|
| 114 | + $request_method = strtolower($_SERVER['REQUEST_METHOD']); |
|
| 115 | + $api_function_to_be_load = $function_to_be_load; |
|
| 116 | 116 | |
| 117 | - if (!empty($request_method)) { |
|
| 118 | - $api_function_to_be_load = $request_method; |
|
| 119 | - } |
|
| 117 | + if (!empty($request_method)) { |
|
| 118 | + $api_function_to_be_load = $request_method; |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - if (function_exists($function_to_be_load)) { |
|
| 122 | - call_user_func_array($function_to_be_load, $content); |
|
| 123 | - } else { |
|
| 124 | - if (function_exists($api_function_to_be_load)) { |
|
| 125 | - $content[] = $function_to_be_load; |
|
| 126 | - call_user_func_array($api_function_to_be_load, $content); |
|
| 127 | - } else { |
|
| 128 | - throw (new Exception("La función <b>$function_to_be_load</b> no existe en la página <b>$page</b>!")); |
|
| 129 | - } |
|
| 130 | - } |
|
| 121 | + if (function_exists($function_to_be_load)) { |
|
| 122 | + call_user_func_array($function_to_be_load, $content); |
|
| 123 | + } else { |
|
| 124 | + if (function_exists($api_function_to_be_load)) { |
|
| 125 | + $content[] = $function_to_be_load; |
|
| 126 | + call_user_func_array($api_function_to_be_load, $content); |
|
| 127 | + } else { |
|
| 128 | + throw (new Exception("La función <b>$function_to_be_load</b> no existe en la página <b>$page</b>!")); |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | 132 | } |