From 502432024e89f38b08f62547cfe8a012d1bd79ce Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Wed, 21 Jul 2021 17:51:28 -0300 Subject: [PATCH] Improvements controller and child loader --- system/engine/controller.php | 40 +++++++++++++++++++----------------- system/engine/registry.php | 2 ++ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/system/engine/controller.php b/system/engine/controller.php index c5fd44e..4eccd70 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -76,6 +76,8 @@ abstract class Controller { */ public $templateTypes = ["tpl", "twig", "mustache", "smarty", "phtml"]; + public $routeOrig; + /** * @param \Phacil\Framework\Registry $registry * @return void @@ -141,6 +143,7 @@ abstract class Controller { foreach($classAlt as $classController){ try { if(class_exists($classController)){ + $this->registry->routeOrig = $child; $controller = new $classController($this->registry); break; @@ -152,6 +155,8 @@ abstract class Controller { $controller->$method($args); + $this->registry->routeOrig = null; + return $controller->output; } else { trigger_error('Error: Could not load controller ' . $child . '!'); @@ -173,8 +178,13 @@ abstract class Controller { $this->data[basename($child)] = $this->getChild($child); } + $pegRout = explode("/", ($this->registry->routeOrig)?: $this->request->get['route']); + $pegRoutWithoutLast = $pegRout; + array_pop($pegRoutWithoutLast); + $pegRoutWithoutPenultimate = $pegRoutWithoutLast; + array_pop($pegRoutWithoutPenultimate); + if($this->template === NULL) { - $pegRout = explode("/", $this->request->get['route']); $thema = ($this->config->get("config_template") != NULL) ? $this->config->get("config_template") : "default"; @@ -184,12 +194,14 @@ abstract class Controller { $structure[] = $thema.'/'.$pegRout[0].'/'.$pegRout[1].((isset($pegRout[2])) ? '_'.$pegRout[2] : '').'.'.$extensionTemplate; $structure[] = 'default/'.$pegRout[0].'/'.$pegRout[1].((isset($pegRout[2])) ? '_'.$pegRout[2] : '').'.'.$extensionTemplate; $structure[] = $pegRout[0].'/'.$pegRout[1].((isset($pegRout[2])) ? '_'.$pegRout[2] : '').'.'.$extensionTemplate; + $structure[] = implode("/", $pegRoutWithoutLast).'/View/'.end($pegRout).'.'.$extensionTemplate; + $structure[] = implode("/", $pegRoutWithoutPenultimate).'/View/'.end($pegRout).((isset($pegRout[count($pegRout)-2])) ? "_".$pegRout[count($pegRout)-2] : "").'.'.$extensionTemplate; foreach($structure as $themefile){ - if(file_exists(DIR_APP_MODULAR."View/" .$themefile)){ + if(file_exists(DIR_APP_MODULAR .$themefile)){ $this->template = $themefile; - $templatePath = DIR_APP_MODULAR."View"; + $templatePath = DIR_APP_MODULAR; break; } if(file_exists(DIR_TEMPLATE .$themefile)){ @@ -198,24 +210,14 @@ abstract class Controller { break; } } - /* if (file_exists(DIR_APP_MODULAR .$structure)) { - $this->template = $structure; - break; - }elseif (file_exists(DIR_TEMPLATE .$structure)) { - $this->template = $structure; - break; - } elseif (file_exists(DIR_TEMPLATE .$structure_D)){ - $this->template = $structure_D; - break; - } elseif(file_exists(DIR_TEMPLATE .$structure_W)) { - $this->template = $structure_W; - break; - } */ - + } } else { - if(file_exists(DIR_APP_MODULAR."View/" .$this->template)){ - $templatePath = DIR_APP_MODULAR."View"; + $teste = DIR_APP_MODULAR.implode("/", $pegRoutWithoutLast)."/View/" .$this->template; + if(file_exists(DIR_APP_MODULAR.implode("/", $pegRoutWithoutLast)."/View/" .$this->template)){ + $templatePath = DIR_APP_MODULAR.implode("/", $pegRoutWithoutLast)."/View/"; + } elseif(file_exists(DIR_APP_MODULAR.implode("/", $pegRoutWithoutPenultimate)."/View/" .$this->template)){ + $templatePath = DIR_APP_MODULAR.implode("/", $pegRoutWithoutPenultimate)."/View/"; } if(file_exists(DIR_TEMPLATE .$this->template)){ $templatePath = DIR_TEMPLATE; diff --git a/system/engine/registry.php b/system/engine/registry.php index 10f29fa..fe6365c 100644 --- a/system/engine/registry.php +++ b/system/engine/registry.php @@ -12,6 +12,8 @@ final class Registry { private $data = array(); + public $routeOrig; + /** * @param string $key * @return mixed