From 55d5bc4f5b5efbd2818905198b3ecac33269704b Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Mon, 12 Jul 2021 08:15:26 -0300 Subject: [PATCH] Improve load controller error class --- system/engine/action.php | 19 +++++++++- system/engine/controller.php | 13 ++++++- system/engine/front.php | 70 ++++++++++++++---------------------- system/system.php | 2 +- 4 files changed, 57 insertions(+), 47 deletions(-) diff --git a/system/engine/action.php b/system/engine/action.php index 2348117..e6c2d7d 100644 --- a/system/engine/action.php +++ b/system/engine/action.php @@ -44,7 +44,7 @@ final class Action { array_shift($parts); continue; - }elseif (is_dir(DIR_APPLICATION . 'controller' . $path)) { + }elseif (is_dir(DIR_APPLICATION . 'controller/' . $path)) { $path .= '/'; array_shift($parts); @@ -93,6 +93,13 @@ final class Action { $this->class = 'Controller' . $pregReplaceOnPath; + $this->classAlt = [ + 'class' => $this->mountClass($strReplaceOnPathNew, $pregReplaceOnPart), + 'legacy' => $this->class, + 'ucfirst' => ucfirst($pregReplaceOnPart), + 'direct' => $pregReplaceOnPart + ]; + array_shift($parts); break; @@ -226,6 +233,16 @@ final class ActionSystem { public function getClass() { return $this->class; } + + /** + * + * @param string $class + * @return $this + */ + public function setClass($class) { + $this->class = $class; + return $this; + } /** @return array */ public function getClassAlt() { diff --git a/system/engine/controller.php b/system/engine/controller.php index b4eff9b..c933bca 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -102,12 +102,23 @@ abstract class Controller { $action = new Action($child, $args); $file = $action->getFile(); $class = $action->getClass(); + $classAlt = $action->getClassAlt(); $method = $action->getMethod(); if (file_exists($file)) { require_once($file); - $controller = new $class($this->registry); + foreach($classAlt as $classController){ + try { + if(class_exists($classController)){ + $controller = new $classController($this->registry); + + break; + } + } catch (\Throwable $th) { + //throw $th; + } + } $controller->$method($args); diff --git a/system/engine/front.php b/system/engine/front.php index ab86133..1293057 100644 --- a/system/engine/front.php +++ b/system/engine/front.php @@ -46,10 +46,10 @@ final class Front { /** * @param Action $action - * @param Action $error + * @param string $error * @return void */ - public function dispatch(\Phacil\Framework\Action $action, \Phacil\Framework\Action $error) { + public function dispatch(\Phacil\Framework\Action $action, $error) { $this->error = $error; foreach ($this->pre_action as $pre_action) { @@ -74,62 +74,44 @@ final class Front { $method = $action->getMethod(); $args = $action->getArgs(); - $action = ''; - - $c = get_declared_classes(); + unset($action); if (file_exists($file)) { require_once($file); - try { - $controller = new $classAlt['class']($this->registry); - $action->setClass($classAlt['class']); - array_shift($classAlt['class']); - } catch (\Throwable $th) { - foreach($classAlt as $classController){ - try { - $controller = new $classController($this->registry); - $action->setClass($classController); - } catch (\Throwable $th) { - //throw $th; - } - } - } - - if(!$controller) { - + foreach($classAlt as $classController){ try { - $controller = new $class($this->registry); - - } catch (\Throwable $th) { - $e = array_diff(get_declared_classes(), $c); - try { - $classController = end($e); + if(class_exists($classController)){ $controller = new $classController($this->registry); - $action->setClass($classController); - } catch (\Throwable $th) { - foreach($e as $classController){ - try { - $controller = new $classController($this->registry); - $action->setClass($classController); - } catch (\Throwable $th) { - //throw $th; - } - } + + break; } - + } catch (\Throwable $th) { + //throw $th; } } - if (is_callable(array($controller, $method))) { - $action = call_user_func_array(array($controller, $method), $args); - } else { - $action = $this->error; + try { + if (is_callable(array($controller, $method))) { + $action = call_user_func_array(array($controller, $method), $args); + } else { + $action = new Action($this->error); + + $this->error = ''; + throw new \Exception("The controller can't be loaded", 1); + } + } catch (\Throwable $th) { + //throw $th; + $action = new Action($this->error); $this->error = ''; + + throw new \Exception("The controller can't be loaded", 1); + } + } else { - $action = $this->error; + $action = new Action($this->error); $this->error = ''; } diff --git a/system/system.php b/system/system.php index 49499a2..3808756 100644 --- a/system/system.php +++ b/system/system.php @@ -405,7 +405,7 @@ if (isset($request->get['route'])) { // Dispatch $not_found = (defined('NOT_FOUND')) ? NOT_FOUND : 'error/not_found'; -$controller->dispatch($action, new Action($not_found)); +$controller->dispatch($action, ($not_found)); // Output $response->output();