diff --git a/system/engine/controller.php b/system/engine/controller.php index 6af3e36..9ecb54a 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -269,51 +269,133 @@ abstract class Controller implements \Phacil\Framework\Interfaces\Controller { $tpl = new \Phacil\Framework\Render($this->registry); $pegRout = explode("/", ($this->registry->routeOrig)?: Request::GET('route')); - $pegRoutWithoutLast = $pegRout; + /* $pegRoutWithoutLast = $pegRout; array_pop($pegRoutWithoutLast); $pegRoutWithoutPenultimate = $pegRoutWithoutLast; - array_pop($pegRoutWithoutPenultimate); + array_pop($pegRoutWithoutPenultimate); */ if($this->template === NULL) { - $thema = ($this->config->get("config_template") != NULL) ? $this->config->get("config_template") : "default"; - - foreach($tpl->getTemplateTypes() as $extensionTemplate) { - $structure = []; + $noCaseFunction = function ($str) { + return \Phacil\Framework\Registry::case_insensitive_pattern($str); + }; + + //Just template not set manual + $routePatterned = array_map($noCaseFunction, $pegRout); + $routeWithoutLastPatterned = $routePatterned; + $lastRoutePatterned = array_pop($routeWithoutLastPatterned); + $routeWithoutFirstPatterned = $routePatterned; + $firstRoutePatterned = array_shift($routeWithoutFirstPatterned); + + $structure = []; - $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/'.((isset($pegRout[count($pegRout)-2])) ? $pegRout[count($pegRout)-2]."_".end($pegRout) : end($pegRout)).'.'.$extensionTemplate; + $structure[self::TEMPLATE_AREA_THEME][] = $thema . '/' . implode("/", $routePatterned); + $structure[self::TEMPLATE_AREA_THEME][] = 'default/' . implode("/", $routePatterned); + //$structure[] = implode("/", $routePatterned); + $structure[self::TEMPLATE_AREA_MODULAR][] = $firstRoutePatterned . '/View/' . implode("/", $routeWithoutFirstPatterned); + if (count($routePatterned) > 2) { + $structure[self::TEMPLATE_AREA_MODULAR][] = $firstRoutePatterned . '/View/' . implode("/", array_slice($routeWithoutFirstPatterned, 0, -1)) . "_" . $lastRoutePatterned; - foreach($structure as $themefile){ - if(file_exists(Config::DIR_APP_MODULAR() .$themefile)){ - $this->template = $themefile; + //Old compatibility + $structure[self::TEMPLATE_AREA_THEME][] = $thema . '/' . implode("/", $routeWithoutLastPatterned) . '_' . $lastRoutePatterned ; + $structure[self::TEMPLATE_AREA_THEME][] = 'default/' . implode("/", $routeWithoutLastPatterned) . '_' . $lastRoutePatterned ; + //$structure[self::TEMPLATE_AREA_THEME][] = implode("/", $routeWithoutLastPatterned) . '_' . $lastRoutePatterned ; + } + + + foreach($structure[self::TEMPLATE_AREA_MODULAR] as $themefile){ + $types = [ + 'modular' => Config::DIR_APP_MODULAR() .$themefile, + //'theme' => Config::DIR_TEMPLATE() .$themefile, + ]; + $files['modular'] = null; + $files['theme'] = null; + foreach ($types as $type => $globs) { + foreach ($tpl->getTemplateTypes() as $extensionTemplate) { + $files[$type] = glob($globs. "." .$extensionTemplate, GLOB_BRACE); + if(count($files[$type]) > 0) break; + } + } + if(empty($files['modular']) && empty($files['theme'])) continue; + if(!empty($files['modular'])) { + foreach ($files['modular'] as $modular){ + $this->template = str_replace(Config::DIR_APP_MODULAR(), "", $modular); + $templatePath = Config::DIR_APP_MODULAR(); + break; + } + } + if(!empty($files['theme'])) { + foreach ($files['theme'] as $modular){ + $this->template = str_replace(Config::DIR_TEMPLATE(), "", $modular); + $templatePath = Config::DIR_TEMPLATE(); + break; + } + } + if(!empty($this->template)) break; + } + + foreach($structure[self::TEMPLATE_AREA_THEME] as $themefile){ + $types = [ + //'modular' => Config::DIR_APP_MODULAR() .$themefile, + 'theme' => Config::DIR_TEMPLATE() .$themefile, + ]; + $files['modular'] = null; + $files['theme'] = null; + foreach ($types as $type => $globs) { + foreach ($tpl->getTemplateTypes() as $extensionTemplate) { + $files[$type] = glob($globs. "." .$extensionTemplate, GLOB_BRACE); + if(count($files[$type]) > 0) break; + } + } + if(empty($files['modular']) && empty($files['theme'])) continue; + if(!empty($files['modular'])) { + foreach ($files['modular'] as $modular){ + $this->template = str_replace(Config::DIR_APP_MODULAR(), "", $modular); $templatePath = Config::DIR_APP_MODULAR(); break; } - if(file_exists(Config::DIR_TEMPLATE() .$themefile)){ - $this->template = $themefile; + } + if(!empty($files['theme'])) { + foreach ($files['theme'] as $modular){ + $this->template = str_replace(Config::DIR_TEMPLATE(), "", $modular); $templatePath = Config::DIR_TEMPLATE(); break; } } - + if(!empty($this->template)) break; } + } else { - if(file_exists(Config::DIR_APP_MODULAR().implode("/", $pegRoutWithoutLast)."/View/" .$this->template)){ + if(file_exists(Config::DIR_APP_MODULAR(). $pegRout[0] ."/View/" .$this->template)){ + $templatePath = Config::DIR_APP_MODULAR(). $pegRout[0] ."/View/"; + } else { + $filesSeted = glob( + Config::DIR_APP_MODULAR() . + \Phacil\Framework\Registry::case_insensitive_pattern($pegRout[0]) + . "/View/" . $this->template, + GLOB_BRACE + ); + + if (count($filesSeted) > 0) { + $templatePath = str_replace($this->template, "", $filesSeted[0]); + } + } + /* elseif(file_exists(Config::DIR_APP_MODULAR().implode("/", $pegRoutWithoutLast)."/View/" .$this->template)){ $templatePath = Config::DIR_APP_MODULAR().implode("/", $pegRoutWithoutLast)."/View/"; } elseif(file_exists(Config::DIR_APP_MODULAR().implode("/", $pegRoutWithoutPenultimate)."/View/" .$this->template)){ $templatePath = Config::DIR_APP_MODULAR().implode("/", $pegRoutWithoutPenultimate)."/View/"; - } + } */ if(file_exists(Config::DIR_TEMPLATE() .$this->template)){ $templatePath = Config::DIR_TEMPLATE(); } } + if(empty($this->template)) { + throw new Exception('Error: template not seted!'); + } + if (file_exists($templatePath . $this->template)) { $templateFileInfo = pathinfo($templatePath .$this->template); diff --git a/system/engine/interfaces/controller.php b/system/engine/interfaces/controller.php index 34b82ae..e914a0f 100644 --- a/system/engine/interfaces/controller.php +++ b/system/engine/interfaces/controller.php @@ -15,6 +15,10 @@ namespace Phacil\Framework\Interfaces; */ interface Controller extends \Phacil\Framework\Interfaces\Common\Registers { + const TEMPLATE_AREA_MODULAR = 1; + const TEMPLATE_AREA_THEME = 2; + const TEMPLATE_AREA_BOTH = 3; + /** * * {@inheritdoc} diff --git a/system/engine/registry.php b/system/engine/registry.php index f25d801..ff2025c 100644 --- a/system/engine/registry.php +++ b/system/engine/registry.php @@ -191,6 +191,52 @@ final class Registry { if (isset($dataArray['preferences'])) self::$preferences = array_merge(self::$preferences, $dataArray['preferences']); } + + /** + * Adds DI preferences from a JSON file. + * + * This method reads a JSON file containing preferences and merges them into the existing preferences array. + * + * @param string $jsonFilePath The path to the JSON file containing preferences. + * @return void + * @throws \Phacil\Framework\Exception If there is an error reading the JSON file or if the JSON data is invalid. + */ + static public function addPreferenceByRoute($route) { + $routeArray = explode('/', $route); + + $directory = \Phacil\Framework\Config::DIR_APP_MODULAR() . self::case_insensitive_pattern($routeArray[0]) . '/etc/preferences.json'; + + $pattern = self::case_insensitive_pattern($routeArray[0]); + + $files = glob($directory . "*", GLOB_MARK); + + if(isset($files[0])) { + $jsonFilePath = $files[0]; + } else { + return; + } + if (file_exists($jsonFilePath)) { + self::addPreference($jsonFilePath); + } + } + + /** + * Generate Glob case insensitive pattern + * @param string $string + * @return string + */ + static public function case_insensitive_pattern($string) + { + $pattern = ''; + foreach (str_split($string) as $char) { + if (ctype_alpha($char)) { // se o caractere é uma letra + $pattern .= '[' . strtoupper($char) . strtolower($char) . ']'; // adiciona as duas variações de caixa + } else { + $pattern .= $char; // mantém o caractere original + } + } + return $pattern; + } /** * Checks if a preference has been set for the specified class and returns it if found. diff --git a/system/system.php b/system/system.php index fbea04d..29398f4 100644 --- a/system/system.php +++ b/system/system.php @@ -101,11 +101,23 @@ final class startEngineExacTI { \Phacil\Framework\Registry::addPreference(\Phacil\Framework\Config::DIR_SYSTEM()."etc/preferences.json"); + \Phacil\Framework\Registry::addPreferenceByRoute(self::getRoute()); + if($this->composer) { $this->registry->set('composer', $this->composer); } } + static public function getRoute() { + if (Request::GET('route')) { + return (Request::GET('route')); + } else { + $default = \Phacil\Framework\Config::DEFAULT_ROUTE() ?: \Phacil\Framework\Config::DEFAULT_ROUTE('common/home'); + return Request::GET('route', $default); + } + return Request::GET('route') ?: (\Phacil\Framework\Config::DEFAULT_ROUTE() ?: \Phacil\Framework\Config::DEFAULT_ROUTE('common/home')); + } + /** * * @return \Phacil\Framework\startEngineExacTI @@ -541,13 +553,7 @@ if($engine->controllerPreActions()){ } // Router -if (Request::GET('route')) { - $action = new Action(Request::GET('route')); -} else { - $default = \Phacil\Framework\Config::DEFAULT_ROUTE() ?: \Phacil\Framework\Config::DEFAULT_ROUTE('common/home'); - Request::GET('route', $default); - $action = new Action($default); -} +$action = new Action(startEngineExacTI::getRoute()); // Dispatch $not_found = \Phacil\Framework\Config::NOT_FOUND() ?: \Phacil\Framework\Config::NOT_FOUND('error/not_found');