- added ADMIN index file

- cleanup config
- added Asset Controller
This commit is contained in:
Maurice Grönwoldt 2020-05-31 17:00:05 +02:00
parent 7ba4e3e0a6
commit fe7bacd2f6
14 changed files with 300 additions and 33 deletions

View file

@ -17,16 +17,23 @@ $config->setDatabase([
// used for external batch-mailing and error reporting.
// please generate login data at: api.vstz.dev
// api will check this data before processing
$config->setAPIAuth([
'useAPI' => true,
'url' => 'https://api.vstz.dev/v1/',
'user' => 'vstz',
'pw' => '6(f&B~ZxH3DfC#qJ',
'logsError' => true
]);
/**
* BATCH Mailing is something that will send only mails after a specific time like 1 min.
* it is used to prevent spamming.
* batch mailing only works via API!... if API not used BatchMailing will disabled... or you have a cron that works like a batch...
*/
$config->setMail([
'useBatch' => true, //if true it will not send mails.
'writeToDB' => true, //is needed for batch and is always true if batch is used
'writeToDB' => true, //is needed for batch and is always true if batch is use
//this stuff is only important if not using batch mailing!
'host' => 'localhost',
'port' => '587',
@ -43,9 +50,11 @@ $config->setRender([
'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
'uploadDir' => 'content/'
]);
$config->setMaintainMode(false);
$config->setDevMode(true);
$config->setBaseUrl(''); // can changed to something like that: https://example.com !not enter a / after the url! this will break all
$config->close();

View file

@ -5,5 +5,7 @@ $modules = [];
// register controllers that can handle templates ;) need to have a render function for this
$controllers = [
'test' => \Modules\TestController::class
'test' => \Modules\TestController::class,
//api-controller
'apiMailer' => \Modules\API\APIMailer::class,
];

30
public/admin/index.php Normal file
View file

@ -0,0 +1,30 @@
<?php
use Venom\Config;
use Venom\Venom;
require_once '../../vendor/autoload.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();
}
$config = Config::getInstance();
$config->setIsAdmin(true);
require_once '../../config.inc.php';
require_once '../../module.inc.php';
if ($config->isDevMode()) {
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
}
$venom = new Venom();
$venom->initModules($modules);
$venom->initControllers($controllers);
$venom->run();

0
public/content/.gitkeep Normal file
View file

View file

@ -0,0 +1,25 @@
* {
box-sizing: border-box;
}
html, body {
margin: 0;
font-size: 16px;
color: #fff;
background-color: #333;
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100vw;
}
header {
font-size: 5vw;
cursor: pointer;
}
header:hover {
color: #ff0323;
}

View file

View file

@ -0,0 +1,37 @@
<?php
namespace Venom;
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;
}
}

View file

@ -14,6 +14,8 @@ class Config
private array $renderer = [];
private bool $maintenance = false;
private bool $devMode = false;
private bool $isAdmin = false;
private string $baseUrl = '';
private function __construct()
{
@ -38,29 +40,17 @@ class Config
public function setAPIAuth(array $array): void
{
if ($this->isWriteable) {
$this->api = $array;
} else {
trigger_error('try to write closed config!');
}
$this->set('api', $array);
}
public function setRender(array $array): void
{
if ($this->isWriteable) {
$this->renderer = $array;
} else {
trigger_error('try to write closed config!');
}
$this->set('renderer', $array);
}
public function setMaintainMode(bool $mode): void
{
if ($this->isWriteable) {
$this->maintenance = $mode;
} else {
trigger_error('try to write closed config!');
}
$this->set('maintenance', $mode);
}
public function getVersion(): float
@ -70,12 +60,7 @@ class Config
public function setVersion(float $param): void
{
if ($this->isWriteable) {
$this->version = $param;
} else {
trigger_error('try to write closed config!');
}
$this->set('version', $param);
}
public function getApi(): array
@ -90,11 +75,7 @@ class Config
public function setMail(array $array): void
{
if ($this->isWriteable) {
$this->mail = $array;
} else {
trigger_error('try to write closed config!');
}
$this->set('mail', $array);
}
public function getRenderer(): array
@ -114,11 +95,35 @@ class Config
public function setDevMode(bool $mode): void
{
if ($this->isWriteable) {
$this->devMode = $mode;
} else {
trigger_error('try to write closed config!');
}
$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 setBaseUrl(string $url) {
$this->set('baseUrl', $url);
}
/**
* @return bool
*/
public function isClosed(): bool
{
return !$this->isWriteable;
}
/**
@ -128,4 +133,17 @@ class Config
{
$this->isWriteable = false;
}
public function set(string $variable, $value) {
if (!$this->isWriteable) {
trigger_error('try to write closed config!');
return;
}
$this->$variable = $value;
}
public function getBaseUrl()
{
return $this->baseUrl;
}
}

View file

@ -4,6 +4,7 @@
namespace Venom;
use Venom\Views\Asset;
use Venom\Views\RenderController;
use Venom\Views\VenomRenderer;
@ -16,6 +17,7 @@ class Venom
public function __construct()
{
$this->renderer = new VenomRenderer($this);
Asset::get()->setRenderer($this->renderer);
}
public function run(): void

89
src/Venom/Views/Asset.php Normal file
View file

@ -0,0 +1,89 @@
<?php
namespace Venom\Views;
use Venom\Config;
class Asset
{
static ?Asset $instance = null;
private ?VenomRenderer $renderer = null;
private array $jsFiles = [];
private array $cssFiles = [];
private function __construct()
{
}
public static function get(): Asset
{
if (self::$instance == null) {
self::$instance = new Asset();
}
return self::$instance;
}
public function addJS(string $name, string $filepath, $pos = 99999)
{
$this->jsFiles[$name] = [
'file' => $filepath,
'pos' => $pos
];
}
public function addCSS(string $name, string $filepath, $pos = 99999)
{
$this->cssFiles[$name] = [
'file' => $filepath,
'pos' => $pos
];
}
public function getImagePath(string $filepath, bool $useAbsolute = false)
{
$preDir = '/content';
if ($useAbsolute) {
$preDir = Config::getInstance()->getBaseUrl() . $preDir;
}
}
public function setRenderer(VenomRenderer $renderer)
{
$this->renderer = $renderer;
}
//this will output all js files! sorted by position
public function renderJS()
{
usort($this->jsFiles, function ($a, $b) {
return $a['pos'] <=> $b['pos'];
});
$theme = $this->getPath('/theme/' . Config::getInstance()->getRenderer()['theme'] . '/js/');
foreach ($this->jsFiles as $key => $file) {
echo '<script src="' . $theme . $file['file'] . '" id="js-' . $key . '"></script>';
}
}
public function renderCSS()
{
usort($this->cssFiles, function ($a, $b) {
return $a['pos'] <=> $b['pos'];
});
$theme = $this->getPath('/theme/' . Config::getInstance()->getRenderer()['theme'] . '/css/');
foreach ($this->cssFiles as $key => $file) {
echo '<link rel="stylesheet" href="' . $theme . $file['file'] . '" id="css-' . $key . '">';
}
}
private function getPath($base)
{
$preDir = $base;
$baseUrl = Config::getInstance()->getBaseUrl();
if ($baseUrl != '') {
$preDir = Config::getInstance()->getBaseUrl() . $preDir;
}
return $preDir;
}
}

View file

@ -4,6 +4,7 @@
namespace Venom\Views;
use Venom\ArgumentHandler;
use Venom\Config;
use Venom\Venom;
@ -24,10 +25,16 @@ class VenomRenderer
public function render(): void
{
$isAsync = false;
if ($this->controller) {
ob_start();
$this->controller->render($this);
$this->templateData = ob_get_clean();
$isAsync = $this->controller->getTemplate() === 'async';
}
if ($isAsync || ArgumentHandler::get()->getItem('async', 'false') === 'true') {
echo $this->templateData;
exit;
}
$this->loadBasicTemplate();
}

View file

@ -0,0 +1,33 @@
<?php
namespace Modules\API;
use Venom\Config;
use Venom\Views\RenderController;
use Venom\Views\VenomRenderer;
class APIMailer implements RenderController
{
private bool $useAPI = false;
public function register(): bool
{
$this->useAPI = Config::getInstance()->getApi()['useAPI'] === true;
return true;
}
public function render(VenomRenderer $renderer): bool
{
//check for batch rendering... this is very important!
echo 'batch...';
return $this->useAPI;
}
//return async to say the backend that this should exit after rendering
public function getTemplate(): string
{
return 'async';
}
}

View file

@ -4,6 +4,7 @@
namespace Modules;
use Venom\Views\Asset;
use Venom\Views\RenderController;
use Venom\Views\VenomRenderer;
@ -17,6 +18,8 @@ class TestController implements RenderController
public function render(VenomRenderer $renderer): bool
{
Asset::get()->addJS('test', 'test.js');
Asset::get()->addCSS('test', 'test.css');
return true;
}

View file

@ -1,3 +1,9 @@
<?php
use Venom\Config;
use Venom\Views\Asset;
?>
<!doctype html>
<html lang="en">
<head>
@ -6,11 +12,17 @@
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>VenomBase</title>
<?php
Asset::get()->renderCSS();
?>
</head>
<body>
<header>
VenomCMS
</header>
<?= $this->templateData ?>
<?php
Asset::get()->renderJS();
?>
</body>
</html>