VENOM-10: Moved to new Module Structure

Fixed File-Endings
Added Entity-System
This commit is contained in:
Maurice Grönwoldt 2021-01-03 16:59:11 +01:00
commit eb6770204a
101 changed files with 1272 additions and 892 deletions

3
src/Venom/Core/Config.php Normal file → Executable file
View file

@ -2,7 +2,8 @@
namespace Venom\Core;
use Venom\Models\ConfigObject;
use Venom\Core\Database\DatabaseHandler;
use Venom\Entities\ConfigObject;
class Config
{

View file

@ -0,0 +1,102 @@
<?php
namespace Venom\Core\Database;
// class that hold the Database Connection! and Executes like the DatabaseHandler
use PDO;
use PDOException;
use PDOStatement;
use Venom\Entities\DatabaseObject;
class Database
{
// 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 ?\PDO $db = null;
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|EasyQuery $query, array $args = []): ?DatabaseObject
{
$sql = $query;
if ($query instanceof EasyQuery) {
$sql = $query->getQuery();
$args = $query->getArgs();
}
$data = $this->getAll($sql, $args);
if (count($data) > 0) {
return $data[0];
}
return null;
}
public function getAll(string|EasyQuery $query, array $args = []): array
{
$sql = $query;
if ($query instanceof EasyQuery) {
$sql = $query->getQuery();
$args = $query->getArgs();
}
$stmt = $this->db->prepare($sql);
$stmt->setFetchMode(PDO::FETCH_CLASS, DatabaseObject::class);
$stmt->execute($args);
return $stmt->fetchAll();
}
public function execute(string|EasyQuery $query, array $args = []): bool
{
$sql = $query;
if ($query instanceof EasyQuery) {
$sql = $query->getQuery();
$args = $query->getArgs();
}
$stmt = $this->db->prepare($sql);
return $stmt->execute($args);
}
public function createStatement($query): bool|PDOStatement
{
$stmt = $this->db->prepare($query);
$stmt->setFetchMode(PDO::FETCH_CLASS, DatabaseObject::class); // set to default fetch-mode :D
return $stmt;
}
public function setClass($stmt, $class)
{
$stmt->setFetchMode(PDO::FETCH_CLASS, $class);
}
public function start()
{
$this->db->beginTransaction();
}
public function commit()
{
$this->db->commit();
}
public function rollBack()
{
$this->db->rollBack();
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace Venom\Core\Database;
class DatabaseHandler
{
private static ?DatabaseHandler $instance = null;
private Database $db;
private array $cache; //EntityManager Cache!
protected function __construct()
{
$this->db = new Database();
}
public static function get(): Database
{
return self::getInstance()->db;
}
public static function getInstance(): DatabaseHandler
{
if (self::$instance === null) {
self::$instance = new DatabaseHandler();
}
return self::$instance;
}
public static function getEntityManager($entityClass): EntityManager
{
$instance = self::getInstance();
// i dont make sure this class exist because the user should do this ;)
if (!isset($instance->cache[$entityClass])) {
$instance->cache[$entityClass] = new EntityManager($entityClass, self::get());
}
return $instance->cache[$entityClass];
}
}

View file

@ -0,0 +1,220 @@
<?php
namespace Venom\Core\Database;
// the QueryBuilder is stupid! dont use it for Very Complex Queries because it's should do Entity Loading Easier :)
class EasyQuery
{
const ORDER_ASC = 0;
const ORDER_DESC = 1;
const WHERE_AND = "AND";
const WHERE_AND_NOT = "AND NOT";
const WHERE_OR = "OR";
const WHERE_OR_NOT = "OR NOT";
const WHERE_NOT = "NOT";
private array $where = [];
private array $args = [];
private string $query = "";
private int $limit = -1;
private int $offset = 0;
private string $whereStmt = "";
private string $havingStmt = "";
private array $order = [];
private array $groupBy = [];
private array $having = [];
public function __construct(private string $tableName, private array $fields = [])
{
}
public static function createSelect(array $fields, string $table): string
{
return "SELECT " . implode(", ", $fields) . " FROM " . $table;
}
public function setWhere(string $statement): static
{
$this->whereStmt = $statement;
return $this;
}
public function setHaving(string $statement): static
{
$this->havingStmt = $statement;
return $this;
}
public function setLimit(int $limit): static
{
$this->limit = $limit;
return $this;
}
public function setOffset(int $offset): static
{
$this->offset = $offset;
return $this;
}
public function addField($field, $as = ""): static
{
if ($as !== "") {
$field .= " AS " . $as;
}
$this->fields[] = $field;
return $this;
}
public function addFields(array $fields): static
{
foreach ($fields as $field) {
$this->fields[] = $field;
}
return $this;
}
public function where($key, $value, $type = "AND"): static
{
$this->where[] = [$key, $type];
$this->args[":" . $key] = $value;
return $this;
}
public function having($key, $value, $type = "AND"): static
{
$this->having[] = [$key, $type];
$this->args[":" . $key] = $value;
return $this;
}
public function orderBy(string $key, int $mode = self::ORDER_ASC): static
{
$this->order[] = $mode === self::ORDER_DESC ? $key . " DESC" : $key;
return $this;
}
public function groupBy(string $key): static
{
$this->groupBy[] = $key;
return $this;
}
public function setArg($key, $value): static
{
$this->args[":" . $key] = $value;
return $this;
}
// returns a Query
public function addArgAndField($key, $value): static
{
$this->args[":" . $key] = $value;
$this->fields[] = $key;
return $this;
}
public function buildSelect(): static
{
// we build an easyQuery Builder that can very easy stuff
$query = self::createSelect($this->fields, $this->tableName);
if (count($this->where) > 0) {
$this->whereStmt = $this->parseStmt($this->where, $this->whereStmt);
}
if (count($this->having) > 0) {
$this->havingStmt = $this->parseStmt($this->having, $this->havingStmt);
}
if ($this->whereStmt !== "") {
$query .= " WHERE " . $this->whereStmt;
}
if (count($this->groupBy)) {
$query .= " GROUP BY " . implode(", ", $this->groupBy);
}
if ($this->havingStmt !== "") {
$query .= " HAVING " . $this->havingStmt;
}
if (count($this->order)) {
$query .= " ORDER BY " . implode(", ", $this->order);
}
if ($this->offset > 0) {
$query .= " OFFSET " . $this->offset;
}
if ($this->limit > 0) {
$query .= " LIMIT " . $this->limit;
}
$this->query = $query;
return $this;
}
public function buildInsertQuery(): static
{
$query = "INSERT INTO " . $this->tableName;
$joinedFields = implode(", ", $this->fields);
$values = implode(", ", array_keys($this->args));
$query .= "(" . $joinedFields . ") VALUES (" . $values . ")";
$this->query = $query;
return $this;
}
public function buildUpdateQuery(): static
{
$query = "UPDATE " . $this->tableName . " SET ";
$setFields = [];
foreach ($this->fields as $field) {
$setFields[] = $field . " = :" . $field;
}
$query .= implode(", ", $setFields);
if (count($this->where) > 0) {
$this->whereStmt = $this->parseStmt($this->where, $this->whereStmt);
}
if ($this->whereStmt !== "") {
$query .= " WHERE " . $this->whereStmt;
}
$this->query = $query;
return $this;
}
public function buildDeleteQuery(): static
{
$query = "DELETE FROM " . $this->tableName;
if (count($this->where) > 0) {
$this->whereStmt = $this->parseStmt($this->where, $this->whereStmt);
}
if ($this->whereStmt !== "") {
$query .= " WHERE " . $this->whereStmt;
}
$this->query = $query;
return $this;
}
public function getQuery(): string
{
return $this->query;
}
public function getArgs(): array
{
return $this->args;
}
public function getFields(): array
{
return $this->fields;
}
private function parseStmt($items, $default = ""): string
{
$query = $default;
foreach ($items as $item) {
if ($query !== "") {
$query .= " " . $item[1] . " ";
}
if ($item[1] === self::WHERE_NOT && $query === "") {
$query .= "NOT ";
}
$query .= $item[0] . " = :" . $item[0];
}
return $query;
}
}

View file

@ -0,0 +1,127 @@
<?php
namespace Venom\Core\Database;
// Entity has a Load and Save function!
// The Entity needs to have a primary key... most of the time this is a id!
use JsonSerializable;
use RuntimeException;
abstract class Entity implements JsonSerializable
{
public static string $tableName = "";
// make sure this exists!
public int $id = -1;
public string $primaryKey = "id";
public array $loadedFields = [];
public array $blackList = ["id"];
// Please override this Property in the Class you implement the Abstract Class! this is needed to run the right SQL calls
public ?array $fields = null;
// Override this if you want special fields :)
public function getFieldsToWrite(): array
{
if ($this->fields !== null) {
return $this->fields;
}
$localBlacklist = array_merge(["primaryKey", "tableName", "loadedFields", "blackList", "fields"], $this->blackList);
$allLoaded = in_array("*", $this->loadedFields);
$vars = get_object_vars($this);
foreach ($vars as $key => $var) {
if (in_array($key, $localBlacklist)) {
unset($vars[$key]);
}
}
if (!$allLoaded) {
foreach ($vars as $key => $var) {
if (!in_array($var, $this->loadedFields)) {
unset($vars[$key]);
}
}
}
unset($vars[$this->primaryKey]);
$this->fields = $vars;
return $this->fields;
}
public function save(): bool
{
$this->preSave();
$primaryKey = $this->primaryKey;
$fields = $this->removeEmptyFields($this->getFieldsToWrite());
$query = new EasyQuery(static::$tableName);
foreach ($fields as $key => $field) {
$query->addArgAndField($key, $field);
}
if ($this->$primaryKey === "") {
$query->buildInsertQuery();
} else {
$query->where($primaryKey, $this->$primaryKey)->buildUpdateQuery();
}
return DatabaseHandler::get()->execute($query);
}
public function load($fields = ['*'], ?EasyQuery $query = null): static
{
if ($query === null) {
$primaryKey = $this->primaryKey;
$query = new EasyQuery(static::$tableName, $fields);
$query->where($primaryKey, $this->$primaryKey)->setLimit(1)->buildSelect();
} else {
$query->setLimit(1)->buildSelect();
}
$item = DatabaseHandler::get()->getOne($query);
if ($item === null) {
return $this;
}
$lazy = $item->getData();
$this->id = $item->id;
foreach ($lazy as $key => $item) {
$this->$key = $item;
}
$this->fields = null;
$this->loadedFields = array_merge($this->loadedFields, $query->getFields());
$this->postLoad();
return $this;
}
public function __set($name, $value)
{
// Implement your own if you want to override this behaviour!
throw new RuntimeException("Write to Property: $name that is not Available in the Entity!");
}
public function delete()
{
$key = $this->primaryKey;
$query = new EasyQuery(self::$tableName);
$query->setArg($this->primaryKey, $this->$key)->buildDeleteQuery();
DatabaseHandler::get()->execute($query->getQuery(), $query->getArgs());
}
public function jsonSerialize(): array
{
return $this->getFieldsToWrite();
}
public function preSave()
{
}
public function postLoad()
{
}
private function removeEmptyFields(array $vars): array
{
foreach ($vars as $name => $item) {
if (empty($item) && $name != $this->primaryKey) {
unset($vars[$name]);
}
}
return $vars;
}
}

View file

@ -0,0 +1,127 @@
<?php
namespace Venom\Core\Database;
use Exception;
class EntityManager
{
/** @var Entity[] */
private array $entities = [];
public function __construct(private string $classType, private Database $db)
{
}
public static function create($callable): EntityManager
{
return DatabaseHandler::getEntityManager($callable);
}
public function createEntity()
{
$ent = new $this->classType;
$this->entities[] = $ent;
return $ent;
}
public function addEntity(Entity $entity)
{
$this->entities[] = $entity;
}
public function removeEntity(Entity $entity)
{
foreach ($this->entities as $key => $item) {
if ($entity === $item) {
unset($this->entities[$key]);
break;
}
}
}
public function findBy($key, $value): ?Entity
{
foreach ($this->entities as $entity) {
if ($entity->$key === $value) {
return $entity;
}
}
return null;
}
public function saveAll()
{
if (count($this->entities) === 0) {
return;
}
try {
$this->db->start();
foreach ($this->entities as $entity) {
$entity->save();
}
$this->db->commit();
} catch (Exception $ex) {
trigger_error($ex->getMessage());
$this->db->rollBack();
}
}
public function deleteEntities()
{
try {
$this->db->start();
foreach ($this->entities as $entity) {
$entity->delete();
}
$this->db->commit();
} catch (Exception $ex) {
trigger_error($ex->getMessage());
$this->db->rollBack();
}
}
public function clearAll()
{
$this->entities = [];
}
public function load(string|EasyQuery $query, $args = [], array $fields = ["*"])
{
$sql = $query;
if ($query instanceof EasyQuery) {
$query->buildSelect();
$sql = $query->getQuery();
$args = $query->getArgs();
$fields = $query->getFields();
}
$stmt = $this->db->createStatement($sql);
$this->db->setClass($stmt, $this->classType);
if ($stmt->execute($args)) {
/** @var Entity[] $all */
$all = $stmt->fetchAll();
foreach ($all as $item) {
$item->loadedFields = $fields;
$item->postLoad();
$this->addEntity($item);
}
}
}
public function execute($query): bool
{
return $this->db->execute($query);
}
public function getAll(): array
{
return $this->entities;
}
private function addEntities(array $entities)
{
foreach ($entities as $entity) {
$this->entities[] = $entity;
}
}
}

View file

@ -1,92 +0,0 @@
<?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 = []): bool
{
$stmt = $this->db->prepare($query);
return $stmt->execute($args);
}
// Returns a Select like this: SELECT id, name, ... FROM table || do what you want
public static function createEasySelect(array $fields, string $table): string
{
return "SELECT " . implode(",", $fields) . " FROM " . $table;
}
public static function getUpdateString(array $data, string $table, string $where): array
{
$string = [];
$save = [];
foreach ($data as $key => $value) {
$k = ":" . strtolower($key);
$string[] = $key . "= " . $k;
$save[$k] = $value;
}
return ["UPDATE " . $table . " SET " . implode(",", $string) . " " . $where, $save];
}
}

