diff --git a/.gitignore b/.gitignore index f861f83..e6d45be 100644 --- a/.gitignore +++ b/.gitignore @@ -457,4 +457,9 @@ healthchecksdb # Backup folder for Package Reference Convert tool in Visual Studio 2017 MigrationBackup/ -# End of https://www.gitignore.io/api/composer,intellij+all,visualstudio,visualstudiocode \ No newline at end of file +# End of https://www.gitignore.io/api/composer,intellij+all,visualstudio,visualstudiocode + + +# real ignore for venom +config.inc.php +module.inc.php \ No newline at end of file diff --git a/composer.json b/composer.json index 6a8608c..00aa673 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,9 @@ "email": "mauricegroenwoldt@gmail.com" } ], - "require": {}, + "require": { + "ext-pdo": "*" + }, "autoload": { "psr-4": { "Venom\\": "./src/Venom/", diff --git a/config.inc.php b/config.base.php similarity index 63% rename from config.inc.php rename to config.base.php index 32b91e3..a4baae4 100644 --- a/config.inc.php +++ b/config.base.php @@ -1,20 +1,21 @@ setVersion(1.0); $config->setDatabase([ - 'type' => 'mysql', //please change only if you now what you're doing! this can break a lot. - 'host' => '127.0.0.1', - 'port' => '3306', //default port is 3306 - 'user' => 'venom', - 'pw' => 'venomPassword', - 'db' => 'venomCMS', - 'extra' => ';' + DatabaseHandler::DB_TYPE => 'mysql', //please change only if you now what you're doing! this can break a lot. + DatabaseHandler::DB_HOST => '127.0.0.1', + DatabaseHandler::DB_PORT => '3306', //default port is 3306 + DatabaseHandler::DB_USER => 'venom', + DatabaseHandler::DB_PASSWORD => 'venomPassword', + DatabaseHandler::DB_DB => 'venomCMS', + DatabaseHandler::DB_EXTRA => '' // need to start with ';' ]); -// used for batch-mailing and error reporting. +// used for external batch-mailing and error reporting. // please generate login data at: api.vstz.dev $config->setAPIAuth([ 'url' => 'https://api.vstz.dev/v1/', @@ -30,7 +31,7 @@ $config->setMail([ 'host' => 'localhost', 'port' => '587', 'useTLS' => true, //use startTLS. is the default case ;) here it's important the security Cert is secure... - 'user' => 'no-reply@vstz.dev', + 'user' => 'youruser@yourdomain.de', 'password' => 'this-is-secret', 'from' => 'info@venom.io' ]); @@ -39,6 +40,7 @@ $config->setMail([ // all themes are in __DIR__/public/theme/ $config->setRender([ 'theme' => 'default', //very important! it will search for a folder with this name. + 'baseFile' => 'base.php', //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/module.base.php b/module.base.php new file mode 100644 index 0000000..c1a1ca6 --- /dev/null +++ b/module.base.php @@ -0,0 +1,7 @@ + need to have the Module Class at parent with the init function ;) +$modules = []; + +// register controllers that can handle templates ;) need to have a render function for this +$controllers = []; \ No newline at end of file diff --git a/public/index.php b/public/index.php old mode 100644 new mode 100755 index 3cd152d..1cc9a02 --- a/public/index.php +++ b/public/index.php @@ -1,3 +1,28 @@ isMaintenance()) { + echo 'Currently not available'; + exit; +} +$venom = new Venom(); +$venom->initModules($modules); +$venom->initControllers($controllers); +$venom->run(); \ No newline at end of file diff --git a/src/Venom/Config.php b/src/Venom/Config.php index e149427..e664ba3 100644 --- a/src/Venom/Config.php +++ b/src/Venom/Config.php @@ -27,16 +27,6 @@ class Config return self::$instance; } - public function setVersion(float $param): void - { - if ($this->isWriteable) { - $this->version = $param; - } else { - trigger_error('try to write closed config!'); - } - - } - public function setDatabase(array $array): void { if ($this->isWriteable) { @@ -55,15 +45,6 @@ class Config } } - public function setMail(array $array): void - { - if ($this->isWriteable) { - $this->mail = $array; - } else { - trigger_error('try to write closed config!'); - } - } - public function setRender(array $array): void { if ($this->isWriteable) { @@ -82,18 +63,21 @@ class Config } } - public function setDevMode(bool $mode): void - { - if ($this->isWriteable) { - $this->devMode = $mode; - } - } - public function getVersion(): float { return $this->version; } + public function setVersion(float $param): void + { + if ($this->isWriteable) { + $this->version = $param; + } else { + trigger_error('try to write closed config!'); + } + + } + public function getApi(): array { return $this->api; @@ -104,6 +88,15 @@ class Config return $this->mail; } + public function setMail(array $array): void + { + if ($this->isWriteable) { + $this->mail = $array; + } else { + trigger_error('try to write closed config!'); + } + } + public function getRenderer(): array { return $this->renderer; @@ -119,6 +112,15 @@ class Config return $this->devMode; } + public function setDevMode(bool $mode): void + { + if ($this->isWriteable) { + $this->devMode = $mode; + } else { + trigger_error('try to write closed config!'); + } + } + /** * function to close the write mode... this make sure after the config is init no other tool can write to it! */ diff --git a/src/Venom/Database/DatabaseHandler.php b/src/Venom/Database/DatabaseHandler.php index ff21834..e98046d 100644 --- a/src/Venom/Database/DatabaseHandler.php +++ b/src/Venom/Database/DatabaseHandler.php @@ -5,6 +5,7 @@ namespace Venom\Database; use PDO; +use PDOException; /** * Singleton DatabaseHandler... make sure we only have one connection to the database.. @@ -12,6 +13,14 @@ use PDO; */ class DatabaseHandler { + // constants + public const DB_TYPE = 'type'; + public const DB_HOST = 'host'; + public const DB_PORT = 'port'; + public const DB_USER = 'user'; + public const DB_PASSWORD = 'pw'; + public const DB_DB = 'db'; + public const DB_EXTRA = 'extra'; private static ?self $instance = null; private ?PDO $db = null; @@ -30,7 +39,35 @@ class DatabaseHandler return; } $dsn = '%s:host=%s;dbname=%s;port=%s'; - + $connectString = sprintf($dsn, $data[self::DB_TYPE], $data[self::DB_HOST], $data[self::DB_DB], $data[self::DB_PORT] . $data[self::DB_EXTRA]); + try { + $this->db = new PDO($connectString, $data[self::DB_USER], $data[self::DB_PASSWORD]); + } catch (PDOException $e) { + trigger_error($e->getMessage()); + die($e->getCode()); + } } + public function getOne(string $query, array $args): ?array + { + $data = $this->getAll($query, $args); + if (count($data) > 0) { + return $data[0]; + } + return null; + } + + public function getAll(string $query, array $args): array + { + $stmt = $this->db->prepare($query); + $stmt->setFetchMode(PDO::FETCH_CLASS, DatabaseObject::class); + $stmt->execute($args); + return $stmt->fetchAll(); + } + + public function execute(string $query, array $args): void + { + $stmt = $this->db->prepare($query); + $stmt->execute($args); + } } \ No newline at end of file diff --git a/src/Venom/Module.php b/src/Venom/Module.php new file mode 100644 index 0000000..8120ddc --- /dev/null +++ b/src/Venom/Module.php @@ -0,0 +1,12 @@ + can + * @package Venom + */ +class Registry +{ + public function __construct() + { + } +} \ No newline at end of file diff --git a/src/Venom/Venom.php b/src/Venom/Venom.php index e421e2c..1382879 100644 --- a/src/Venom/Venom.php +++ b/src/Venom/Venom.php @@ -4,7 +4,55 @@ namespace Venom; +use Venom\Views\RenderController; +use Venom\Views\VenomRenderer; + class Venom { + private VenomRenderer $renderer; + private array $controllers = []; + private array $modules = []; + public function __construct() + { + $this->renderer = new VenomRenderer($this); + } + + public function run(): void + { + // we need to load the current controller and the current start template. + // after this we can start the renderer + $this->renderer->init(null); + $this->renderer->render(); + } + + public function initModules(array $modules): void + { + foreach ($modules as $key => $moduleClass) { + $module = new $moduleClass; + if ($module instanceof Module && $module->register()) { + $this->modules[$key] = $module; + } + } + } + + public function initControllers(array $controllers): void + { + foreach ($controllers as $key => $controllerClass) { + $controller = new $controllerClass; + if ($controller instanceof RenderController && $controller->register()) { + $this->controllers[$key] = $controller; + } + } + } + + private function prepare() + { + + } + + private function findController() + { + + } } \ No newline at end of file diff --git a/src/Venom/Views/RenderController.php b/src/Venom/Views/RenderController.php new file mode 100644 index 0000000..cb724f8 --- /dev/null +++ b/src/Venom/Views/RenderController.php @@ -0,0 +1,14 @@ +venom = $venom; + } + + public function render(): void + { + if ($this->controller) { + ob_start(); + $this->controller->render(); + $this->templateData = ob_end_clean(); + } + $this->loadBasicTemplate(); + } + + public function loadBasicTemplate(): void + { + if (file_exists($this->templateDir . $this->baseTemplate)) { + include_once $this->templateDir . $this->baseTemplate; + } else { + echo "Base Template not found..."; + echo $this->templateData; + } + } + + public function init(?RenderController $controller): void + { + $this->controller = $controller; + $data = Config::getInstance()->getRenderer(); + $this->baseTemplate = $data['baseFile'] ?? 'base.php'; + $this->templateDir = __DIR__ . '/../../../tpl/' . $data['theme'] . '/'; + $this->assetsDir = __DIR__ . '/../../../public/theme/' . $data['theme'] . '/'; + } +} \ No newline at end of file