Example #1
0
 /**
  * Función encargada de enrutar una petición a controlador indicado.
  * @param string $url URL de la petición.
  * @param bool $throw Si en caso de error debe generar una excepción o mostrar un error.
  * @author Ignacio Daniel Rostagno <*****@*****.**>
  */
 private static function route($url, $throw = FALSE)
 {
     // Obtenemos los segmentos de la URL
     $segmentos = explode('/', $url);
     // Verificamos si es un plugin.
     if (count($segmentos) >= 2) {
         if (strtolower($segmentos[0]) === 'plugin') {
             // Formateo el plugins.
             $p_name = strtolower($segmentos[1]);
             // Validamos que tenga el formato requerido.
             if (preg_match('/^[a-z0-9]+$/D', $p_name) < 1) {
                 if (!$throw) {
                     Error::show_error('Petición inválida', 404);
                 } else {
                     throw new Exception('Petición inválida', 404);
                 }
                 return FALSE;
             }
             // Verifico su existencia y que este activo.
             $p_obj = Plugin_Manager::get_instance()->get($p_name);
             if ($p_obj === NULL || !$p_obj->estado()) {
                 if (!$throw) {
                     Error::show_error('Plugin inexistente', 404);
                 } else {
                     throw new Exception('Plugin inexistente', 404);
                 }
                 return FALSE;
             }
             $p_dir = Plugin_Manager::nombre_as_path($p_name);
             if (file_exists($p_dir) && is_dir($p_dir)) {
                 // Cargamos el plugin.
                 // Obtenemos el controlador
                 $controller = empty($segmentos[2]) ? 'home' : strtolower($segmentos[2]);
                 // Validamos que tenga el formato requerido.
                 if (preg_match('/^[a-z0-9_]+$/D', $controller) < 1) {
                     if (!$throw) {
                         Error::show_error('Petición inválida', 404);
                     } else {
                         throw new Exception('Petición inválida', 404);
                     }
                     return FALSE;
                 }
                 // Obtenemos la acción.
                 $accion = empty($segmentos[3]) ? 'index' : strtolower($segmentos[3]);
                 // Validamos que tenga el formato requerido.
                 if (preg_match('/^[a-z0-9_]+$/D', $accion) < 1) {
                     if (!$throw) {
                         Error::show_error('Petición inválida', 404);
                     } else {
                         throw new Exception('Petición inválida', 404);
                     }
                     return FALSE;
                 }
                 // Obtenemos los argumentos.
                 if (is_array($segmentos) && count($segmentos) > 4) {
                     $args = array_slice($segmentos, 4);
                 } else {
                     $args = array();
                 }
                 // Normalizamos el nombre del controlador para usar en las clases.
                 $controller_name = 'Plugin_' . ucfirst($p_name) . '_Controller_' . ucfirst($controller);
                 //Instanciamos el controllador
                 if (!class_exists($controller_name)) {
                     if (!$throw) {
                         Error::show_error("No existe el controlador: '{$controller_name}'", 404);
                     } else {
                         throw new Exception("No existe el controlador: '{$controller_name}'", 404);
                     }
                 } else {
                     // Verificamos exista método.
                     $r_c = new ReflectionClass($controller_name);
                     if (!$r_c->hasMethod('action_' . $accion)) {
                         if (!$throw) {
                             Error::show_error("No existe la acción '{$accion}' para el controlador '{$controller_name}'", 404);
                         } else {
                             throw new Exception("No existe la acción '{$accion}' para el controlador '{$controller_name}'", 404);
                         }
                     } else {
                         $cont = new $controller_name();
                     }
                 }
                 // Obtenemos la cantidad de parámetros necesaria.
                 $r_m = new ReflectionMethod($cont, 'action_' . $accion);
                 $p_n = $r_m->getNumberOfRequiredParameters();
                 // Expandemos el arreglo de parámetros con NULL si es necesario.
                 while (count($args) < $p_n) {
                     $args[] = NULL;
                 }
                 Request::add_stack(NULL, $controller, $accion, $args, $p_name);
                 // No hubo problemas, llamamos.
                 $rst = call_user_func_array(array($cont, 'action_' . $accion), $args);
                 Request::pop_stack();
                 return $rst;
             } else {
                 // Plugin Inválido.
                 if (!$throw) {
                     Error::show_error("El plugin '{$p_name}' no existe", 404);
                 } else {
                     throw new Exception("El plugin '{$p_name}' no existe", 404);
                 }
                 return FALSE;
             }
         }
     }
     // Verificamos subdirectorio.
     if (!empty($segmentos[0])) {
         // Directorio
         $directorio = strtolower($segmentos[0]);
         // Obtenemos el controlador
         $controller = empty($segmentos[1]) ? 'home' : strtolower($segmentos[1]);
         // Obtenemos la acción.
         $accion = empty($segmentos[2]) ? 'index' : strtolower($segmentos[2]);
         if (preg_match('/^[a-z0-9_]+$/D', $controller) && preg_match('/^[a-z0-9_]+$/D', $accion)) {
             // Obtenemos los argumentos.
             if (is_array($segmentos) && count($segmentos) > 3) {
                 $args = array_slice($segmentos, 3);
             } else {
                 $args = array();
             }
             // Normalizamos el nombre del controlador para usar en las clases.
             $controller_name = 'Controller_' . ucfirst($directorio) . '_' . ucfirst($controller);
             //Instanciamos el controllador
             if (class_exists($controller_name)) {
                 // Verificamos exista método.
                 $r_c = new ReflectionClass($controller_name);
                 if ($r_c->hasMethod('action_' . $accion)) {
                     $cont = new $controller_name();
                     // Obtenemos la cantidad de parámetros necesaria.
                     $r_m = new ReflectionMethod($cont, 'action_' . $accion);
                     $p_n = $r_m->getNumberOfRequiredParameters();
                     // Expandemos el arreglo de parámetros con NULL si es necesario.
                     while (count($args) < $p_n) {
                         $args[] = NULL;
                     }
                     Request::add_stack(NULL, $controller, $accion, $args, NULL);
                     // No hubo problemas, llamamos.
                     $rst = call_user_func_array(array($cont, 'action_' . $accion), $args);
                     Request::pop_stack();
                     return $rst;
                 }
             }
         }
     }
     // Obtenemos el controlador
     $controller = empty($segmentos[0]) ? 'home' : strtolower($segmentos[0]);
     if (preg_match('/^[a-z0-9_]+$/D', $controller) < 1) {
         if (!$throw) {
             Error::show_error('Petición inválida', 404);
         } else {
             throw new Exception('Petición inválida', 404);
         }
         return FALSE;
     }
     // Obtenemos la acción.
     $accion = empty($segmentos[1]) ? 'index' : strtolower($segmentos[1]);
     if (preg_match('/^[a-z0-9_]+$/D', $accion) < 1) {
         if (!$throw) {
             Error::show_error('Petición inválida', 404);
         } else {
             throw new Exception('Petición inválida', 404);
         }
         return FALSE;
     }
     // Obtenemos los argumentos.
     if (is_array($segmentos) && count($segmentos) > 2) {
         $args = array_slice($segmentos, 2);
     } else {
         $args = array();
     }
     // Normalizamos el nombre del controlador para usar en las clases.
     $controller_name = 'Controller_' . ucfirst($controller);
     //Instanciamos el controllador
     if (!class_exists($controller_name)) {
         if (!$throw) {
             Error::show_error("No existe el controlador: '{$controller_name}'", 404);
         } else {
             throw new Exception("No existe el controlador: '{$controller_name}'", 404);
         }
     } else {
         // Verificamos exista método.
         $r_c = new ReflectionClass($controller_name);
         if (!$r_c->hasMethod('action_' . $accion)) {
             if (!$throw) {
                 Error::show_error("No existe la acción '{$accion}' para el controlador '{$controller_name}'", 404);
             } else {
                 throw new Exception("No existe la acción '{$accion}' para el controlador '{$controller_name}'", 404);
             }
         } else {
             $cont = new $controller_name();
         }
     }
     // Obtenemos la cantidad de parámetros necesaria.
     $r_m = new ReflectionMethod($cont, 'action_' . $accion);
     $p_n = $r_m->getNumberOfRequiredParameters();
     // Expandemos el arreglo de parámetros con NULL si es necesario.
     while (count($args) < $p_n) {
         $args[] = NULL;
     }
     Request::add_stack(NULL, $controller, $accion, $args, NULL);
     // No hubo problemas, llamamos.
     $rst = call_user_func_array(array($cont, 'action_' . $accion), $args);
     Request::pop_stack();
     return $rst;
 }