web-base/Core/Objects/Router/ApiRoute.class.php

85 lines
2.8 KiB
PHP
Raw Normal View History

2022-06-01 09:47:31 +02:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\Objects\Router;
2022-06-01 09:47:31 +02:00
2022-11-18 18:06:46 +01:00
use Core\API\Request;
use Core\Elements\TemplateDocument;
2022-11-27 12:33:27 +01:00
use Core\Objects\DatabaseEntity\Route;
2022-06-01 09:47:31 +02:00
use ReflectionClass;
use ReflectionException;
2022-11-27 12:33:27 +01:00
class ApiRoute extends Route {
2022-06-01 09:47:31 +02:00
public function __construct() {
2022-11-27 12:33:27 +01:00
parent::__construct("API", "/api/{endpoint:?}/{method:?}", false);
2022-06-01 09:47:31 +02:00
}
2022-08-20 22:04:09 +02:00
private static function checkClass(string $className): bool {
$classPath = getClassPath($className);
return file_exists($classPath) && class_exists($className);
}
2022-06-01 09:47:31 +02:00
public function call(Router $router, array $params): string {
if (empty($params["endpoint"])) {
header("Content-Type: text/html");
2022-11-18 18:06:46 +01:00
$document = new TemplateDocument($router, "swagger.twig");
2022-08-20 22:17:17 +02:00
return $document->load();
} else if (!preg_match("/[a-zA-Z]+/", $params["endpoint"]) ||
!preg_match("/[a-zA-Z]*/", $params["method"])) {
2022-06-01 09:47:31 +02:00
http_response_code(400);
$response = createError("Invalid Method");
} else {
$apiEndpoint = ucfirst($params["endpoint"]);
2022-08-20 22:04:09 +02:00
$isNestedAPI = !empty($params["method"]);
if ($isNestedAPI) {
2022-06-01 09:47:31 +02:00
$apiMethod = ucfirst($params["method"]);
2022-11-18 18:06:46 +01:00
$parentClass = "\\API\\${apiEndpoint}API";
$apiClass = "\\API\\${apiEndpoint}\\${apiMethod}";
2022-06-01 09:47:31 +02:00
} else {
2022-11-18 18:06:46 +01:00
$apiClass = "\\API\\${apiEndpoint}";
2022-06-01 09:47:31 +02:00
$parentClass = $apiClass;
}
try {
2022-08-20 22:04:09 +02:00
$classFound = False;
// first: check if the parent class exists, for example:
// /stats => Stats.class.php
// /mail/send => MailAPI.class.php
2022-11-18 18:06:46 +01:00
foreach (["Site", "Core"] as $module) {
if ($this->checkClass("\\$module$parentClass")) {
if (!$isNestedAPI || class_exists("\\$module$apiClass")) {
$classFound = true;
$apiClass = "\\$module$apiClass";
break;
}
2022-08-20 22:04:09 +02:00
}
}
if ($classFound) {
2022-06-01 09:47:31 +02:00
$apiClass = new ReflectionClass($apiClass);
2022-08-20 22:04:09 +02:00
if (!$apiClass->isSubclassOf(Request::class) || !$apiClass->isInstantiable()) {
2022-06-01 09:47:31 +02:00
http_response_code(400);
$response = createError("Invalid Method");
} else {
$request = $apiClass->newInstanceArgs([$router->getContext(), true]);
2022-06-20 19:52:31 +02:00
$success = $request->execute();
$response = $request->getResult();
$response["success"] = $success;
$response["msg"] = $request->getLastError();
2022-06-01 09:47:31 +02:00
}
2022-08-20 22:04:09 +02:00
} else {
http_response_code(404);
$response = createError("Not found");
2022-06-01 09:47:31 +02:00
}
} catch (ReflectionException $e) {
http_response_code(500);
$router->getLogger()->error("Error instantiating class: $e");
2022-06-01 09:47:31 +02:00
$response = createError("Error instantiating class: $e");
}
}
header("Content-Type: application/json");
2022-06-20 19:52:31 +02:00
return json_encode($response);
2022-06-01 09:47:31 +02:00
}
}