Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Finish migrating the old command system :)
  • Loading branch information
NaysKutzu committed Jul 25, 2024
1 parent 9bc0575 commit e665005
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 122 deletions.
89 changes: 89 additions & 0 deletions app/Cli/Commands/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace MythicalSystemsFramework\Cli\Commands;

use MythicalSystemsFramework\Database\MySQLCache;
use MythicalSystemsFramework\Handlers\CacheHandler as CacheWorker;

class Cache extends Command
{
public static string $description = 'A command that can help if you want to cache the database or the config!';

public static function execute(bool $isFrameworkCommand = false): void
{
echo self::log_info('');
echo self::log_info('&c1.&7 Flush the caches!');
echo self::log_info('&c2.&7 Process the caches!');
echo self::log_info('&c3.&7 Create a table cache!');
echo self::log_info('&c4.&7 Exit');
echo self::log_info('');

$option = readline('Select an option: ');

switch ($option) {
case '1':
self::flush();
break;
case '2':
self::process();
break;
case '3':
self::create();
break;
case '4':
self::exit();
break;
default:
echo 'Invalid option selected.';
break;
}
}

public static function flush(): void
{
echo self::translateColorsCode('Please wait while we purge your &ccaches&r.&o');
try {
CacheWorker::purge();
echo self::translateColorsCode('&rPurged caches!');
} catch (\Exception $e) {
echo self::translateColorsCode('&cFailed to purge caches: &r' . $e->getMessage());
}
}

public static function process(): void
{
echo self::translateColorsCode('Please wait while we process your &ccaches&r.&o');
try {
CacheWorker::process();
echo self::translateColorsCode('&rProcessed caches!');
} catch (\Exception $e) {
echo self::translateColorsCode('&cFailed to process caches: &r' . $e->getMessage());
}
}

public static function create(): void
{
echo self::translateColorsCode('Please wait while we create your &ccaches&r.&o');
$table = readline('Enter the name of the table you want to cache: ');
$status = MySQLCache::saveCache($table);
switch ($status) {
case 'ERROR_TABLE_DOES_NOT_EXIST':
self::exit('&cTable not found!&o');
break;
case 'ERROR_NO_DATA_FOUND_IN_TABLE':
self::exit('&cNo data found in table!&o');
break;
case 'ERROR_TABLE_NOT_SUPPORTED':
self::exit('&cTable does not support cache!&o');
break;
case 'ERROR_MYSQL_ERROR':
self::exit('&cThere was an error while trying to connect to the mysql server :(&o');
break;
case 'OK':
self::exit('&rCreated &acaches&r!&o');
break;
default:
break;
}
}
}
2 changes: 1 addition & 1 deletion app/Cli/Commands/Colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Colors extends Command