5
src/Venom/Core/Language.php Normal file → Executable file
View file

@ -5,7 +5,8 @@ namespace Venom\Core;
use RuntimeException;
use Venom\Models\DatabaseObject;
use Venom\Core\Database\DatabaseHandler;
use Venom\Entities\DatabaseObject;
class Language
{
@ -20,7 +21,7 @@ class Language
public function initLang()
{
$lang = ArgumentHandler::get()->getItem("lang", $this->defaultLang->shortTag);
$lang = ArgumentHandler::get()->getItem("lang", $this->defaultLang->shortTag ?? 'de');
//check if language exists
$data = DatabaseHandler::get()->getOne("select id from language where shortTag = :shortTag", [
':shortTag' => $lang

13
src/Venom/Core/Module.php Normal file → Executable file
View file

@ -3,18 +3,17 @@
namespace Venom\Core;
use Venom\Venom;
interface Module
{
const NAME = "name";
const AUTHOR = "author";
const SECURE = "secure";
const ROUTE = "routes";
const ADMIN_ROUTE = "adminRoutes";
const DESC = "description";
public function register(Venom $venom): bool;
public function init(): void;
const TEMPLATES = "templates";
const ADMIN_TEMPLATES = "adminTemplates";
const CONTROLLER = "controllers";
const TEMPLATE_PATH = "tplPath";
const ACTIVE = "isActive";
}

72
src/Venom/Core/ModuleLoader.php Executable file
View file

@ -0,0 +1,72 @@
<?php
namespace Venom\Core;
use RuntimeException;
use Venom\Helper\TemplateUtil;
use Venom\Routing\Route;
use Venom\Routing\Router;
use Venom\Venom;
class ModuleLoader
{
public static function getModules(): array
{
return [
'Meta',
'User',
'Data',
'Role',
'SEO',
'VenomStatus',
];
}
public static function loadModule(string $name, Venom $venom)
{
// load module search in the Module Path for a module.php file
$dir = __DIR__ . "/../../modules/" . $name . "/module.php";
if (!file_exists($dir)) {
throw new RuntimeException("Module File: \"$dir\" Not found");
}
include_once $dir;
}
public static function initModule(array $module, Venom $venom): bool
{
if (!$module[Module::ACTIVE]) {
return false;
}
// register Router, Templates and more :)
$isAdmin = Config::getInstance()->isAdmin();
if ($isAdmin) {
self::registerRoutes($module,
$venom,
$module[Module::ADMIN_ROUTE],
$venom->getRouter(Router::ADMIN_ROUTER)
);
TemplateUtil::getInstance()->addTemplates($module[Module::ADMIN_TEMPLATES], $module[Module::TEMPLATE_PATH]);
} else {
self::registerRoutes($module,
$venom,
$module[Module::ROUTE],
$venom->getRouter(Router::DEFAULT_ROUTER)
);
TemplateUtil::getInstance()->addTemplates($module[Module::TEMPLATES], $module[Module::TEMPLATE_PATH]);
}
$venom->addControllers($module[Module::CONTROLLER]);
return true;
}
public static function registerRoutes(array $module, Venom $venom, array $routes, Router $router)
{
foreach ($routes as $key => $route) {
/** @var Route $route */
$route->module = $module[Module::NAME];
$route->isSecure = $module[Module::SECURE];
$route->venom = $venom;
$router->addRoute($key, $route);
}
}
}

0
src/Venom/Core/Registry.php Normal file → Executable file
View file

3
src/Venom/Core/Setup.php Normal file → Executable file
View file

@ -39,9 +39,6 @@ class Setup
if (isset($modules)) {
$venom->initModules($modules);
}
if (isset($controllers)) {
$venom->initControllers($controllers);
}
}
public static function loadRouters(Venom $venom): void