- added SEO-URL-Mode

- added Language support
This commit is contained in:
Maurice Grönwoldt 2020-07-24 11:08:07 +02:00
parent fe7bacd2f6
commit ccf2f506f0
13 changed files with 201 additions and 7 deletions

View File

@ -56,5 +56,6 @@ $config->setRender([
$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->setSeoMode(true); //seo mode is to lookup the complete entered url in the database! without query-parameters and load the get parameters from it
$config->close();

18
install/db.sql Normal file
View File

@ -0,0 +1,18 @@
create table if not exists seoData
(
id int(255) auto_increment not null unique primary key,
seo varchar(255) not null,
raw varchar(255) not null,
isActive tinyint(1) default 1 null
)
comment 'seo url mapping';
create table if not exists language
(
id int(255) auto_increment not null unique primary key,
language varchar(255) not null,
shortTag varchar(255) not null,
isActive tinyint(1) default 1 null,
isDefault tinyint(1) default 0 null
)
comment 'Language File';

3
lang.php Normal file
View File

@ -0,0 +1,3 @@
<?php
require_once __DIR__ . '/lang/de.php';

7
lang/de.php Normal file
View File

@ -0,0 +1,7 @@
<?php
use Venom\Registry;
Registry::getInstance()->getLang()->registerLang('de', [
'HEADLINE' => 'VenomCMS',
'TEST_TRANSLATION' => 'Das ist ein Test :)',
]);

View File

@ -16,6 +16,7 @@ if (!file_exists('../module.inc.php')) {
}
require_once '../config.inc.php';
require_once '../module.inc.php';
require_once '../lang.php';
$config = Config::getInstance();
if ($config->isMaintenance()) {

View File

@ -34,4 +34,9 @@ class ArgumentHandler
}
return $default;
}
public function setItem(string $key, $item)
{
$this->arguments[$key] = $item;
}
}

View File

@ -16,6 +16,7 @@ class Config
private bool $devMode = false;
private bool $isAdmin = false;
private string $baseUrl = '';
private bool $seoMode = false;
private function __construct()
{
@ -118,6 +119,14 @@ class Config
$this->set('baseUrl', $url);
}
public function setSeoMode(bool $mode) {
$this->set('seoMode', $mode);
}
public function getSeoEnabled() {
return $this->seoMode;
}
/**
* @return bool
*/

View File

@ -48,7 +48,7 @@ class DatabaseHandler
}
}
public function getOne(string $query, array $args): ?array
public function getOne(string $query, array $args): ?DatabaseObject
{
$data = $this->getAll($query, $args);
if (count($data) > 0) {

61
src/Venom/Language.php Normal file
View File

@ -0,0 +1,61 @@
<?php
namespace Venom;
use Venom\Database\DatabaseHandler;
use Venom\Database\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;
}
}

View File

@ -4,13 +4,39 @@
namespace Venom;
use Venom\SEO\SeoController;
/**
* Singleton Class... hold current URL => can
* @package Venom
*/
class Registry
{
public function __construct()
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;
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace Venom\SEO;
use Venom\ArgumentHandler;
use Venom\Config;
use Venom\Database\DatabaseHandler;
class SeoController
{
private bool $shouldUse;
private $data = null;
public function __construct()
{
$this->shouldUse = Config::getInstance()->getSeoEnabled();
}
public function loadSite()
{
if (!$this->shouldUse) {
return;
}
$url = htmlspecialchars(parse_url($_SERVER['REQUEST_URI'])['path']);
$data = DatabaseHandler::get()->getOne("SELECT * FROM seoData WHERE seo = :url", [
':url' => $url,
]);
$this->data = $data;
if($this->data != null) {
parse_str(parse_url($this->data->raw)['query'], $queryItems);
foreach($queryItems as $key => $item) {
ArgumentHandler::get()->setItem($key, $item);
}
}
}
public function addSite()
{
}
public function deleteSite()
{
}
public function getData()
{
return $this->data;
}
}

View File

@ -24,14 +24,18 @@ class Venom
{
// we need to load the current controller and the current start template.
// after this we can start the renderer
$registry = Registry::getInstance();
$registry->getSeo()->loadSite();
$registry->getLang()->initLang();
$this->renderer->init($this->findController());
$this->renderer->render();
}
private function findController(): ?RenderController
{
if (isset($_GET['cl'], $this->controllers[$_GET['cl']])) {
return $this->controllers[$_GET['cl']];
$cl = ArgumentHandler::get()->getItem('cl');
if ($cl != null && isset($this->controllers[$cl])) {
return $this->controllers[$cl];
}
return null;
}

View File

@ -1,8 +1,10 @@
<?php
use Venom\Config;
use Venom\Registry;
use Venom\Views\Asset;
$reg = Registry::getInstance();
$lang = $reg->getLang();
?>
<!doctype html>
<html lang="en">
@ -18,9 +20,12 @@ use Venom\Views\Asset;
</head>
<body>
<header>
VenomCMS
<?=$lang->getTranslation("HEADLINE");?>
</header>
<?= $this->templateData ?>
<?php
$lang->getTranslation("TEST_TRANSLATION");
echo $this->templateData;
?>
<?php
Asset::get()->renderJS();
?>