- 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
commit fe7bacd2f6
14 changed files with 300 additions and 33 deletions

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