From 419378fe37fad4f31ac602bab3a680e13381192c Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Mon, 12 Jul 2021 06:40:36 -0300 Subject: [PATCH 01/16] New Controller class load --- system/engine/action.php | 75 +++++++++++++++++++++++++++++++++++----- system/engine/front.php | 45 ++++++++++++++++++++++-- 2 files changed, 109 insertions(+), 11 deletions(-) diff --git a/system/engine/action.php b/system/engine/action.php index ef3fc2b..2348117 100644 --- a/system/engine/action.php +++ b/system/engine/action.php @@ -15,6 +15,8 @@ final class Action { protected $method; protected $args = array(); + private $classAlt = []; + /** * @param string $route * @param array $args @@ -49,27 +51,47 @@ final class Action { continue; } + + $strReplaceOnPathNew = str_replace('../', '', $pathNew); + $strReplaceOnPath = str_replace('../', '', $path); + $strReplaceOnPart = str_replace('../', '', $part); + $pregReplaceOnPath = preg_replace('/[^a-zA-Z0-9]/', '', $path); + $pregReplaceOnPart = preg_replace('/[^a-zA-Z0-9]/', '', $part); - if (is_file(DIR_APP_MODULAR . str_replace('../', '', $pathNew) . 'Controller/' . str_replace('../', '', $part) . '.php')) { - $this->file = DIR_APP_MODULAR . str_replace('../', '', $pathNew) . 'Controller/' . str_replace('../', '', $part) . '.php'; + if (is_file(DIR_APP_MODULAR . $strReplaceOnPathNew . 'Controller/' . $strReplaceOnPart . '.php')) { + $this->file = DIR_APP_MODULAR . $strReplaceOnPathNew . 'Controller/' . $strReplaceOnPart . '.php'; - $this->class = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $path); + $this->class = 'Controller' . $pregReplaceOnPath; + + $this->classAlt = [ + 'class' => $this->mountClass($strReplaceOnPathNew, $pregReplaceOnPart), + 'legacy' => $this->class, + 'ucfirst' => ucfirst($pregReplaceOnPart), + 'direct' => $pregReplaceOnPart + ]; array_shift($parts); break; - } elseif (is_file(DIR_APP_MODULAR . str_replace('../', '', $pathNew) . 'Controller/' . str_replace('../', '', ucfirst($part)) . '.php')) { - $this->file = DIR_APP_MODULAR . str_replace('../', '', $pathNew) . 'Controller/' . str_replace('../', '', ucfirst($part)) . '.php'; + } elseif (is_file(DIR_APP_MODULAR . $strReplaceOnPathNew . 'Controller/' . ucfirst($strReplaceOnPart) . '.php')) { + $this->file = DIR_APP_MODULAR . $strReplaceOnPathNew . 'Controller/' . ucfirst($strReplaceOnPart) . '.php'; - $this->class = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $path); + $this->class = 'Controller' . $pregReplaceOnPath; + + $this->classAlt = [ + 'class' => $this->mountClass($strReplaceOnPathNew, $pregReplaceOnPart), + 'legacy' => $this->class, + 'ucfirst' => ucfirst($pregReplaceOnPart), + 'direct' => $pregReplaceOnPart + ]; array_shift($parts); break; - } elseif (is_file(DIR_APPLICATION . 'controller/' . str_replace('../', '', $path) . '.php')) { - $this->file = DIR_APPLICATION . 'controller/' . str_replace('../', '', $path) . '.php'; + } elseif (is_file(DIR_APPLICATION . 'controller/' . $strReplaceOnPath . '.php')) { + $this->file = DIR_APPLICATION . 'controller/' . $strReplaceOnPath . '.php'; - $this->class = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $path); + $this->class = 'Controller' . $pregReplaceOnPath; array_shift($parts); @@ -100,6 +122,25 @@ final class Action { public function getClass() { return $this->class; } + + private function mountClass(string $namespace, string $class) { + return (defined('NAMESPACE_PREFIX') ? NAMESPACE_PREFIX."\\" : "").str_replace("/", "\\", $namespace)."Controller\\".$class; + } + + /** + * + * @param string $class + * @return $this + */ + public function setClass($class) { + $this->class = $class; + return $this; + } + + /** @return array */ + public function getClassAlt() { + return $this->classAlt; + } /** @return string */ public function getMethod() { @@ -120,6 +161,12 @@ final class ActionSystem { protected $method; protected $args = array(); + /** + * + * @var (string[]|string|null)[] + */ + private $classAlt = []; + /** * @param string $route * @param array $args @@ -146,6 +193,11 @@ final class ActionSystem { $this->class = 'System' . preg_replace('/[^a-zA-Z0-9]/', '', $path); + $this->classAlt = [ + 'legacy' => $this->class, + 'direct' => preg_replace('/[^a-zA-Z0-9]/', '', $part) + ]; + array_shift($parts); break; @@ -175,6 +227,11 @@ final class ActionSystem { return $this->class; } + /** @return array */ + public function getClassAlt() { + return $this->classAlt; + } + /** @return string */ public function getMethod() { return $this->method; diff --git a/system/engine/front.php b/system/engine/front.php index ccb7c59..ab86133 100644 --- a/system/engine/front.php +++ b/system/engine/front.php @@ -67,18 +67,59 @@ final class Front { } } - private function execute($action) { + private function execute(object $action) { $file = $action->getFile(); $class = $action->getClass(); + $classAlt = $action->getClassAlt(); $method = $action->getMethod(); $args = $action->getArgs(); $action = ''; + $c = get_declared_classes(); + if (file_exists($file)) { require_once($file); - $controller = new $class($this->registry); + 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) { + + try { + $controller = new $class($this->registry); + + } catch (\Throwable $th) { + $e = array_diff(get_declared_classes(), $c); + try { + $classController = end($e); + $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; + } + } + } + + } + } if (is_callable(array($controller, $method))) { $action = call_user_func_array(array($controller, $method), $args); From 55d5bc4f5b5efbd2818905198b3ecac33269704b Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Mon, 12 Jul 2021 08:15:26 -0300 Subject: [PATCH 02/16] 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(); From 0ca7ba2d39996a3efe7d0e2cf0106cbbe6eca829 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Tue, 13 Jul 2021 19:53:45 -0300 Subject: [PATCH 03/16] Remove var_dump and add extra logs --- system/engine/autoload.php | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/system/engine/autoload.php b/system/engine/autoload.php index 996be67..44dc3df 100644 --- a/system/engine/autoload.php +++ b/system/engine/autoload.php @@ -10,12 +10,10 @@ require_once(DIR_SYSTEM . 'engine/action.php'); require_once(DIR_SYSTEM . 'engine/controller.php'); - +/** @autoload class */ spl_autoload_register(function ($class) { $namespace = explode("\\", $class); - var_dump($class); - $legacy = [ 'Controller', 'Model', @@ -29,13 +27,18 @@ spl_autoload_register(function ($class) { ]; if(in_array($class, $legacy)){ - - class_alias("\\Phacil\\Framework\\".$class, $class); + try { + class_alias("\\Phacil\\Framework\\".$class, $class); + } catch (\Exception $th) { + $log = new \Phacil\Framework\Log(DIR_LOGS."exception.log"); + $log->write($class.' not loaded!'); + } + //eval("class ".$class." extends \\Phacil\\Framework\\".$class." {}"); return; } - $class = ($namespace[0] == "Phacil") ? str_replace('phacil\\framework\\', '', strtolower( $class)) : $class; + $classNative = ($namespace[0] == "Phacil") ? str_replace('phacil\\framework\\', '', strtolower( $class)) : $class; $allowed = [ 'log', @@ -50,16 +53,18 @@ spl_autoload_register(function ($class) { 'abstracthelper' ]; - if($namespace[0] == "Phacil" && in_array($class, $allowed)){ + if($namespace[0] == "Phacil" && in_array($classNative, $allowed)){ try { - include_once(DIR_SYSTEM . 'engine/'. $class.'.php'); + include_once(DIR_SYSTEM . 'engine/'. $classNative.'.php'); return; - } catch (\Throwable $th) { + } catch (\Exception $th) { + $log = new \Phacil\Framework\Log(DIR_LOGS."exception.log"); + $log->write($class.' not loaded!'); throw new \Exception("Class '$class' not loaded."); } } - $value = DIR_SYSTEM . $class.'/autoload.php'; + $value = DIR_SYSTEM . $classNative.'/autoload.php'; if($namespace[0] == "Phacil" && in_array($value, $this->dirs)){ try { @@ -67,9 +72,13 @@ spl_autoload_register(function ($class) { require_once $value; return; } else { + $log = new \Phacil\Framework\Log(DIR_LOGS."exception.log"); + $log->write($class.' not loaded!'); throw new \Exception("I can't load '$value' file! Please check system permissions."); } } catch (\Exception $e) { + $log = new \Phacil\Framework\Log(DIR_LOGS."exception.log"); + $log->write($class.' not loaded!'); exit($e->getMessage()); } } @@ -81,9 +90,13 @@ spl_autoload_register(function ($class) { require_once $tryMagicOne; return; } else { + $log = new \Phacil\Framework\Log(DIR_LOGS."exception.log"); + $log->write($class.' not loaded!'); throw new \Exception("I can't load '$tryMagicOne' file! Please check system permissions."); } } catch (\Exception $e) { + $log = new \Phacil\Framework\Log(DIR_LOGS."exception.log"); + $log->write($class.' not loaded!'); exit($e->getMessage()); } } @@ -96,12 +109,17 @@ spl_autoload_register(function ($class) { require_once $tryMagicOne; return; } else { + $log = new \Phacil\Framework\Log(DIR_LOGS."exception.log"); + $log->write($class.' not loaded!'); throw new \Exception("I can't load '$tryMagicOne' file! Please check system permissions."); } } catch (\Exception $e) { + $log = new \Phacil\Framework\Log(DIR_LOGS."exception.log"); + $log->write($class.' not loaded!'); exit($e->getMessage()); } } + return; }); From bc5f45e5902ef6d78e7314a84256f85989e36278 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Wed, 14 Jul 2021 21:32:41 -0300 Subject: [PATCH 04/16] Controller improvement --- system/engine/autoload.php | 19 ++++++ system/engine/controller.php | 119 ++++++++++++++++++++++++++++++----- 2 files changed, 121 insertions(+), 17 deletions(-) diff --git a/system/engine/autoload.php b/system/engine/autoload.php index 44dc3df..bbd16b4 100644 --- a/system/engine/autoload.php +++ b/system/engine/autoload.php @@ -13,6 +13,7 @@ require_once(DIR_SYSTEM . 'engine/controller.php'); /** @autoload class */ spl_autoload_register(function ($class) { $namespace = explode("\\", $class); + $namespaceWithoutPrefix = (defined('NAMESPACE_PREFIX')) ? explode("\\", str_replace(NAMESPACE_PREFIX."\\" , "", $class)) : $namespace; $legacy = [ 'Controller', @@ -100,6 +101,24 @@ spl_autoload_register(function ($class) { exit($e->getMessage()); } } + + + if(file_exists($tryMagicOne = DIR_APP_MODULAR. implode("/", $namespaceWithoutPrefix).".php")){ + try { + if(is_readable($tryMagicOne)) { + require_once $tryMagicOne; + return; + } else { + $log = new \Phacil\Framework\Log(DIR_LOGS."exception.log"); + $log->write($class.' not loaded!'); + throw new \Exception("I can't load '$tryMagicOne' file! Please check system permissions."); + } + } catch (\Exception $e) { + $log = new \Phacil\Framework\Log(DIR_LOGS."exception.log"); + $log->write($class.' not loaded!'); + exit($e->getMessage()); + } + } $prefix = array_shift($namespace); diff --git a/system/engine/controller.php b/system/engine/controller.php index c933bca..c5fd44e 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -21,8 +21,23 @@ abstract class Controller { * @var Registry */ protected $registry; + + /** + * + * @var int + */ protected $id; + + /** + * + * @var mixed + */ protected $layout; + + /** + * + * @var string + */ protected $template; /** @@ -49,13 +64,17 @@ abstract class Controller { */ protected $error = array(); + /** + * + * @var string + */ protected $output; /** * * @var string[] */ - public $templateTypes = ["tpl", "twig", "mustache", "smarty", "dwoo"]; + public $templateTypes = ["tpl", "twig", "mustache", "smarty", "phtml"]; /** * @param \Phacil\Framework\Registry $registry @@ -65,10 +84,21 @@ abstract class Controller { $this->registry = $registry; } + /** + * + * @param string $key + * @return Registry + */ public function __get($key) { return $this->registry->get($key); } + /** + * + * @param string $key + * @param object $value + * @return void + */ public function __set($key, $value) { $this->registry->set($key, $value); } @@ -96,7 +126,7 @@ abstract class Controller { /** * @param string $child * @param array $args - * @return mixed + * @return object */ protected function getChild($child, array $args = array()) { $action = new Action($child, $args); @@ -130,7 +160,7 @@ abstract class Controller { } /** - * @return mixed + * @return string * @throws TypeError * @throws Mustache_Exception_UnknownTemplateException * @throws RuntimeException @@ -148,13 +178,30 @@ abstract class Controller { $thema = ($this->config->get("config_template") != NULL) ? $this->config->get("config_template") : "default"; + $structure = []; foreach($this->templateTypes as $extensionTemplate) { - $structure = $thema.'/'.$pegRout[0].'/'.$pegRout[1].((isset($pegRout[2])) ? '_'.$pegRout[2] : '').'.'.$extensionTemplate; - $structure_D = 'default/'.$pegRout[0].'/'.$pegRout[1].((isset($pegRout[2])) ? '_'.$pegRout[2] : '').'.'.$extensionTemplate; - $structure_W = $pegRout[0].'/'.$pegRout[1].((isset($pegRout[2])) ? '_'.$pegRout[2] : '').'.'.$extensionTemplate; + $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; + - if (file_exists(DIR_TEMPLATE .$structure) != false) { + foreach($structure as $themefile){ + if(file_exists(DIR_APP_MODULAR."View/" .$themefile)){ + $this->template = $themefile; + $templatePath = DIR_APP_MODULAR."View"; + break; + } + if(file_exists(DIR_TEMPLATE .$themefile)){ + $this->template = $themefile; + $templatePath = DIR_TEMPLATE; + 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)){ @@ -163,21 +210,40 @@ abstract class Controller { } 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"; + } + if(file_exists(DIR_TEMPLATE .$this->template)){ + $templatePath = DIR_TEMPLATE; + } } - if (file_exists(DIR_TEMPLATE . $this->template)) { + if (file_exists($templatePath . $this->template)) { - $templateType = substr(strrchr($this->template, '.'), 1); + $templateFileInfo = pathinfo($templatePath .$this->template); + $templateType = $templateFileInfo['extension']; switch($templateType) { case 'tpl': extract($this->data); ob_start(); - require(DIR_TEMPLATE . $this->template); + require($templatePath . $this->template); + + $this->output = ob_get_contents(); + + ob_end_clean(); + break; + + case 'phtml': + extract($this->data); + + ob_start(); + require($templatePath . $this->template); $this->output = ob_get_contents(); @@ -187,6 +253,9 @@ abstract class Controller { case 'twig': require_once(DIR_SYSTEM."templateEngines/Twig/autoload.php"); + /** + * @var array + */ $config = array( 'autoescape' => false, 'cache' => DIR_CACHE."twig/", @@ -197,13 +266,23 @@ abstract class Controller { $Twig_SimpleFilter = constant('TwigSimpleFilter'); $Twig_Extension_Debug = constant('TwigExtensionDebug'); - $loader = new $TwigLoaderFilesystem (DIR_TEMPLATE); + /** + * @var \TwigLoaderFilesystem + */ + $loader = new $TwigLoaderFilesystem ($templatePath); + + /** + * @var \TwigEnvironment + */ $twig = new $Twig_Environment($loader, $config); if($config['debug']) { $twig->addExtension(new $Twig_Extension_Debug()); } + /** + * @var \transExtension + */ $twig->addExtension(new \transExtension()); $twig->addFilter(new $Twig_SimpleFilter('translate', function ($str) { @@ -231,12 +310,15 @@ abstract class Controller { \Mustache_Autoloader::register(); + /** + * @var \Mustache_Engine + */ $mustache = new \Mustache_Engine(array( //'template_class_prefix' => '__MyTemplates_', 'cache' => DIR_CACHE.'mustache', 'cache_file_mode' => 0666, // Please, configure your umask instead of doing this :) //'cache_lambda_templates' => true, - 'loader' => new \Mustache_Loader_FilesystemLoader(DIR_TEMPLATE), + 'loader' => new \Mustache_Loader_FilesystemLoader($templatePath), //'partials_loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views/partials'), 'helpers' => array('translate' => function($text) { if (class_exists('Translate')) { @@ -262,9 +344,12 @@ abstract class Controller { case 'smarty': require_once(DIR_SYSTEM."templateEngines/smarty/autoload.php"); + /** + * @var \Smarty + */ $smarty = new \Smarty(); - $smarty->setTemplateDir(DIR_TEMPLATE); + $smarty->setTemplateDir($templatePath); $smarty->setCompileDir(DIR_CACHE."Smarty/compile/"); //$smarty->setConfigDir('/web/www.example.com/guestbook/configs/'); $smarty->setCacheDir(DIR_CACHE."Smarty/cache/"); @@ -285,7 +370,7 @@ abstract class Controller { extract($this->data); ob_start(); - require(DIR_TEMPLATE . $this->template); + require($templatePath . $this->template); $this->output = ob_get_contents(); @@ -297,14 +382,14 @@ abstract class Controller { return $this->output; } else { - trigger_error('Error: Could not load template ' . DIR_TEMPLATE . $this->template . '!'); + trigger_error('Error: Could not load template ' . $templatePath . $this->template . '!'); exit(); } } /** * @param bool $commonChildren - * @return mixed + * @return \Phacil\Framework\Response * @throws TypeError * @throws Mustache_Exception_UnknownTemplateException * @throws RuntimeException From f43b28ab010900139fd6d39dd22cf85b659de609 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Wed, 14 Jul 2021 21:51:10 -0300 Subject: [PATCH 05/16] Updates on readme --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9c30163..c8e4735 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ A super easy PHP Framework for web development! +v2 is comming soon... check the beta branch! + ## Requirements @@ -574,9 +576,10 @@ In a sample case, we have this controller: ##### Sample: ```php - define("ROUTES", array( + define("ROUTES", [ "produto/%d/%/promo" => "feriado/natal/presentes" - ) + ] + ); ``` ```php From f7fb221f4eb4d5c4891e266ccbc474a377d45c4f Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Tue, 20 Jul 2021 19:19:26 -0300 Subject: [PATCH 06/16] Minor changes --- public_html/index.php | 3 ++- system/caches/caches.php | 17 ++++++++++++++++- system/system.php | 7 +++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/public_html/index.php b/public_html/index.php index 651cb25..342c6e9 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -1,7 +1,7 @@ dirCache . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.cache'); @@ -155,6 +163,7 @@ final class Caches { } } + /** @return true */ public function deleteAll() { $files = glob($this->dirCache . '*.cache'); @@ -165,13 +174,15 @@ final class Caches { return true; } + /** @return bool */ public function clear() { return $this->deleteAll(); } + /** @return Phacil\Framework\stdClass */ public function stats() { - $obj = new stdClass(); + $obj = new \stdClass(); $obj->size = $this->GetDirectorySize($this->dirCache); $obj->info = NULL; @@ -181,6 +192,10 @@ final class Caches { return $obj; } + /** + * @param string $path + * @return float|int + */ private function GetDirectorySize($path){ $bytestotal = 0; $path = realpath($path); diff --git a/system/system.php b/system/system.php index 3808756..c88c71d 100644 --- a/system/system.php +++ b/system/system.php @@ -11,9 +11,11 @@ namespace Phacil\Framework; use Exception; use TypeError; +/** + * + * @package Phacil\Framework + */ class startEngineExacTI { - /*public $constants; - public $userConstants;*/ /** * @@ -240,6 +242,7 @@ $loader = new Loader($engine->registry); $engine->registry->set('load', $loader); // Config +/** @var Config */ $config = new Config(); $engine->registry->set('config', $config); From 502432024e89f38b08f62547cfe8a012d1bd79ce Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Wed, 21 Jul 2021 17:51:28 -0300 Subject: [PATCH 07/16] 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 From a28d719b16523c554aded04e4136174808184ca1 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Thu, 22 Jul 2021 14:43:11 -0300 Subject: [PATCH 08/16] bugfix template loader --- system/engine/controller.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/system/engine/controller.php b/system/engine/controller.php index 4eccd70..e7d85f8 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -195,7 +195,7 @@ abstract class Controller { $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; + $structure[] = implode("/", $pegRoutWithoutPenultimate).'/View/'.((isset($pegRout[count($pegRout)-2])) ? $pegRout[count($pegRout)-2]."_".end($pegRout) : end($pegRout)).'.'.$extensionTemplate; foreach($structure as $themefile){ @@ -265,8 +265,8 @@ abstract class Controller { ); $TwigLoaderFilesystem = constant('\TwigLoaderFilesystem'); $Twig_Environment = constant('\TwigEnvironment'); - $Twig_SimpleFilter = constant('TwigSimpleFilter'); - $Twig_Extension_Debug = constant('TwigExtensionDebug'); + $Twig_SimpleFilter = constant('\TwigSimpleFilter'); + $Twig_Extension_Debug = constant('\TwigExtensionDebug'); /** * @var \TwigLoaderFilesystem From c3371dd72d6ef51d3ae5ef48e0565d1553f81120 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Thu, 22 Jul 2021 15:19:02 -0300 Subject: [PATCH 09/16] Controller doc --- system/engine/controller.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/system/engine/controller.php b/system/engine/controller.php index e7d85f8..c344f35 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -14,7 +14,19 @@ use RuntimeException; use SmartyException; use Exception; -/** @package Phacil\Framework */ +/** + * Extend this class to create interation with your module controller to Phacil engine controller. + * + * Use as: + * class YouClass extends \Phacil\Framework\Controller {} + * + * You can use the __construct function on call the \Phacil\Framework\Register object inside parent. + * + * Example: public funcion __construct(\Phacil\Framework\Registry $registry){ parent::__construct($registry); YOUR_CODE; } + * + * @package Phacil\Framework + * @since 1.0 + * */ abstract class Controller { /** * From 87850105d2030e0b1413951fb59abbec02219a47 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Thu, 22 Jul 2021 15:23:54 -0300 Subject: [PATCH 10/16] More phpdoc --- system/engine/controller.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/system/engine/controller.php b/system/engine/controller.php index c344f35..1be3b56 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -91,10 +91,14 @@ abstract class Controller { public $routeOrig; /** + * Implements constructor. + * + * If you use this, don't forget the parent::__construct($registry); + * * @param \Phacil\Framework\Registry $registry * @return void */ - public function __construct($registry) { + public function __construct(\Phacil\Framework\Registry $registry) { $this->registry = $registry; } From 1d7348d802b3124e0047d94da1f86d47980b3972 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Fri, 30 Jul 2021 02:37:27 -0300 Subject: [PATCH 11/16] Implement front interface --- system/engine/autoload.php | 5 +++-- system/engine/controller.php | 6 +----- system/engine/front.php | 11 ++++++++++- system/engine/interfaces/front.php | 14 ++++++++++++++ 4 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 system/engine/interfaces/front.php diff --git a/system/engine/autoload.php b/system/engine/autoload.php index bbd16b4..7b825c4 100644 --- a/system/engine/autoload.php +++ b/system/engine/autoload.php @@ -51,12 +51,13 @@ spl_autoload_register(function ($class) { 'document', 'response', 'classes', - 'abstracthelper' + 'abstracthelper', + 'interfaces\front' ]; if($namespace[0] == "Phacil" && in_array($classNative, $allowed)){ try { - include_once(DIR_SYSTEM . 'engine/'. $classNative.'.php'); + include_once(DIR_SYSTEM . 'engine/'. str_replace("\\", "/", $classNative).'.php'); return; } catch (\Exception $th) { $log = new \Phacil\Framework\Log(DIR_LOGS."exception.log"); diff --git a/system/engine/controller.php b/system/engine/controller.php index 1be3b56..2366f12 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -229,7 +229,7 @@ abstract class Controller { } } else { - $teste = DIR_APP_MODULAR.implode("/", $pegRoutWithoutLast)."/View/" .$this->template; + //$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)){ @@ -408,10 +408,6 @@ abstract class Controller { /** * @param bool $commonChildren * @return \Phacil\Framework\Response - * @throws TypeError - * @throws Mustache_Exception_UnknownTemplateException - * @throws RuntimeException - * @throws SmartyException * @throws Exception */ protected function out ($commonChildren = true) { diff --git a/system/engine/front.php b/system/engine/front.php index 1293057..065c71d 100644 --- a/system/engine/front.php +++ b/system/engine/front.php @@ -8,8 +8,12 @@ namespace Phacil\Framework; +use Phacil\Framework\Interfaces\front as frontinterface; + +use Exception; + /** @package Phacil\Framework */ -final class Front { +final class Front implements frontinterface { /** * @@ -67,6 +71,11 @@ final class Front { } } + /** + * @param object $action + * @return Action + * @throws Exception + */ private function execute(object $action) { $file = $action->getFile(); $class = $action->getClass(); diff --git a/system/engine/interfaces/front.php b/system/engine/interfaces/front.php new file mode 100644 index 0000000..8eca1b9 --- /dev/null +++ b/system/engine/interfaces/front.php @@ -0,0 +1,14 @@ + Date: Fri, 30 Jul 2021 02:43:53 -0300 Subject: [PATCH 12/16] Front interface --- system/engine/interfaces/front.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/system/engine/interfaces/front.php b/system/engine/interfaces/front.php index 8eca1b9..d20951d 100644 --- a/system/engine/interfaces/front.php +++ b/system/engine/interfaces/front.php @@ -6,9 +6,24 @@ */ - namespace Phacil\Framework\Interfaces; +namespace Phacil\Framework\Interfaces; - interface front { +use Phacil\Framework\Action; +use Phacil\Framework\ActionSystem; +interface front { + + /** + * @param ActionSystem $pre_action + * @return void + */ public function addPreAction(\Phacil\Framework\ActionSystem $pre_action); + + /** + * @param Action $action + * @param string $error + * @return void + */ + public function dispatch(\Phacil\Framework\Action $action, $error); + } \ No newline at end of file From 2c58830fc5850b83982a3a5d27401b72ed2cc362 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Fri, 30 Jul 2021 02:44:47 -0300 Subject: [PATCH 13/16] Front case --- system/engine/front.php | 2 +- system/engine/interfaces/front.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/system/engine/front.php b/system/engine/front.php index 065c71d..4b0af5e 100644 --- a/system/engine/front.php +++ b/system/engine/front.php @@ -8,7 +8,7 @@ namespace Phacil\Framework; -use Phacil\Framework\Interfaces\front as frontinterface; +use Phacil\Framework\Interfaces\Front as frontinterface; use Exception; diff --git a/system/engine/interfaces/front.php b/system/engine/interfaces/front.php index d20951d..3f8d9c1 100644 --- a/system/engine/interfaces/front.php +++ b/system/engine/interfaces/front.php @@ -11,7 +11,7 @@ namespace Phacil\Framework\Interfaces; use Phacil\Framework\Action; use Phacil\Framework\ActionSystem; -interface front { +interface Front { /** * @param ActionSystem $pre_action From b3f0088db82030e198a76892a18e3cad40b665c9 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Fri, 30 Jul 2021 02:52:59 -0300 Subject: [PATCH 14/16] loader interface --- system/engine/autoload.php | 3 +- system/engine/interfaces/loader.php | 78 +++++++++++++++++++++++++++++ system/engine/loader.php | 5 +- 3 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 system/engine/interfaces/loader.php diff --git a/system/engine/autoload.php b/system/engine/autoload.php index 7b825c4..9a80eed 100644 --- a/system/engine/autoload.php +++ b/system/engine/autoload.php @@ -52,7 +52,8 @@ spl_autoload_register(function ($class) { 'response', 'classes', 'abstracthelper', - 'interfaces\front' + 'interfaces\front', + 'interfaces\loader' ]; if($namespace[0] == "Phacil" && in_array($classNative, $allowed)){ diff --git a/system/engine/interfaces/loader.php b/system/engine/interfaces/loader.php new file mode 100644 index 0000000..37df8d7 --- /dev/null +++ b/system/engine/interfaces/loader.php @@ -0,0 +1,78 @@ +controller($control); } From d01ca5c256ccaf1f9ecdf1e9e50dad310b781597 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Fri, 30 Jul 2021 12:28:23 -0300 Subject: [PATCH 15/16] More interfaces --- system/engine/action.php | 69 ++++++++++++++++++++++++----- system/engine/autoload.php | 11 +++-- system/engine/controller.php | 2 +- system/engine/front.php | 9 ++-- system/engine/interfaces/action.php | 41 +++++++++++++++++ system/engine/interfaces/front.php | 8 ++-- system/engine/interfaces/loader.php | 36 +++++++-------- 7 files changed, 131 insertions(+), 45 deletions(-) create mode 100644 system/engine/interfaces/action.php diff --git a/system/engine/action.php b/system/engine/action.php index e6c2d7d..01d54ff 100644 --- a/system/engine/action.php +++ b/system/engine/action.php @@ -8,13 +8,38 @@ namespace Phacil\Framework; +use Phacil\Framework\Interfaces\Action as ActionInterface; + /** @package Phacil\Framework */ -final class Action { +final class Action implements ActionInterface { + /** + * + * @var string + */ protected $file; + + /** + * + * @var string + */ protected $class; + + /** + * + * @var string + */ protected $method; + + /** + * + * @var array + */ protected $args = array(); + /** + * + * @var (string[]|string|null)[] + */ private $classAlt = []; /** @@ -121,12 +146,12 @@ final class Action { } /** @return string */ - public function getFile() { + public function getFile():string { return $this->file; } /** @return string */ - public function getClass() { + public function getClass():string { return $this->class; } @@ -145,27 +170,47 @@ final class Action { } /** @return array */ - public function getClassAlt() { + public function getClassAlt():array { return $this->classAlt; } /** @return string */ - public function getMethod() { + public function getMethod():string { return $this->method; } /** @return array */ - public function getArgs() { + public function getArgs():array { return $this->args; } } /** @package Phacil\Framework */ -final class ActionSystem { +final class ActionSystem implements ActionInterface { + + /** + * + * @var string + */ protected $file; + + /** + * + * @var string + */ protected $class; + + /** + * + * @var string + */ protected $method; + + /** + * + * @var array + */ protected $args = array(); /** @@ -225,12 +270,12 @@ final class ActionSystem { } /** @return string */ - public function getFile() { + public function getFile():string { return $this->file; } /** @return string */ - public function getClass() { + public function getClass():string { return $this->class; } @@ -245,17 +290,17 @@ final class ActionSystem { } /** @return array */ - public function getClassAlt() { + public function getClassAlt():array { return $this->classAlt; } /** @return string */ - public function getMethod() { + public function getMethod():string { return $this->method; } /** @return array */ - public function getArgs() { + public function getArgs():array { return $this->args; } } diff --git a/system/engine/autoload.php b/system/engine/autoload.php index 9a80eed..276d723 100644 --- a/system/engine/autoload.php +++ b/system/engine/autoload.php @@ -7,9 +7,6 @@ */ -require_once(DIR_SYSTEM . 'engine/action.php'); -require_once(DIR_SYSTEM . 'engine/controller.php'); - /** @autoload class */ spl_autoload_register(function ($class) { $namespace = explode("\\", $class); @@ -52,8 +49,9 @@ spl_autoload_register(function ($class) { 'response', 'classes', 'abstracthelper', - 'interfaces\front', - 'interfaces\loader' + 'interfaces\\front', + 'interfaces\\loader', + 'interfaces\\action' ]; if($namespace[0] == "Phacil" && in_array($classNative, $allowed)){ @@ -145,5 +143,6 @@ spl_autoload_register(function ($class) { }); - +require_once(DIR_SYSTEM . 'engine/action.php'); +//require_once(DIR_SYSTEM . 'engine/controller.php'); //require_once(DIR_SYSTEM . 'engine/legacy.php'); \ No newline at end of file diff --git a/system/engine/controller.php b/system/engine/controller.php index 2366f12..f7e6aef 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -124,7 +124,7 @@ abstract class Controller { /** * @param string $route * @param array $args - * @return Action + * @return \Phacil\Framework\Interfaces\Action */ protected function forward($route, array $args = array()) { return new Action($route, $args); diff --git a/system/engine/front.php b/system/engine/front.php index 4b0af5e..6ef124b 100644 --- a/system/engine/front.php +++ b/system/engine/front.php @@ -9,6 +9,7 @@ namespace Phacil\Framework; use Phacil\Framework\Interfaces\Front as frontinterface; +use Phacil\Framework\Interfaces\Action; use Exception; @@ -40,10 +41,10 @@ final class Front implements frontinterface { /** - * @param ActionSystem $pre_action + * @param \Phacil\Framework\Interfaces\Action $pre_action * @return void */ - public function addPreAction(\Phacil\Framework\ActionSystem $pre_action) { + public function addPreAction(\Phacil\Framework\Interfaces\Action $pre_action) { $this->pre_action[] = $pre_action; } @@ -53,7 +54,7 @@ final class Front implements frontinterface { * @param string $error * @return void */ - public function dispatch(\Phacil\Framework\Action $action, $error) { + public function dispatch(\Phacil\Framework\Interfaces\Action $action, $error) { $this->error = $error; foreach ($this->pre_action as $pre_action) { @@ -73,7 +74,7 @@ final class Front implements frontinterface { /** * @param object $action - * @return Action + * @return \Phacil\Framework\Interfaces\Action * @throws Exception */ private function execute(object $action) { diff --git a/system/engine/interfaces/action.php b/system/engine/interfaces/action.php new file mode 100644 index 0000000..3a18b9c --- /dev/null +++ b/system/engine/interfaces/action.php @@ -0,0 +1,41 @@ + Date: Fri, 30 Jul 2021 20:56:43 -0300 Subject: [PATCH 16/16] Traits for Actions class --- system/engine/action.php | 135 ++------------------------------ system/engine/autoload.php | 5 +- system/engine/traits/action.php | 86 ++++++++++++++++++++ 3 files changed, 94 insertions(+), 132 deletions(-) create mode 100644 system/engine/traits/action.php diff --git a/system/engine/action.php b/system/engine/action.php index 01d54ff..8006c92 100644 --- a/system/engine/action.php +++ b/system/engine/action.php @@ -8,40 +8,14 @@ namespace Phacil\Framework; -use Phacil\Framework\Interfaces\Action as ActionInterface; +use \Phacil\Framework\Interfaces\Action as ActionInterface; +use \Phacil\Framework\Traits\Action as ActionTrait; /** @package Phacil\Framework */ final class Action implements ActionInterface { - /** - * - * @var string - */ - protected $file; - - /** - * - * @var string - */ - protected $class; - - /** - * - * @var string - */ - protected $method; - - /** - * - * @var array - */ - protected $args = array(); - - /** - * - * @var (string[]|string|null)[] - */ - private $classAlt = []; + use ActionTrait; + /** * @param string $route * @param array $args @@ -145,79 +119,14 @@ final class Action implements ActionInterface { } - /** @return string */ - public function getFile():string { - return $this->file; - } - - /** @return string */ - public function getClass():string { - return $this->class; - } - - private function mountClass(string $namespace, string $class) { - return (defined('NAMESPACE_PREFIX') ? NAMESPACE_PREFIX."\\" : "").str_replace("/", "\\", $namespace)."Controller\\".$class; - } - - /** - * - * @param string $class - * @return $this - */ - public function setClass($class) { - $this->class = $class; - return $this; - } - - /** @return array */ - public function getClassAlt():array { - return $this->classAlt; - } - - /** @return string */ - public function getMethod():string { - return $this->method; - } - /** @return array */ - public function getArgs():array { - return $this->args; - } } /** @package Phacil\Framework */ final class ActionSystem implements ActionInterface { - /** - * - * @var string - */ - protected $file; - - /** - * - * @var string - */ - protected $class; - - /** - * - * @var string - */ - protected $method; - - /** - * - * @var array - */ - protected $args = array(); - - /** - * - * @var (string[]|string|null)[] - */ - private $classAlt = []; + use ActionTrait; /** * @param string $route @@ -269,38 +178,4 @@ final class ActionSystem implements ActionInterface { } } - /** @return string */ - public function getFile():string { - return $this->file; - } - - /** @return string */ - public function getClass():string { - return $this->class; - } - - /** - * - * @param string $class - * @return $this - */ - public function setClass($class) { - $this->class = $class; - return $this; - } - - /** @return array */ - public function getClassAlt():array { - return $this->classAlt; - } - - /** @return string */ - public function getMethod():string { - return $this->method; - } - - /** @return array */ - public function getArgs():array { - return $this->args; - } } diff --git a/system/engine/autoload.php b/system/engine/autoload.php index 276d723..b9c5115 100644 --- a/system/engine/autoload.php +++ b/system/engine/autoload.php @@ -51,12 +51,13 @@ spl_autoload_register(function ($class) { 'abstracthelper', 'interfaces\\front', 'interfaces\\loader', - 'interfaces\\action' + 'interfaces\\action', + 'traits\\action' ]; if($namespace[0] == "Phacil" && in_array($classNative, $allowed)){ try { - include_once(DIR_SYSTEM . 'engine/'. str_replace("\\", "/", $classNative).'.php'); + require_once(DIR_SYSTEM . 'engine/'. str_replace("\\", "/", $classNative).'.php'); return; } catch (\Exception $th) { $log = new \Phacil\Framework\Log(DIR_LOGS."exception.log"); diff --git a/system/engine/traits/action.php b/system/engine/traits/action.php new file mode 100644 index 0000000..4034c91 --- /dev/null +++ b/system/engine/traits/action.php @@ -0,0 +1,86 @@ +file; + } + + /** @return string */ + public function getClass():string { + return $this->class; + } + + private function mountClass(string $namespace, string $class) { + return (defined('NAMESPACE_PREFIX') ? NAMESPACE_PREFIX."\\" : "").str_replace("/", "\\", $namespace)."Controller\\".$class; + } + + /** + * + * @param string $class + * @return $this + */ + public function setClass($class) { + $this->class = $class; + return $this; + } + + /** @return array */ + public function getClassAlt():array { + return $this->classAlt; + } + + /** @return string */ + public function getMethod():string { + return $this->method; + } + + /** @return array */ + public function getArgs():array { + return $this->args; + } +} \ No newline at end of file