rework structure
added Router added .htaccess removed API-Module => is not used renamed batch to CRON
This commit is contained in:
parent
ccf2f506f0
commit
c33bb4cb3a
37 changed files with 680 additions and 190 deletions
42
src/Venom/Core/ArgumentHandler.php
Normal file
42
src/Venom/Core/ArgumentHandler.php
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Venom\Core;
|
||||
|
||||
|
||||
class ArgumentHandler
|
||||
{
|
||||
public static ?ArgumentHandler $instance = null;
|
||||
private array $arguments = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
foreach ($_GET as $key => $item) {
|
||||
$this->arguments[htmlspecialchars($key)] = htmlspecialchars($item);
|
||||
}
|
||||
foreach ($_POST as $key => $item) {
|
||||
$this->arguments[htmlspecialchars($key)] = htmlspecialchars($item);
|
||||
}
|
||||
}
|
||||
|
||||
public static function get(): ArgumentHandler
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new ArgumentHandler();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function getItem(string $key, $default = null)
|
||||
{
|
||||
if (isset($this->arguments[$key])) {
|
||||
return $this->arguments[$key];
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function setItem(string $key, $item)
|
||||
{
|
||||
$this->arguments[$key] = $item;
|
||||
}
|
||||
}
|
||||
181
src/Venom/Core/Config.php
Normal file
181
src/Venom/Core/Config.php
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
<?php
|
||||
|
||||
namespace Venom\Core;
|
||||
|
||||
use Venom\Models\ConfigObject;
|
||||
|
||||
class Config
|
||||
{
|
||||
private static ?Config $instance = null;
|
||||
private bool $isWriteable = true;
|
||||
private float $version = 1.0;
|
||||
private ConfigObject $mail;
|
||||
private ConfigObject $renderer;
|
||||
private ConfigObject $security;
|
||||
private bool $maintenance = false;
|
||||
private bool $devMode = false;
|
||||
private bool $isAdmin = false;
|
||||
private string $baseUrl = '';
|
||||
private bool $seoMode = false;
|
||||
private bool $useRouter = false;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
$this->mail = new ConfigObject();
|
||||
$this->renderer = new ConfigObject();
|
||||
$this->security = new ConfigObject();
|
||||
}
|
||||
|
||||
public static function getInstance(): Config
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new Config();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function setDatabase(array $array): void
|
||||
{
|
||||
if ($this->isWriteable) {
|
||||
DatabaseHandler::get()->init($array);
|
||||
} else {
|
||||
trigger_error('try to write closed config!');
|
||||
}
|
||||
}
|
||||
|
||||
public function setRender(array $array): void
|
||||
{
|
||||
$this->set('renderer', $array);
|
||||
}
|
||||
|
||||
public function set(string $variable, $value): void
|
||||
{
|
||||
if (!$this->isWriteable) {
|
||||
trigger_error('try to write closed config!');
|
||||
return;
|
||||
}
|
||||
if ($this->$variable instanceof ConfigObject) {
|
||||
$this->$variable->setData($value);
|
||||
} else {
|
||||
$this->$variable = $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function setMaintainMode(bool $mode): void
|
||||
{
|
||||
$this->set('maintenance', $mode);
|
||||
}
|
||||
|
||||
public function getVersion(): float
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
public function setVersion(float $param): void
|
||||
{
|
||||
$this->set('version', $param);
|
||||
}
|
||||
|
||||
public function getMail(): ConfigObject
|
||||
{
|
||||
return $this->mail;
|
||||
}
|
||||
|
||||
public function setMail(array $array): void
|
||||
{
|
||||
$this->set('mail', $array);
|
||||
}
|
||||
|
||||
public function getRenderer(): ConfigObject
|
||||
{
|
||||
return $this->renderer;
|
||||
}
|
||||
|
||||
public function isMaintenance(): bool
|
||||
{
|
||||
return $this->maintenance;
|
||||
}
|
||||
|
||||
public function isDevMode(): bool
|
||||
{
|
||||
return $this->devMode;
|
||||
}
|
||||
|
||||
public function setDevMode(bool $mode): void
|
||||
{
|
||||
$this->set('devMode', $mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isAdmin(): bool
|
||||
{
|
||||
return $this->isAdmin;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $isAdmin
|
||||
*/
|
||||
public function setIsAdmin(bool $isAdmin): void
|
||||
{
|
||||
$this->set('isAdmin', $isAdmin);
|
||||
}
|
||||
|
||||
public function setSeoMode(bool $mode): void
|
||||
{
|
||||
$this->set('seoMode', $mode);
|
||||
}
|
||||
|
||||
public function getSeoEnabled(): bool
|
||||
{
|
||||
return $this->seoMode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isClosed(): bool
|
||||
{
|
||||
return !$this->isWriteable;
|
||||
}
|
||||
|
||||
/**
|
||||
* function to close the write mode... this make sure after the config is init no other tool can write to it!
|
||||
*/
|
||||
public function close(): void
|
||||
{
|
||||
$this->isWriteable = false;
|
||||
}
|
||||
|
||||
public function getBaseUrl(): string
|
||||
{
|
||||
return $this->baseUrl;
|
||||
}
|
||||
|
||||
public function setBaseUrl(string $url): void
|
||||
{
|
||||
$this->set('baseUrl', $url);
|
||||
}
|
||||
|
||||
public function getSecurity(): ConfigObject
|
||||
{
|
||||
return $this->security;
|
||||
}
|
||||
|
||||
public function setSecurity(array $security): void
|
||||
{
|
||||
$this->set('security', $security);
|
||||
}
|
||||
|
||||
public function setEnableRouter(bool $value): void
|
||||
{
|
||||
$this->set('useRouter', $value);
|
||||
}
|
||||
|
||||
public function isRouterEnabled(): bool
|
||||
{
|
||||
return $this->useRouter;
|
||||
}
|
||||
|
||||
}
|
||||
74
src/Venom/Core/DatabaseHandler.php
Normal file
74
src/Venom/Core/DatabaseHandler.php
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Venom\Core;
|
||||
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use Venom\Models\DatabaseObject;
|
||||
|
||||
/**
|
||||
* Singleton DatabaseHandler... make sure we only have one connection to the database..
|
||||
* @package Venom\Database
|
||||
*/
|
||||
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;
|
||||
|
||||
public static function get(): DatabaseHandler
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function init(array $data): void
|
||||
{
|
||||
//init instance with the current data... only working if the db is not init!
|
||||
if ($this->db != null) {
|
||||
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): ?DatabaseObject
|
||||
{
|
||||
$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);
|
||||
}
|
||||
}
|
||||
61
src/Venom/Core/Language.php
Normal file
61
src/Venom/Core/Language.php
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Venom\Core;
|
||||
|
||||
|
||||
use RuntimeException;
|
||||
use Venom\Models\DatabaseObject;
|
||||
|
||||
class Language
|
||||
{
|
||||
private string $language = "";
|
||||
private array $languages = [];
|
||||
private ?DatabaseObject $defaultLang;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->defaultLang = DatabaseHandler::get()->getOne("select language, shortTag from language WHERE isActive = 1 and isDefault = 1", []);
|
||||
}
|
||||
|
||||
public function initLang()
|
||||
{
|
||||
$lang = ArgumentHandler::get()->getItem("lang", $this->defaultLang->shortTag);
|
||||
//check if language exists
|
||||
$data = DatabaseHandler::get()->getOne("select id from language where shortTag = :shortTag", [
|
||||
':shortTag' => $lang
|
||||
]);
|
||||
|
||||
if (isset($data->id)) {
|
||||
$this->language = $lang;
|
||||
} else {
|
||||
throw new RuntimeException("Language \"$lang\" not found");
|
||||
}
|
||||
}
|
||||
|
||||
public function registerLang(string $key, array $values)
|
||||
{
|
||||
$this->languages[$key] = $values;
|
||||
}
|
||||
|
||||
public function getCurrentLang()
|
||||
{
|
||||
return $this->language;
|
||||
}
|
||||
|
||||
public function getTranslations()
|
||||
{
|
||||
return $this->languages[$this->language] ?: [];
|
||||
}
|
||||
|
||||
public function getTranslation($key)
|
||||
{
|
||||
if (!isset($this->languages[$this->language])) {
|
||||
return $key;
|
||||
}
|
||||
if (isset($this->languages[$this->language][$key])) {
|
||||
return $this->languages[$this->language][$key];
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
}
|
||||
12
src/Venom/Core/Module.php
Normal file
12
src/Venom/Core/Module.php
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Venom\Core;
|
||||
|
||||
|
||||
interface Module
|
||||
{
|
||||
public function register(): bool;
|
||||
|
||||
public function init(): void;
|
||||
}
|
||||
41
src/Venom/Core/Registry.php
Normal file
41
src/Venom/Core/Registry.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Venom\Core;
|
||||
|
||||
use Venom\Routing\SeoController;
|
||||
|
||||
/**
|
||||
* Singleton Class... hold current URL => can
|
||||
* @package Venom
|
||||
*/
|
||||
class Registry
|
||||
{
|
||||
private static ?Registry $instance = null;
|
||||
private SeoController $seo;
|
||||
private Language $lang;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
$this->seo = new SeoController();
|
||||
$this->lang = new Language();
|
||||
}
|
||||
|
||||
public static function getInstance(): Registry
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new Registry();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function getSeo(): SeoController
|
||||
{
|
||||
return $this->seo;
|
||||
}
|
||||
|
||||
public function getLang(): Language
|
||||
{
|
||||
return $this->lang;
|
||||
}
|
||||
}
|
||||
58
src/Venom/Core/Setup.php
Normal file
58
src/Venom/Core/Setup.php
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Venom\Core;
|
||||
|
||||
|
||||
use Venom\Venom;
|
||||
|
||||
class Setup
|
||||
{
|
||||
public static function loadConfig(bool $isAdmin): void
|
||||
{
|
||||
$config = Config::getInstance();
|
||||
$config->setIsAdmin($isAdmin);
|
||||
$file = self::tryLoading('config.inc.php', 'config.base.php', "Config");
|
||||
require $file;
|
||||
}
|
||||
|
||||
public static function tryLoading(string $file, string $baseFile, string $type): string
|
||||
{
|
||||
$newFile = __DIR__ . '/../../../conf/' . $file;
|
||||
if (!file_exists($newFile)) {
|
||||
$newBaseFile = __DIR__ . '/../../../base/' . $baseFile;
|
||||
if (copy($newBaseFile, $newFile)) {
|
||||
echo 'Created File for: ' . $type . '! Please Adjust the file';
|
||||
} else {
|
||||
echo 'Cannot create File for: ' . $type . '!';
|
||||
}
|
||||
|
||||
exit(1);
|
||||
}
|
||||
return $newFile;
|
||||
}
|
||||
|
||||
public static function loadModules(Venom $venom): void
|
||||
{
|
||||
$file = self::tryLoading('modules.inc.php', 'module.base.php', "Modules");
|
||||
require $file;
|
||||
if (isset($modules)) {
|
||||
$venom->initModules($modules);
|
||||
}
|
||||
if (isset($controllers)) {
|
||||
$venom->initControllers($controllers);
|
||||
}
|
||||
}
|
||||
|
||||
public static function loadRouters(Venom $venom): void
|
||||
{
|
||||
$file = self::tryLoading('routers.inc.php', 'router.base.php', "Routers");
|
||||
require $file;
|
||||
}
|
||||
|
||||
public static function loadLanguage(): void
|
||||
{
|
||||
$file = self::tryLoading('lang.inc.php', 'lang.base.php', "Languages");
|
||||
require $file;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue