From 21977588d80137d925b85d38517ec255a193f887 Mon Sep 17 00:00:00 2001 From: versustunez Date: Sun, 20 Sep 2020 17:15:20 +0200 Subject: [PATCH] - cleaned up some code - added DataLoader - added assetDir - splitted modules and controller namespace --- .gitignore | 4 +- base/config.base.php | 1 + base/lang.base.php | 2 +- base/module.base.php | 2 +- composer.json | 3 +- install/db.sql | 11 +++ lang/{de.php => example.php} | 0 src/Venom/Core/ArgumentHandler.php | 12 ++- src/Venom/Core/DatabaseHandler.php | 4 +- src/Venom/Helper/ErrorHandler.php | 24 +++-- src/Venom/Models/DataModel.php | 90 ++++++++++++++++++ src/Venom/Security/Security.php | 3 +- src/Venom/Venom.php | 25 +++-- src/Venom/Views/Asset.php | 11 ++- src/Venom/Views/DataLoader.php | 94 +++++++++++++++++++ src/Venom/Views/VenomRenderer.php | 16 +++- src/controllers/.gitkeep | 0 .../TestController.php | 2 +- 18 files changed, 263 insertions(+), 41 deletions(-) mode change 100644 => 100755 base/config.base.php mode change 100644 => 100755 base/module.base.php mode change 100644 => 100755 composer.json mode change 100644 => 100755 install/db.sql rename lang/{de.php => example.php} (100%) mode change 100644 => 100755 src/Venom/Core/ArgumentHandler.php mode change 100644 => 100755 src/Venom/Core/DatabaseHandler.php mode change 100644 => 100755 src/Venom/Helper/ErrorHandler.php create mode 100755 src/Venom/Models/DataModel.php mode change 100644 => 100755 src/Venom/Venom.php mode change 100644 => 100755 src/Venom/Views/Asset.php create mode 100755 src/Venom/Views/DataLoader.php mode change 100644 => 100755 src/Venom/Views/VenomRenderer.php create mode 100755 src/controllers/.gitkeep rename src/{modules => controllers}/TestController.php (95%) mode change 100644 => 100755 diff --git a/.gitignore b/.gitignore index 7b54800..b0baee5 100644 --- a/.gitignore +++ b/.gitignore @@ -462,7 +462,7 @@ MigrationBackup/ # real ignore for venom conf/config.inc.php -conf/module.inc.php +conf/modules.inc.php conf/routers.inc.php conf/lang.inc.php -logs/Exception.log \ No newline at end of file +logs/Exception.log diff --git a/base/config.base.php b/base/config.base.php old mode 100644 new mode 100755 index a3f7224..feec79c --- a/base/config.base.php +++ b/base/config.base.php @@ -42,6 +42,7 @@ $config->setSecurity([ // all themes are in __DIR__/public/theme/ $config->setRender([ 'theme' => 'default', //very important! it will search for a folder with this name. + 'assetDir' => 'default', 'baseFile' => 'base', //this will called after all templates are rendered... 'useCache' => false, //is only on big systems good 'cacheName' => 'defaultCache', //this is for bigger systems, ignore it diff --git a/base/lang.base.php b/base/lang.base.php index d770e23..aab698f 100644 --- a/base/lang.base.php +++ b/base/lang.base.php @@ -1,3 +1,3 @@ \Modules\TestController::class, + 'test' => \Controllers\TestController::class, ]; \ No newline at end of file diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 index 00aa673..8e4f0cb --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "autoload": { "psr-4": { "Venom\\": "./src/Venom/", - "Modules\\": "./src/modules/" + "Modules\\": "./src/modules/", + "Controllers\\": "./src/controllers/" } } } diff --git a/install/db.sql b/install/db.sql old mode 100644 new mode 100755 index 5d53092..3e89174 --- a/install/db.sql +++ b/install/db.sql @@ -16,3 +16,14 @@ create table if not exists language isDefault tinyint(1) default 0 null ) comment 'Language File'; + +create table if not exists data +( + id int(255) auto_increment not null unique primary key, + identity varchar(255) not null unique, + isActive tinyint(1) default 1 null, + generated longtext not null, + raw longtext not null, + datatype enum ('content', 'form') +) + comment 'DataLoader File'; diff --git a/lang/de.php b/lang/example.php similarity index 100% rename from lang/de.php rename to lang/example.php diff --git a/src/Venom/Core/ArgumentHandler.php b/src/Venom/Core/ArgumentHandler.php old mode 100644 new mode 100755 index 69e5ac9..9fc9a52 --- a/src/Venom/Core/ArgumentHandler.php +++ b/src/Venom/Core/ArgumentHandler.php @@ -29,14 +29,16 @@ class ArgumentHandler public function getItem(string $key, $default = null) { - if (isset($this->arguments[$key])) { - return $this->arguments[$key]; - } - return $default; + return $this->arguments[$key] ?? $default; } - public function setItem(string $key, $item) + public function setItem(string $key, $item): void { $this->arguments[$key] = $item; } + + public function hasItem(string $key): bool + { + return isset($this->arguments[$key]); + } } \ No newline at end of file diff --git a/src/Venom/Core/DatabaseHandler.php b/src/Venom/Core/DatabaseHandler.php old mode 100644 new mode 100755 index 38b8c09..72e5ffe --- a/src/Venom/Core/DatabaseHandler.php +++ b/src/Venom/Core/DatabaseHandler.php @@ -66,9 +66,9 @@ class DatabaseHandler return $stmt->fetchAll(); } - public function execute(string $query, array $args): void + public function execute(string $query, array $args): bool { $stmt = $this->db->prepare($query); - $stmt->execute($args); + return $stmt->execute($args); } } \ No newline at end of file diff --git a/src/Venom/Helper/ErrorHandler.php b/src/Venom/Helper/ErrorHandler.php old mode 100644 new mode 100755 index 764afca..c3d9e68 --- a/src/Venom/Helper/ErrorHandler.php +++ b/src/Venom/Helper/ErrorHandler.php @@ -8,13 +8,25 @@ use Venom\Core\ArgumentHandler; class ErrorHandler { - + public const ERROR_KEY = 'errorHandler'; public static function setFatalError(): void { self::setError(500); } + public static function setError(int $errorCode): void + { + http_response_code($errorCode); + $handler = ArgumentHandler::get(); + if (!$handler->hasItem('cl')) { + $handler->setItem('cl', 'error'); + $handler->setItem('fnc', 'handleError'); + $handler->setItem('errorCode', $errorCode); + $handler->setItem(self::ERROR_KEY, true); + } + } + public static function setNotFound(): void { self::setError(404); @@ -24,12 +36,4 @@ class ErrorHandler { self::setError(204); } - - public static function setError(int $errorCode): void - { - http_response_code($errorCode); - ArgumentHandler::get()->setItem('cl', 'error'); - ArgumentHandler::get()->setItem('fnc', 'handleError'); - ArgumentHandler::get()->setItem('errorCode', $errorCode); - } -} \ No newline at end of file +} diff --git a/src/Venom/Models/DataModel.php b/src/Venom/Models/DataModel.php new file mode 100755 index 0000000..e5dc2d1 --- /dev/null +++ b/src/Venom/Models/DataModel.php @@ -0,0 +1,90 @@ +id = $id; + $this->type = $type; + $this->raw = $raw; + $this->generated = $generated; + } + + public function getId(): string + { + return $this->id; + } + + public function setId(string $id): void + { + $this->id = $id; + } + + public function getRaw(): string + { + return $this->raw; + } + + public function setRaw(string $raw): void + { + $this->raw = $raw; + } + + public function getGenerated(): string + { + return $this->generated; + } + + public function setGenerated(string $generated): void + { + $this->generated = $generated; + } + + public function getType(): string + { + return $this->type; + } + + public function setType(string $type): void + { + $this->type = $type; + } + + public function validate(): bool + { + return $this->type !== '' && $this->id !== '' && $this->generated !== '' && $this->raw !== ''; + } + + public function isActive(): bool + { + return $this->active === 1; + } + + public function getActive(): int + { + return $this->active; + } + + public function setActive(bool $value): void + { + $this->active = $value ? 1 : 0; + } +} \ No newline at end of file diff --git a/src/Venom/Security/Security.php b/src/Venom/Security/Security.php index c684e71..1585dbd 100644 --- a/src/Venom/Security/Security.php +++ b/src/Venom/Security/Security.php @@ -4,7 +4,6 @@ namespace Venom\Security; - class Security { private static ?Security $instance = null; @@ -28,4 +27,4 @@ class Security { return true; } -} \ No newline at end of file +} diff --git a/src/Venom/Venom.php b/src/Venom/Venom.php old mode 100644 new mode 100755 index d6bb509..d27b073 --- a/src/Venom/Venom.php +++ b/src/Venom/Venom.php @@ -32,6 +32,8 @@ class Venom public function run(): void { + $arguments = ArgumentHandler::get(); + $arguments->setItem(ErrorHandler::ERROR_KEY, false); // we need to load the current controller and the current start template. // after this we can start the renderer if (Config::getInstance()->isRouterEnabled()) { @@ -45,7 +47,10 @@ class Venom } $registry = Registry::getInstance(); $registry->getLang()->initLang(); - $registry->getSeo()->loadSite(); + // if site is errored then dont load via SEO + if (!$arguments->getItem(ErrorHandler::ERROR_KEY)) { + $registry->getSeo()->loadSite(); + } $this->renderer->init($this->findController()); $this->renderer->render(); } @@ -68,7 +73,16 @@ class Venom { $cl = ArgumentHandler::get()->getItem('cl'); if ($cl !== null && isset($this->controllers[$cl])) { - return $this->controllers[$cl]; + return $this->loadController($this->controllers[$cl]); + } + return null; + } + + public function loadController($controllerClass): ?RenderController + { + $controller = new $controllerClass; + if ($controller instanceof RenderController && $controller->register()) { + return $controller; } return null; } @@ -85,12 +99,7 @@ class Venom public function initControllers(array $controllers): void { - foreach ($controllers as $key => $controllerClass) { - $controller = new $controllerClass; - if ($controller instanceof RenderController && $controller->register()) { - $this->controllers[$key] = $controller; - } - } + $this->controllers = $controllers; } public function addRouter(string $name, Router $router): void diff --git a/src/Venom/Views/Asset.php b/src/Venom/Views/Asset.php old mode 100644 new mode 100755 index 77305d4..c4c4e03 --- a/src/Venom/Views/Asset.php +++ b/src/Venom/Views/Asset.php @@ -43,9 +43,10 @@ class Asset public function getImagePath(string $filepath, bool $useAbsolute = false) { - $preDir = '/content/'; + $config = Config::getInstance(); + $preDir = '/' . $config->getRenderer()->uploadDir; if ($useAbsolute) { - $preDir = Config::getInstance()->getBaseUrl() . $preDir; + $preDir = $config->getBaseUrl() . $preDir; } return $preDir . $filepath; } @@ -61,7 +62,7 @@ class Asset usort($this->jsFiles, function ($a, $b) { return $a['pos'] <=> $b['pos']; }); - $theme = $this->getPath('/theme/' . Config::getInstance()->getRenderer()->theme . '/js/'); + $theme = $this->getPath('/theme/' . Config::getInstance()->getRenderer()->assetDir . '/js/'); foreach ($this->jsFiles as $key => $file) { echo ''; } @@ -72,7 +73,7 @@ class Asset $preDir = $base; $config = Config::getInstance(); $baseUrl = Config::getInstance()->getBaseUrl(); - if ($baseUrl != '' && $config->getRenderer()->useStaticUrl) { + if ($baseUrl !== '' && $config->getRenderer()->useStaticUrl) { $preDir = Config::getInstance()->getBaseUrl() . $preDir; } return $preDir; @@ -83,7 +84,7 @@ class Asset usort($this->cssFiles, function ($a, $b) { return $a['pos'] <=> $b['pos']; }); - $theme = $this->getPath('/theme/' . Config::getInstance()->getRenderer()->theme . '/css/'); + $theme = $this->getPath('/theme/' . Config::getInstance()->getRenderer()->assetDir . '/css/'); foreach ($this->cssFiles as $key => $file) { echo ''; } diff --git a/src/Venom/Views/DataLoader.php b/src/Venom/Views/DataLoader.php new file mode 100755 index 0000000..90405b4 --- /dev/null +++ b/src/Venom/Views/DataLoader.php @@ -0,0 +1,94 @@ +getOne('SELECT identity, raw, generated, datatype FROM data WHERE identity = :id and isActive = 1 LIMIT 1', [ + ':id' => $id + ]); + + if ($data !== null) { + $model = new DataModel($data->identity, $data->datatype, $data->raw, $data->generated); + $model->setActive(true); + return $model; + } + return null; + } + + public function updateData(DataModel $model): bool + { + if ($model->getId() === '') { + return $this->insertData($model); + } + return DatabaseHandler::get()->execute( + "UPDATE data SET identity=:id, isActive=:isActive, generated=:gen, raw=:raw, datatype=:dt WHERE identity=:id", + [ + ':id' => $model->getId(), + ':isActive' => $model->getActive(), + ':gen' => $model->getGenerated(), + ':raw' => $model->getRaw(), + ':dt' => $model->getType() + ] + ); + } + + public function insertData(DataModel $model): bool + { + + $this->validateModel($model); + return DatabaseHandler::get()->execute( + "INSERT INTO data (identity, isActive, generated, raw, datatype) VALUES (:id, :isActive, :gen, :raw, :dt)", + [ + ':id' => $model->getId(), + ':isActive' => $model->getActive(), + ':gen' => $model->getGenerated(), + ':raw' => $model->getRaw(), + ':dt' => $model->getType() + ] + ); + } + + private function validateModel(DataModel $model): void + { + if ($model->getId() === '') { + $model->setId($this->generateID()); + } + if (!$model->validate()) { + $id = htmlspecialchars($model->getId()); + throw new RuntimeException("DataModel with id: \"$id\" is invalid!"); + } + } + + private function generateID(string $id = ''): string + { + if ($id === '') { + $id = bin2hex(random_bytes(16)); + } + return $id; + } +} diff --git a/src/Venom/Views/VenomRenderer.php b/src/Venom/Views/VenomRenderer.php old mode 100644 new mode 100755 index 5111120..5e9c51e --- a/src/Venom/Views/VenomRenderer.php +++ b/src/Venom/Views/VenomRenderer.php @@ -16,7 +16,6 @@ class VenomRenderer private array $vars = []; private string $baseTemplate = ''; private string $templateDir = ''; - private string $assetsDir = ''; public function __construct(Venom $venom) { @@ -48,6 +47,13 @@ class VenomRenderer } } + public function renderTemplate($template): void + { + // random variable name... to remove it instantly + echo $this->includeTemplate($template, '1408138186'); + unset($this->vars['1408138186']); + } + /** * function will load a template (without extension!) into a variable and return the content * @param $template @@ -78,12 +84,16 @@ class VenomRenderer return $this->vars[$name]; } + public function deleteVar($name) + { + unset($this->vars[$name]); + } + public function init(?RenderController $controller): void { $this->controller = $controller; $data = Config::getInstance()->getRenderer(); $this->baseTemplate = $data->baseFile . '.php' ?? 'base.php'; $this->templateDir = __DIR__ . '/../../../tpl/' . $data->theme . '/'; - $this->assetsDir = __DIR__ . '/../../../public/theme/' . $data->theme . '/'; } -} \ No newline at end of file +} diff --git a/src/controllers/.gitkeep b/src/controllers/.gitkeep new file mode 100755 index 0000000..e69de29 diff --git a/src/modules/TestController.php b/src/controllers/TestController.php old mode 100644 new mode 100755 similarity index 95% rename from src/modules/TestController.php rename to src/controllers/TestController.php index 87dbc0f..f0bbadd --- a/src/modules/TestController.php +++ b/src/controllers/TestController.php @@ -1,7 +1,7 @@