public static function execute(bool $isFrameworkCommand = false): void
{
$colors = 'Colors: &0Black&r, &1Dark Blue&r, &2Dark Green&r, &3Dark Aqua&r, &4Dark Red&r, &5Dark Purple&r, &6Gold&r, &7Gray&r, &8Dark Gray&r, &9Blue&r, &aGreen&r, &bAqua&r, &cRed&r, &dLight Purple&r, &eYellow&r, &fWhite&r, &rReset&r, &lBold&r, &nUnderline&r, &mStrikethrough&r';
$colors = 'Colors: &0Black&r, &1Dark Blue&r, &2Dark Green&r, &3Dark Aqua&r, &4Dark Red&r, &5Dark Purple&r, &6Gold&r, &7Gray&r, &8Dark Gray&r, &9Blue&r, &aGreen&r, &bAqua&r, &cRed&r, &dLight Purple&r, &eYellow&r, &rWhite&r, &rReset&r, &lBold&r, &nUnderline&r, &mStrikethrough&r';
echo self::translateColorsCode($colors);
echo PHP_EOL;
echo str_replace('&r', '', $colors);
Expand Down
35 changes: 20 additions & 15 deletions app/Cli/Commands/Configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function execute(bool $isFrameworkCommand = false): void
public static function dbconfigure(): void
{
if (cfg::get('database', 'password') !== null) {
echo self::translateColorsCode('&fDatabase configuration already exists. &oWould you like to overwrite it? &8[&aY&8/&cN&8]&f: ');
echo self::translateColorsCode('&rDatabase configuration already exists. &oWould you like to overwrite it? &8[&aY&8/&cN&8]&r: ');
$overwrite = readline();
if (strtolower($overwrite) !== 'y') {
return;
Expand All @@ -47,15 +47,15 @@ public static function dbconfigure(): void
$defaultHost = '127.0.0.1';
$defaultPort = '3306';
$db = new MySQL();
echo self::translateColorsCode("&fEnter the host of the database &8[&e$defaultHost&8]&f: ");
echo self::translateColorsCode("&rEnter the host of the database &8[&e$defaultHost&8]&r: ");
$host = readline() ?: $defaultHost;
echo self::translateColorsCode("&fEnter the port of the database &8[&e$defaultPort&8]&f: ");
echo self::translateColorsCode("&rEnter the port of the database &8[&e$defaultPort&8]&r: ");
$port = readline() ?: $defaultPort;
echo self::translateColorsCode('&fEnter the username: ');
echo self::translateColorsCode('&rEnter the username: ');
$username = readline();
echo self::translateColorsCode('&fEnter the password: ');
echo self::translateColorsCode('&rEnter the password: ');
$password = readline();
echo self::translateColorsCode('&fEnter the database name: ');
echo self::translateColorsCode('&rEnter the database name: ');
$database = readline();
// Perform validation
if (empty($username) || empty($password) || empty($database)) {
Expand All @@ -69,29 +69,34 @@ public static function dbconfigure(): void

// Use the provided information
echo self::NewLine();
echo self::translateColorsCode("&fHost: &e$host&o");
echo self::translateColorsCode("&fPort: &e$port&o");
echo self::translateColorsCode("&fUsername: &e$username&o");
echo self::translateColorsCode("&fPassword: &e$hiddenPassword&o");
echo self::translateColorsCode("&fDatabase: &e$database&o");
echo self::translateColorsCode("&rHost: &e$host&o");
echo self::translateColorsCode("&rPort: &e$port&o");
echo self::translateColorsCode("&rUsername: &e$username&o");
echo self::translateColorsCode("&rPassword: &e$hiddenPassword&o");
echo self::translateColorsCode("&rDatabase: &e$database&o");

if ($db->tryConnection($host, $port, $username, $password, $database) == true) {
echo self::NewLine();
echo self::translateColorsCode('&fConnection to the database was &asuccessful!&o');
echo self::translateColorsCode('&rConnection to the database was &asuccessful!&o');
echo self::NewLine();
echo self::translateColorsCode('&fSaving the configuration...&o');
echo self::translateColorsCode('&rSaving the configuration...&o');
cfg::set('database', 'host', $host);
cfg::set('database', 'port', $port);
cfg::set('database', 'username', $username);
cfg::set('database', 'password', $password);
cfg::set('database', 'name', $database);
echo self::translateColorsCode('&fConfiguration saved &asuccessfully!&o');
echo self::translateColorsCode('&rGenerating an encryption key for database...!&o');
$key = 'mythicalcore_' . bin2hex(random_bytes(64 * 128));
cfg::set('encryption', 'key', $key);
echo self::translateColorsCode('&rKey generated &asuccessfully&r!&o');
echo self::translateColorsCode('&rConfiguration saved &asuccessfully&r!&o');
} else {
echo self::translateColorsCode('&7Failed to connect to the database. &o&fPlease check the provided information.');
echo self::translateColorsCode('&7Failed to connect to the database. &o&rPlease check the provided information.');
}
}

public static function configure(): void
{
self::exit('This feature is not implemented yet.');
}
}
15 changes: 12 additions & 3 deletions app/Cli/Commands/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function execute(bool $isFrameworkCommand = false): void
self::db();
break;
case '2':
echo self::translateColorsCode('&cThis option is not available yet!&o');
self::cfg();
break;
case '3':
self::exit();
Expand All @@ -38,11 +38,11 @@ public static function execute(bool $isFrameworkCommand = false): void
public static function db(): void
{
try {
self::log_info('Migrating the database...');
self::log_info('Migrating the database...&o');
self::log_info('');
MySQL::migrate(true);
Settings::migrate(true);
self::log_success('&rDatabase migrated &asuccessfully&r!');
self::log_success('&rDatabase migrated &asuccessfully&r!&o');
self::exit();
} catch (\Exception $e) {
exit('Failed to migrate the database: ' . $e->getMessage() . '');
Expand All @@ -51,5 +51,14 @@ public static function db(): void

public static function cfg(): void
{
try {
self::log_info('Migrating the config...&o');
self::log_info('');
Settings::migrate(true);
self::log_success('&rConfig migrated &asuccessfully&r!&o');
self::exit();
} catch (\Exception $e) {
exit('Failed to migrate the config: ' . $e->getMessage() . '');
}
}
}
24 changes: 12 additions & 12 deletions app/Cli/Commands/Rebuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public static function db(): void
try {
$db = new MySQL();
if ($db->tryConnection(cfg::get('database', 'host'), cfg::get('database', 'port'), cfg::get('database', 'username'), cfg::get('database', 'password'), cfg::get('database', 'name')) == true) {
echo self::translateColorsCode('&o&fConnection to the database was &asuccessful!&o');
echo self::translateColorsCode('&o&rConnection to the database was &asuccessful!&o');
echo self::NewLine();
echo self::translateColorsCode('&4&lWARNING: &fThis option will wipe the database. &o');
echo self::translateColorsCode('&4&lWARNING: &fOnly use this function if you know what you are doing &o');
echo self::translateColorsCode('&4&lWARNING: &fOnce you wipe the database there is no going back! &o');
echo self::translateColorsCode("&4&lWARNING: &fPlease be careful and don't play around with commands! &o");
echo self::translateColorsCode('&4&lWARNING: &fThere is no other message then this so keep in mind! &o');
echo self::translateColorsCode('&4&lWARNING: &fDo you really want to wipe the database? (&ey&f/&en&f): ');
echo self::translateColorsCode('&4&lWARNING: &rThis option will wipe the database. &o');
echo self::translateColorsCode('&4&lWARNING: &rOnly use this function if you know what you are doing &o');
echo self::translateColorsCode('&4&lWARNING: &rOnce you wipe the database there is no going back! &o');
echo self::translateColorsCode("&4&lWARNING: &rPlease be careful and don't play around with commands! &o");
echo self::translateColorsCode('&4&lWARNING: &rThere is no other message then this so keep in mind! &o');
echo self::translateColorsCode('&4&lWARNING: &rDo you really want to wipe the database? (&ey&r/&en&r): ');

$confirm = readline();
if (strtolower($confirm) == 'y') {
Expand All @@ -67,19 +67,19 @@ public static function db(): void
echo self::NewLine();
echo self::NewLine();
echo self::NewLine();
echo self::translateColorsCode('&fDatabase wiped!!&o');
echo self::translateColorsCode('&rDatabase wiped!!&o');
MySQL::migrate(true);
Settings::migrate(true);
echo self::NewLine();
echo self::translateColorsCode('&fDatabase rebuilt!&o');
echo self::translateColorsCode("&fLet's start by setting up your configuration!&o");
echo self::translateColorsCode('&rDatabase rebuilt!&o');
echo self::translateColorsCode("&rLet's start by setting up your configuration!&o");
Configure::configure();
} catch (\PDOException $e) {
echo self::translateColorsCode('&fFailed to drop tables: &c' . $e->getMessage() . '&o');
echo self::translateColorsCode('&rFailed to drop tables: &c' . $e->getMessage() . '&o');
echo self::NewLine();
}
} else {
self::exit('&fExiting...&o');
self::exit('&rExiting...&o');
}
} else {
self::exit('&cFailed to connect to the database!&o');
Expand Down
4 changes: 2 additions & 2 deletions app/Database/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,12 @@ public static function migrate(bool $isCli = false): void
$stmt = $db->prepare('INSERT INTO framework_migrations (script) VALUES (?)');
$stmt->execute([$fileName]);
if ($isCli == true) {
echo Kernel::translateColorsCode('&fExecuted migration: &e' . $fileName . '&o');
echo Kernel::translateColorsCode('&rExecuted migration: &e' . $fileName . '&o');
echo Kernel::NewLine();
}
} else {
if ($isCli == true) {
echo Kernel::translateColorsCode('&fSkipping migration: &e' . $fileName . ' &f(&ealready executed&f)&o');
echo Kernel::translateColorsCode('&rSkipping migration: &e' . $fileName . ' &r(&ealready executed&r)&o');
echo Kernel::NewLine();
}
}
Expand Down
18 changes: 13 additions & 5 deletions app/Database/MySQLCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@

class MySQLCache extends MySQL
{
public static function saveCache(string $table_name): void
/**
* Summary of saveCache.
*
* @param string $table_name The name of the table
*/
public static function saveCache(string $table_name): string
{
try {
if (self::doesTableExist($table_name) == false) {
throw new \Exception('Table does not exist.');
return 'ERROR_TABLE_DOES_NOT_EXIST';
}

$mysqli = new MySQL();
Expand All @@ -19,7 +24,7 @@ public static function saveCache(string $table_name): void
$result = $conn->query($query);

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

/*
Expand Down Expand Up @@ -61,8 +66,9 @@ public static function saveCache(string $table_name): void
}
}
} elseif ($table_name == 'framework_users') {
// TODO: ADD SUPPORT FOR DUMP USERS IN CACHE!
} else {
throw new \Exception('Table not supported.');
return 'ERROR_TABLE_NOT_SUPPORTED';
}

$cache_info['cache_info'] = [
Expand All @@ -74,8 +80,10 @@ public static function saveCache(string $table_name): void
$json = json_encode($data, JSON_PRETTY_PRINT);
Settings::up();
file_put_contents(Settings::$cache_path . '/' . $table_name . '.json', $json);

return 'OK';
} catch (\Exception $e) {
throw new \Exception('Failed to save cache: ' . $e);
return 'ERROR_MYSQL_ERROR';
}
}

Expand Down
6 changes: 3 additions & 3 deletions app/Managers/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ public static function migrate(bool $isTerminal = false): void
}
}
if ($isTerminal) {
echo color::translateColorsCode('&fMigration completed. Migrated &e' . $migratedCount . ' &ffiles.');
echo color::translateColorsCode('&rMigration completed. &oMigrated &e' . $migratedCount . ' &rfiles.&o');
}
} else {
if ($isTerminal) {
echo color::translateColorsCode('&fNo migrations found!');
echo color::translateColorsCode('&rNo migrations found!');
}
}
} catch (\Exception $e) {
if ($isTerminal) {
echo color::translateColorsCode('&cFailed to migrate the database: &f' . $e->getMessage() . '');
echo color::translateColorsCode('&cFailed to migrate the database: &r' . $e->getMessage() . '');
} else {
throw new NoMigrationsFound('No migrations found!' . $e->getMessage());
}
Expand Down
18 changes: 0 additions & 18 deletions commands/core/cache/cacheprocess.php

This file was deleted.

18 changes: 0 additions & 18 deletions commands/core/cache/cachepurge.php

This file was deleted.

Loading

0 comments on commit e665005

Please sign in to comment.