Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Test
  • Loading branch information
NaysKutzu committed Jul 24, 2024
1 parent f7e247e commit 166db90
Show file tree
Hide file tree
Showing 107 changed files with 597 additions and 83 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,10 @@ jobs:
${{ steps.composer-cache.outputs.vcs_cache }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
${{ runner.os }}-composer-
- name: Install dependencies
run: composer validate --no-check-publish && composer install --prefer-dist --no-progress

- name: Run PHPUnit
run: composer run tests
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ composer.phar
/package-lock.json
/node_modules
/test.json
/caches/*
/caches/*/
caches/last_settings_migrate_run
Empty file modified addons/Example/Example.php
100644 → 100755
Empty file.
Empty file modified addons/Example/MythicalFramework.json
100644 → 100755
Empty file.
Empty file modified addons/data.json
100644 → 100755
Empty file.
Empty file modified api/System/logs.php
100644 → 100755
Empty file.
Empty file modified api/User/login.php
100644 → 100755
Empty file.
123 changes: 123 additions & 0 deletions app/Database/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PDO;
use PDOException;
use mysqli;
use Exception;

class MySQL
{
Expand Down Expand Up @@ -95,4 +96,126 @@ public function tryConnection(string $host, string|int $port, string $username,
return false;
}
}

/**
* Try to lock a record!
*
* @param string $table The table name!
* @param string $id The record id!
*
* @return void
*/
public function requestLock(string $table, string $id): void
{
try {
if (self::doesTableExist($table) === false) {
return;
}
$mysqli = new MySQL();
$conn = $mysqli->connectMYSQLI();
$stmt = $conn->prepare("UPDATE ? SET `locked` = 'true' WHERE `id` = ?;");
$stmt->bind_param("si", $table, $id);
$stmt->execute();
$stmt->close();
} catch (Exception $e) {
Logger::log(LoggerLevels::CRITICAL, LoggerTypes::DATABASE, "Failed to lock table: " . $e);
return;
}
}
/**
* Try to unlock a record.
* Unlock a record so you can write and read it!
*
* @param string $table The table name!
* @param string $id The id of the record!
*
* @return void
*/
public static function requestUnLock(string $table, string $id): void
{
try {
if (self::doesTableExist($table) === false) {
return;
}
$mysqli = new MySQL();
$conn = $mysqli->connectMYSQLI();
$stmt = $conn->prepare("UPDATE ? SET `locked` = 'false' WHERE `id` = ?;");
$stmt->bind_param("si", $table, $id);
$stmt->execute();
$stmt->close();
} catch (Exception $e) {
Logger::log(LoggerLevels::CRITICAL, LoggerTypes::DATABASE, "Failed to unlock table: " . $e);
return;
}
}
/**
* Get the lock status of a record.
*
* @param string $table The table name
* @param string $id The id of the record
*
* @return bool
*/
public static function getLock(string $table, string $id): bool
{
try {
if (self::doesTableExist($table) === false) {
return false;
}
$mysqli = new MySQL();
$conn = $mysqli->connectMYSQLI();
$stmt = $conn->prepare("SELECT `locked` FROM ? WHERE `id` = ?;");
$stmt->bind_param("si", $table, $id);
$stmt->execute();
$stmt->close();
return $stmt->get_result()->fetch_assoc()["locked"];
} catch (Exception $e) {
Logger::log(LoggerLevels::CRITICAL, LoggerTypes::DATABASE, "Failed to get lock status: " . $e);
return false;
}
}
/**
* Does a table exist in the database?
*
* @param string $table The table name
*
* @return bool
*/
public static function doesTableExist(string $table): bool
{
try {
$mysqli = new MySQL();
$conn = $mysqli->connectMYSQLI();
$conn->query("SELECT * FROM ".mysqli_real_escape_string($conn, $table));
return true;
} catch (Exception $e) {
Logger::log(LoggerLevels::CRITICAL, LoggerTypes::DATABASE, "Failed to check if table exists: " . $e);
return false;
}
}
/**
* Does a record exist in the database?
*
* @param string $table Table name
* @param string $search The term you want to search for (id)
* @param string $term What the value should be (1)
* @return bool
*/
public static function doesRecordExist(string $table, string $search, string $term) : bool {
try {
if (self::doesTableExist($table) === false) {
return false;
}
$mysqli = new MySQL();
$conn = $mysqli->connectMYSQLI();
$stmt = $conn->prepare("SELECT * FROM ? WHERE ? = ?;");
$stmt->bind_param("sss", $table, $search, $term);
$stmt->execute();
$stmt->close();
return true;
} catch (Exception $e) {
Logger::log(LoggerLevels::CRITICAL, LoggerTypes::DATABASE, "Failed to check if record exists: " . $e);
return false;
}
}
}
95 changes: 95 additions & 0 deletions app/Database/MySQLCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace MythicalSystemsFramework\Database;

use Exception;
use MythicalSystems\Api\Api;
use MythicalSystemsFramework\Managers\Settings;

class MySQLCache extends MySQL
{
public static function saveCache(string $table_name): void
{
try {
if (self::doesTableExist($table_name) == false) {
throw new Exception("Table does not exist.");
}

$mysqli = new MySQL();
$conn = $mysqli->connectMYSQLI();
$query = "SELECT * FROM " . mysqli_real_escape_string($conn, $table_name);
$result = $conn->query($query);

if ($result->num_rows == 0) {
throw new Exception("No data found.");
}

/**
* Specific table dump settings!
*
* @requires framework_settings
*/
if ($table_name == "framework_settings") {
/**
* Code to export the settings file in a format that MythicalSystemsFramework\Managers\Settings can read!
*/
$query = "SELECT scategory FROM " . mysqli_real_escape_string($conn, $table_name);
$result = $conn->query($query);

if ($result->num_rows > 0) {
$categories = [];
while ($row = $result->fetch_assoc()) {
$category = $row['scategory'];
if (!in_array($category, $categories)) {
$categories[] = $category;
}
}
}
$data = [];
foreach ($categories as $category) {
if ($category !== 0) {
$data[$category] = [];

$query = "SELECT skey, svalue FROM " . mysqli_real_escape_string($conn, $table_name) . " WHERE scategory = '" . mysqli_real_escape_string($conn, $category) . "'";
$result = $conn->query($query);

if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$name = $row['skey'];
$value = $row['svalue'];
$data[$category][$name] = $value;
}
}
}
}

} elseif ($table_name == "framework_users") {

} else {
throw new Exception("Table not supported.");
}

$cache_info["cache_info"] = [
"table" => $table_name,
"date_created" => date("Y-m-d H:i:s"),
"date_expire" => date("Y-m-d H:i:s", strtotime("+".$data["caches"]["settings_cache_life"]." seconds")),
];
$data = array_merge($cache_info,$data);
$json = json_encode($data, JSON_PRETTY_PRINT);
Settings::up();
file_put_contents(Settings::$cache_path . '/' . $table_name . '.json', $json);
} catch (Exception $e) {
throw new Exception("Failed to save cache: " . $e);
}
}

public static function deleteCaches(): void
{
$files = glob(Settings::$cache_path . '/*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
}
}
}
13 changes: 13 additions & 0 deletions app/Database/Redis/Redis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace MythicalSystemsFramework\Database\Redis;


class Redis
{
public static function getConnection()
{


}
}
Loading

0 comments on commit 166db90

Please sign in to comment.