diff --git a/system/encryption/autoload.php b/system/encryption/autoload.php index 3432eab..61f07ea 100644 --- a/system/encryption/autoload.php +++ b/system/encryption/autoload.php @@ -40,6 +40,10 @@ class Encryption { $this->key = $this->hash($key); } + public function getKey() { + return $this->key; + } + /** * @param string $value * @return string|false diff --git a/system/engine/controller.php b/system/engine/controller.php index 388be3f..f2ee97e 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -224,31 +224,50 @@ abstract class Controller implements \Phacil\Framework\Interfaces\Controller { $classAlt = $action->getClassAlt(); $method = $action->getMethod(); - if (file_exists($file)) { + if ($file && file_exists($file)) { require_once($file); foreach($classAlt as $classController){ try { if(class_exists($classController)){ $this->registry->routeOrig = $child; - $controller = new $classController($this->registry); + $controller = $this->registry->injectionClass($classController); + //$controller = new $classController($this->registry); + if (is_callable([$controller, $method])) { + call_user_func_array(array($controller, $method), $args); + } else { + throw new \Phacil\Framework\Exception("Error Processing Request", 1); + } break; } - } catch (Exception $th) { - //throw $th; + } catch (\Phacil\Framework\Exception\Throwable $th) { + throw $th; } } - $controller->$method($args); - $this->registry->routeOrig = null; return $controller->output; + + } elseif (!$file && isset($classAlt['class']) && !empty($classAlt['class']) && class_exists($classAlt['class'])) { + try { + $this->registry->routeOrig = $child; + $controller = $this->registry->injectionClass($classAlt['class']); + if (is_callable(array($controller, $method))) { + call_user_func_array(array($controller, $method), $args); + } else { + throw new \Phacil\Framework\Exception("Error Processing Request", 1); + } + + $this->registry->routeOrig = null; + + return $controller->output; + } catch (\Phacil\Framework\Exception\Throwable $th) { + throw ($th); + } } else { throw new Exception("Could not load controller " . $child . '!', 1); - - //exit(); } } @@ -266,7 +285,10 @@ abstract class Controller implements \Phacil\Framework\Interfaces\Controller { $this->data[basename($child)] = $this->getChild($child); } - $tpl = new \Phacil\Framework\Render($this->registry); + /** + * @var \Phacil\Framework\Render + */ + $tpl = $this->registry->getInstance("Phacil\Framework\Render"); $pegRout = explode("/", ($this->registry->routeOrig)?: \Phacil\Framework\startEngineExacTI::getRoute()); @@ -281,23 +303,35 @@ abstract class Controller implements \Phacil\Framework\Interfaces\Controller { $routePatterned = array_map($noCaseFunction, $pegRout); $routeWithoutLastPatterned = $routePatterned; $lastRoutePatterned = array_pop($routeWithoutLastPatterned); - $routeWithoutFirstPatterned = $routePatterned; - $firstRoutePatterned = array_shift($routeWithoutFirstPatterned); + //$routeWithoutFirstPatterned = $routePatterned; + //$firstRoutePatterned = array_shift($routeWithoutFirstPatterned); $structure = []; - $structure[self::TEMPLATE_AREA_THEME][] = $thema . '/' . implode("/", $routePatterned); + if ($thema != "default") + $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; + $positions = 2; + + for ($i=1; $i <= $positions; $i++) { + if(count($routePatterned) <= ($i)) break; + $mount = $routePatterned; + array_splice($mount, $i, 0, 'View'); + $structure[self::TEMPLATE_AREA_MODULAR][] = implode("/", $mount); + + if(($i+1) < count($routePatterned)) + $structure[self::TEMPLATE_AREA_MODULAR][] = implode("/", array_slice($mount, 0, -1)). "_" .end($mount); + } + + if (count($routePatterned) > 2) { //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 ; + if($thema != "default") + $structure[self::TEMPLATE_AREA_THEME][] = $thema . '/' . implode("/", $routeWithoutLastPatterned) . '_' . $lastRoutePatterned ; + + $structure[self::TEMPLATE_AREA_THEME][] = 'default/' . implode("/", $routeWithoutLastPatterned) . '_' . $lastRoutePatterned; } //Check if theme exists @@ -325,7 +359,7 @@ abstract class Controller implements \Phacil\Framework\Interfaces\Controller { } }); - $templatePath = $findThemeFile($structure[self::TEMPLATE_AREA_THEME], Config::DIR_TEMPLATE()) ?: $findThemeFile($structure[self::TEMPLATE_AREA_MODULAR], Config::DIR_APP_MODULAR()); + $templatePath = $findThemeFile($structure[self::TEMPLATE_AREA_THEME], Config::DIR_TEMPLATE()) ?: $findThemeFile($structure[self::TEMPLATE_AREA_MODULAR], Config::DIR_APP_MODULAR()); } else { if(file_exists(Config::DIR_APP_MODULAR(). $pegRout[0] ."/View/" .$this->template)){ $templatePath = Config::DIR_APP_MODULAR(). $pegRout[0] ."/View/"; @@ -351,7 +385,7 @@ abstract class Controller implements \Phacil\Framework\Interfaces\Controller { throw new Exception('Error: template not seted!'); } - if (file_exists($templatePath . $this->template)) { + if (is_readable($templatePath . $this->template)) { $templateFileInfo = pathinfo($templatePath .$this->template); $templateType = $templateFileInfo['extension']; diff --git a/system/engine/registry.php b/system/engine/registry.php index 33fa704..c79430f 100644 --- a/system/engine/registry.php +++ b/system/engine/registry.php @@ -142,8 +142,9 @@ final class Registry { if(is_string($class)) { $classCreate = self::checkPreference($class); - $reflector = new ReflectionClass($classCreate); - return self::setAutoInstance($reflector->newInstanceArgs($args), $class); + return self::getInstance()->injectionClass($classCreate, $args, true); + /* $reflector = new ReflectionClass($classCreate); + return self::setAutoInstance($reflector->newInstanceArgs($args), $class); */ } if (is_object($class)) { @@ -310,7 +311,7 @@ final class Registry { * @throws \Exception * @throws \Phacil\Framework\Exception */ - public function injectionClass($class, $args = array(), $forceCreate = false, $factored = null) + public function injectionClass($class, $args = array(), $forceCreate = false) { $argsToInject = !empty($args) ? $args : false; $originalClass = $class; diff --git a/system/engine/render.php b/system/engine/render.php index e94f021..9d7516a 100644 --- a/system/engine/render.php +++ b/system/engine/render.php @@ -95,11 +95,11 @@ /** * - * @param mixed $templateType - * @param mixed $templatePath - * @param mixed $template - * @param mixed $data - * @param mixed $extras + * @param string $templateType + * @param string $templatePath + * @param string $template + * @param array $data + * @param array $extras * @return $this */ public function setTemplate($templateType, $templatePath, $template, $data, $extras) { diff --git a/system/templateEngines/Twig/Extension/ExacTITranslate.php b/system/templateEngines/Twig/Extension/ExacTITranslate.php index 2bec143..0ddb136 100644 --- a/system/templateEngines/Twig/Extension/ExacTITranslate.php +++ b/system/templateEngines/Twig/Extension/ExacTITranslate.php @@ -177,12 +177,8 @@ class transExtension extends \Twig\Extension\AbstractExtension function traduzir() { $params = func_get_args(); $body = array_shift($params); - - if (class_exists('Translate')) { - $trans = new \Phacil\Framework\Translate(); - echo ($trans->translation($body)); - } else { - echo $body; - } - + + /** @var \Phacil\Framework\Translate */ + $trans = \Phacil\Framework\Registry::getInstance("Phacil\Framework\Translate"); + echo ($trans->translation($body)); } \ No newline at end of file diff --git a/system/translate/autoload.php b/system/translate/autoload.php index e049b51..29f14d1 100644 --- a/system/translate/autoload.php +++ b/system/translate/autoload.php @@ -11,9 +11,10 @@ namespace Phacil\Framework; use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; use Phacil\Framework\Registry; -/** @package Phacil\Framework */ - -final class Translate { +/** + * @package Phacil\Framework + */ +class Translate { /** * @@ -38,6 +39,18 @@ final class Translate { * @var \Phacil\Framework\Caches */ protected $cache; + + /** + * + * @var string|null + */ + private $cookie; + + /** + * + * @var string + */ + protected $table = 'translate'; public function __construct(){ @@ -45,7 +58,7 @@ final class Translate { $this->autoLang = (isset($this->session->data['lang'])) ? $this->session->data['lang'] : NULL; - $this->cookie = (Request::COOKIE(['lang'])) ?: NULL; + $this->cookie = (Request::COOKIE('lang')) ?: NULL; $this->cache = Registry::getInstance()->cache; @@ -56,6 +69,16 @@ final class Translate { } } + + /** + * + * @param string $table + * @return $this + */ + public function setTranslateTable($table){ + $this->table = $table; + return $this; + } /** * @param string $value @@ -71,33 +94,37 @@ final class Translate { return $this->cache->get("lang_".$lang."_".md5($value)); } else { - $sql = "SELECT * FROM translate WHERE text = '".$this->db->escape($value)."'"; - $result = $this->db->query($sql, false); + $result = $this->db->query()->select()->from($this->table); + $result->where()->equals('text', $value)->end(); + $result = $result->load(); - if ($result->num_rows == 1) { - if(isset($result->row[$lang]) and $result->row[$lang] != "") { - $this->cache->set("lang_".$lang."_".md5($value), $result->row[$lang], false); - return $result->row[$lang]; //valid translation present + if ($result->getNumRows() == 1) { + if($lang && $result->getRow()->getValue($lang) and !empty($result->getRow()->getValue($lang))) { + $this->cache->set("lang_".$lang."_".md5($value), $result->getRow()->getValue($lang), false); + return $result->getRow()->getValue($lang); //valid translation present } else { return $value; } - } else { //message not found in the table + } elseif($result->getNumRows() < 1) { //message not found in the table //add unfound message to the table with empties translations $this->insertBaseText($value); - return $value; + } } - - + + return $value; } /** * @param string $value * @return void */ - public function insertBaseText ($value){ - $this->db->query("INSERT INTO translate SET text='".$this->db->escape($value)."'"); + public function insertBaseText ($value){ + $this->db->query()->insert()->setTable($this->table)->setValues([ + 'text' => $value + ])->load(); + return $this; } } \ No newline at end of file