still wip

This commit is contained in:
VersusTune 2020-03-28 20:11:51 +01:00
parent d21ca3e6ab
commit 9be884c8e4
12 changed files with 261 additions and 39 deletions

7
.gitignore vendored
View file

@ -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
# End of https://www.gitignore.io/api/composer,intellij+all,visualstudio,visualstudiocode
# real ignore for venom
config.inc.php
module.inc.php

View file

@ -8,7 +8,9 @@
"email": "mauricegroenwoldt@gmail.com"
}
],
"require": {},
"require": {
"ext-pdo": "*"
},
"autoload": {
"psr-4": {
"Venom\\": "./src/Venom/",

View file

@ -1,20 +1,21 @@
<?php
use Venom\Config;
use Venom\Database\DatabaseHandler;
$config = Config::getInstance();
$config->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
]);

7
module.base.php Normal file
View file

@ -0,0 +1,7 @@
<?php
//register modules -> 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 = [];

27
public/index.php Normal file → Executable file
View file

@ -1,3 +1,28 @@
<?php
use Venom\Config;
use Venom\Venom;
require_once '../vendor/autoload.php';
require_once '../config.inc.php';
if (!file_exists('../config.inc.php')) {
copy('../config.base.php', '../config.inc.php');
echo 'Please change Config @ config.inc.php';
exit();
}
if (!file_exists('../module.inc.php')) {
copy('../module.base.php', '../module.inc.php');
echo 'Please change Modules @ module.inc.php';
exit();
}
require_once '../config.inc.php';
require_once '../module.inc.php';
$config = Config::getInstance();
if ($config->isMaintenance()) {
echo 'Currently not available';
exit;
}
$venom = new Venom();
$venom->initModules($modules);
$venom->initControllers($controllers);
$venom->run();

View file

@ -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!
*/

View file

@ -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);
}
}

12
src/Venom/Module.php Normal file
View file

@ -0,0 +1,12 @@
<?php
namespace Venom;
interface Module
{
public function register(): bool;
public function init(): void;
}

16
src/Venom/Registry.php Normal file
View file

@ -0,0 +1,16 @@
<?php
namespace Venom;
/**
* Singleton Class... hold current URL => can
* @package Venom
*/
class Registry
{
public function __construct()
{
}
}

View file

@ -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()
{
}
}

View file

@ -0,0 +1,14 @@
<?php
namespace Venom\Views;
interface RenderController
{
public function register(): bool;
public function render(): string;
public function getTemplate(): string;
}

View file

@ -0,0 +1,52 @@
<?php
namespace Venom\Views;
use Venom\Config;
use Venom\Venom;
class VenomRenderer
{
private Venom $venom;
private ?RenderController $controller;
private string $templateData = '';
private string $baseTemplate = '';
private string $templateDir = '';
private string $assetsDir = '';
public function __construct(Venom $venom)
{
$this->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'] . '/';
}
}