Router, Logger, Bump v1.5

This commit is contained in:
2022-05-31 16:14:49 +02:00
parent 5bb0d1419f
commit 658157167e
33 changed files with 968 additions and 267 deletions

View File

@@ -0,0 +1,94 @@
<?php
namespace Driver\Logger;
use Driver\SQL\SQL;
class Logger {
public const LOG_FILE_DATE_FORMAT = "Y-m-d_H-i-s_v";
public const LOG_LEVELS = [
0 => "debug",
1 => "info",
2 => "warning",
3 => "error",
4 => "severe"
];
public static Logger $INSTANCE;
private ?SQL $sql;
private string $module;
public function __construct(string $module = "Unknown", ?SQL $sql = null) {
$this->module = $module;
$this->sql = $sql;
}
protected function getStackTrace(int $pop = 2): string {
$debugTrace = debug_backtrace();
if ($pop > 0) {
array_splice($debugTrace, 0, $pop);
}
return implode("\n", array_map(function ($trace) {
return $trace["file"] . "#" . $trace["line"] . ": " . $trace["function"] . "()";
}, $debugTrace));
}
public function log(string $message, string $severity, bool $appendStackTrace = true) {
if ($appendStackTrace) {
$message .= "\n" . $this->getStackTrace();
}
if ($this->sql !== null && $this->sql->isConnected()) {
$success = $this->sql->insert("SystemLog", ["module", "message", "severity"])
->addRow($this->module, $message, $severity)
->execute();
if ($success !== false) {
return;
}
}
// database logging failed, try to log to file
$module = preg_replace("/[^a-zA-Z0-9-]/", "-", $this->module);
$date = (\DateTime::createFromFormat('U.u', microtime(true)))->format(self::LOG_FILE_DATE_FORMAT);
$logFile = implode("_", [$module, $severity, $date]) . ".log";
$logPath = implode(DIRECTORY_SEPARATOR, [WEBROOT, "core", "Logs", $logFile]);
@file_put_contents($logPath, $message);
}
public function error(string $message): string {
$this->log($message, "error");
return $message;
}
public function severe(string $message): string {
$this->log($message, "severe");
return $message;
}
public function warning(string $message): string {
$this->log($message, "warning", false);
return $message;
}
public function info(string $message): string {
$this->log($message, "info", false);
return $message;
}
public function debug(string $message): string {
$this->log($message, "debug");
return $message;
}
public static function instance(): Logger {
if (self::$INSTANCE === null) {
self::$INSTANCE = new Logger("Global");
}
return self::$INSTANCE;
}
}

View File

