Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Rewrote the API
-> Rewrote the public index.php :)
-> Rewrite 80% of the structure!
  • Loading branch information
NaysKutzu committed Aug 20, 2024
1 parent 997c920 commit cc20008
Show file tree
Hide file tree
Showing 160 changed files with 729 additions and 393 deletions.
5 changes: 5 additions & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ Donations:
- [Discord](https://discord.mythicalsystems.xyz) (Joining the server also helps <3)
- [Star](https://github.com/MythicalLTD/Framework) (Consider staring this repo!)

## API Documentation

We do not have any api documentation built in so you have to look at the postman documentation :)
- https://mythicalsystems.postman.co/workspace/mythicalframework/overview

## Repository Activity

![Alt](https://repobeats.axiom.co/api/embed/c403f00d546911a7f80a4e9ace2628c2ab760b04.svg "Repobeats analytics image")
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ caches/last_settings_migrate_run
/storage/caches/*.log
/storage/caches/*/
/storage/logs/*.log
/frontend/node_modules
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
"php.version": "8.3",
"cSpell.words": [
"acaches",
"aindex",
"asuccessful",
"asuccessfully",
"autoloader",
"Bcmath",
"ccaches",
"configpath",
"dbconfigure",
"doesinfoexist",
"ealready",
"ERRMODE",
"fklmnor",
Expand All @@ -15,6 +19,8 @@
"Kutzu",
"mythicalcore",
"mythicalframework",
"Nahh",
"Repobeats",
"rfiles",
"Swal",
"tailwindcss",
Expand Down
38 changes: 0 additions & 38 deletions api/System/getTranslationKey.php

This file was deleted.

14 changes: 0 additions & 14 deletions api/System/logs.php

This file was deleted.

58 changes: 0 additions & 58 deletions api/User/infoalreadyexists.php

This file was deleted.

6 changes: 0 additions & 6 deletions api/User/login.php

This file was deleted.

71 changes: 0 additions & 71 deletions api/User/register.php

This file was deleted.

98 changes: 98 additions & 0 deletions app/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,102 @@ public static function makeSureValueIsNotNull(string $info, ?array $array): void
self::BadRequest("You are missing the field for $info!", $array);
}
}

/**
* Register all api endpoints.
*/
public static function registerApiRoutes(\Router\Router $router): void
{
$admin_folder = __DIR__ . '/Apis/Admin';
$user_folder = __DIR__ . '/Apis/User';
$system_folder = __DIR__ . '/Apis/System';

$admin_files = scandir($admin_folder);
$user_files = scandir($user_folder);
$system_files = scandir($system_folder);

foreach ($system_files as $file) {
if ($file == '.' || $file == '..') {
continue;
}
$class = 'MythicalSystemsFramework\Api\Apis\System\\' . str_replace('.php', '', $file);
$class = new $class();
$router->add('/api' . $class->route, function () use ($class) {
Api::init();
$class->handleRequest();
});
}

foreach ($admin_files as $file) {
if ($file == '.' || $file == '..') {
continue;
}
$class = 'MythicalSystemsFramework\Api\Apis\Admin\\' . str_replace('.php', '', $file);
$class = new $class();
$router->add('/api' . $class->route, function () use ($class) {
Api::init();
$class->handleRequest();
});
}

foreach ($user_files as $file) {
if ($file == '.' || $file == '..') {
continue;
}
$class = 'MythicalSystemsFramework\Api\Apis\User\\' . str_replace('.php', '', $file);
$class = new $class();
$router->add('/api' . $class->route, function () use ($class) {
Api::init();
$class->handleRequest();
});
}

$router->add('/api/(.*)', function () {
self::init();
self::NotFound('The api route does not exist!', null);
});
}

/**
* Extracts the dynamic argument based on the route structure.
*
* @param string $route The route it should include (.*) if you are looking for a dynamic argument
* @param int $aindex This is more like a game :) You need to guess the index of the dynamic argument
*
* @return string The dynamic argument
*
* For people who think they can optimize this function:
*
* I have tried my best to optimize this function as much as possible.
* If you think you can optimize it further, please do so and create a pull request.
* But if not make sure to increase the following line with the hours you wasted over here:
*
* @time 1 hour
*
* For the people who think they know what this function does:
* You don't trust me on this one, do you? Well, I can assure you that this function is the best function you will ever see in your life.
*/
public static function getRouteArg(string $route, int $aindex = 1): string
{
// Break down the route and the current URI into their segments
$routeParts = explode('/', trim($route, '/'));
$uriParts = explode('/', trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'));

// Find the part of the URI that matches the "(.*)" in the route
foreach ($routeParts as $index => $part) {
if ($part === '(.*)') {
// +1 cuz we have /api in front and the code does not know that we have that
// so we need to adjust the index by 1 because of that so yeah do not increase the index by 1 or remove it
// Doing that is gay! (no offense) Just kidding, but seriously do not do that.
// I mean we can technically add /api before the $route up there in the code but that would be a waste of time
// and we do not want to waste time, do we? Nahh we like wasting time on comments like those :)
// So yeah, do not remove the +1 or increase the index by 1.
$adjustedIndex = $index + $aindex;

return $uriParts[$adjustedIndex] ?? '';
}
}

return '';
}
}
11 changes: 11 additions & 0 deletions app/Api/Apis/ApiBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace MythicalSystemsFramework\Api\Apis;

interface ApiBuilder
{
/**
* This function should handle the request and return the response.
*/
public function handleRequest(): void;
}
24 changes: 24 additions & 0 deletions app/Api/Apis/System/Logs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace MythicalSystemsFramework\Api\Apis\System;

use MythicalSystemsFramework\Api\Api;
use MythicalSystemsFramework\Kernel\Logger;
use MythicalSystemsFramework\Kernel\LoggerTypes;
use MythicalSystemsFramework\Api\Apis\ApiBuilder;
use MythicalSystemsFramework\Kernel\LoggerLevels;

class Logs extends Api implements ApiBuilder
{
public string $route = '/system/logs';

public string $description = 'This route will just return the logs';

public function handleRequest(): void
{
self::allowOnlyGET();
Api::OK('Showing you latest logs', [
'logs' => Logger::getAllSortedByDate(LoggerTypes::OTHER, LoggerLevels::OTHER, 10),
]);
}
}
Loading

0 comments on commit cc20008

Please sign in to comment.