@@ -59,7 +59,7 @@ class MySQL extends SQL {
);
if (mysqli_connect_errno()) {
$this->lastError = "Failed to connect to MySQL: " . mysqli_connect_error();
$this->lastError = $this->logger->severe("Failed to connect to MySQL: " . mysqli_connect_error());
$this->connection = NULL;
return false;
}
@@ -164,20 +164,20 @@ class MySQL extends SQL {
}
$success = true;
} else {
$this->lastError = "PreparedStatement::get_result failed: $stmt->error ($stmt->errno)";
$this->lastError = $this->logger->error("PreparedStatement::get_result failed: $stmt->error ($stmt->errno)");
}
} else {
$success = true;
}
} else {
$this->lastError = "PreparedStatement::execute failed: $stmt->error ($stmt->errno)";
$this->lastError = $this->logger->error("PreparedStatement::execute failed: $stmt->error ($stmt->errno)");
}
} else {
$this->lastError = "PreparedStatement::prepare failed: $stmt->error ($stmt->errno)";
$this->lastError = $this->logger->error("PreparedStatement::prepare failed: $stmt->error ($stmt->errno)");
}
}
} catch (\mysqli_sql_exception $exception) {
$this->lastError = "MySQL::execute failed: $stmt->error ($stmt->errno)";
$this->lastError = $this->logger->error("MySQL::execute failed: $stmt->error ($stmt->errno)");
} finally {
if ($res !== null && !is_bool($res)) {
$res->close();
@@ -214,7 +214,7 @@ class MySQL extends SQL {
return " ON DUPLICATE KEY UPDATE " . implode(",", $updateValues);
} else {
$strategyClass = get_class($strategy);
$this->lastError = "ON DUPLICATE Strategy $strategyClass is not supported yet.";
$this->lastError = $this->logger->error("ON DUPLICATE Strategy $strategyClass is not supported yet.");
return null;
}
}
@@ -243,7 +243,7 @@ class MySQL extends SQL {
} else if($column instanceof JsonColumn) {
return "LONGTEXT"; # some maria db setups don't allow JSON here…
} else {
$this->lastError = "Unsupported Column Type: " . get_class($column);
$this->lastError = $this->logger->error("Unsupported Column Type: " . get_class($column));
return NULL;
}
}
@@ -251,17 +251,17 @@ class MySQL extends SQL {
public function getColumnDefinition(Column $column): ?string {
$columnName = $this->columnName($column->getName());
$defaultValue = $column->getDefaultValue();
$type = $this->getColumnType($column);
if (!$type) {
if ($column instanceof EnumColumn) {
$values = array();
foreach($column->getValues() as $value) {
$values[] = $this->getValueDefinition($value);
}
if ($column instanceof EnumColumn) { // check this, shouldn't it be in getColumnType?
$values = array();
foreach($column->getValues() as $value) {
$values[] = $this->getValueDefinition($value);
}
$values = implode(",", $values);
$type = "ENUM($values)";
} else {
$values = implode(",", $values);
$type = "ENUM($values)";
} else {
$type = $this->getColumnType($column);
if (!$type) {
return null;
}
}
@@ -393,7 +393,7 @@ class MySQL extends SQL {
if ($param instanceof Column) {
$paramDefs[] = $this->getParameterDefinition($param);
} else {
$this->setLastError("PROCEDURE parameter type " . gettype($returns) . " is not implemented yet");
$this->lastError = $this->logger->error("PROCEDURE parameter type " . gettype($returns) . " is not implemented yet");
return null;
}
}
@@ -402,7 +402,7 @@ class MySQL extends SQL {
if ($returns instanceof Column) {
$paramDefs[] = $this->getParameterDefinition($returns, true);
} else if (!($returns instanceof Trigger)) { // mysql does not need to return triggers here
$this->setLastError("PROCEDURE RETURN type " . gettype($returns) . " is not implemented yet");
$this->lastError = $this->logger->error("PROCEDURE RETURN type " . gettype($returns) . " is not implemented yet");
return null;
}
}

View File

@@ -67,7 +67,7 @@ class PostgreSQL extends SQL {
$this->connection = @pg_connect(implode(" ", $connectionString), PGSQL_CONNECT_FORCE_NEW);
if (!$this->connection) {
$this->lastError = "Failed to connect to Database";
$this->lastError = $this->logger->severe("Failed to connect to Database");
$this->connection = NULL;
return false;
}
@@ -170,7 +170,7 @@ class PostgreSQL extends SQL {
return " ON CONFLICT ($conflictingColumns) DO UPDATE SET $updateValues";
} else {
$strategyClass = get_class($strategy);
$this->lastError = "ON DUPLICATE Strategy $strategyClass is not supported yet.";
$this->lastError = $this->logger->error("ON DUPLICATE Strategy $strategyClass is not supported yet.");
return null;
}
} else {
@@ -233,7 +233,7 @@ class PostgreSQL extends SQL {
} else if($column instanceof JsonColumn) {
return "JSON";
} else {
$this->lastError = "Unsupported Column Type: " . get_class($column);
$this->lastError = $this->logger->error("Unsupported Column Type: " . get_class($column));
return NULL;
}
}
@@ -317,8 +317,7 @@ class PostgreSQL extends SQL {
if ($col instanceof KeyWord) {
return $col->getValue();
} elseif(is_array($col)) {
$columns = array();
foreach($col as $c) $columns[] = $this->columnName($c);
$columns = array_map(function ($c) { return $this->columnName($c); }, $col);
return implode(",", $columns);
} else {
if (($index = strrpos($col, ".")) !== FALSE) {

View File

@@ -2,6 +2,7 @@
namespace Driver\SQL;
use Driver\Logger\Logger;
use Driver\SQL\Column\Column;
use Driver\SQL\Condition\Compare;
use Driver\SQL\Condition\CondAnd;
@@ -40,6 +41,7 @@ use Objects\ConnectionData;
abstract class SQL {
protected Logger $logger;
protected string $lastError;
protected $connection;
protected ConnectionData $connectionData;
@@ -50,6 +52,7 @@ abstract class SQL {
$this->lastError = 'Unknown Error';
$this->connectionData = $connectionData;
$this->lastInsertId = 0;
$this->logger = new Logger(getClassName($this), $this);
}
public function isConnected(): bool {
@@ -168,7 +171,7 @@ abstract class SQL {
return $code;
} else {
$this->lastError = "Unsupported constraint type: " . get_class($constraint);
$this->lastError = $this->logger->error("Unsupported constraint type: " . get_class($constraint));
return null;
}
}
@@ -200,7 +203,7 @@ abstract class SQL {
} else if ($value === null) {
return "NULL";
} else {
$this->lastError = "Cannot create unsafe value of type: " . gettype($value);
$this->lastError = $this->logger->error("Cannot create unsafe value of type: " . gettype($value));
return null;
}
}
@@ -290,7 +293,7 @@ abstract class SQL {
} else if($haystack instanceof Select) {
$values = $haystack->build($params);
} else {
$this->lastError = "Unsupported in-expression value: " . get_class($condition);
$this->lastError = $this->logger->error("Unsupported in-expression value: " . get_class($condition));
return false;
}
@@ -322,7 +325,7 @@ abstract class SQL {
} else if ($condition instanceof Exists) {
return "EXISTS(" .$condition->getSubQuery()->build($params) . ")";
} else {
$this->lastError = "Unsupported condition type: " . gettype($condition);
$this->lastError = $this->logger->error("Unsupported condition type: " . gettype($condition));
return null;
}
}
@@ -345,7 +348,7 @@ abstract class SQL {
$alias = $this->columnName($exp->getAlias());
return "SUM($value) AS $alias";
} else {
$this->lastError = "Unsupported expression type: " . get_class($exp);
$this->lastError = $this->logger->error("Unsupported expression type: " . get_class($exp));
return null;
}
}
@@ -370,6 +373,7 @@ abstract class SQL {
} else if ($type === "postgres") {
$sql = new PostgreSQL($connectionData);
} else {
Logger::instance()->error("Unknown database type: $type");
return "Unknown database type";
}