2022-06-01 09:47:31 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Objects\Router;
|
|
|
|
|
|
|
|
use Api\Request;
|
|
|
|
use ReflectionClass;
|
|
|
|
use ReflectionException;
|
|
|
|
|
|
|
|
class ApiRoute extends AbstractRoute {
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct("/api/{endpoint:?}/{method:?}", false);
|
|
|
|
}
|
|
|
|
|
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-06-01 12:28:50 +02:00
|
|
|
$document = new \Elements\TemplateDocument($router, "swagger.twig");
|
2022-08-20 22:17:17 +02:00
|
|
|
return $document->load();
|
2022-06-01 12:28:50 +02:00
|
|
|
} else if (!preg_match("/[a-zA-Z]+/", $params["endpoint"])) {
|
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"]);
|
|
|
|
$parentClass = "\\Api\\${apiEndpoint}API";
|
|
|
|
$apiClass = "\\Api\\${apiEndpoint}\\${apiMethod}";
|
|
|
|
} else {
|
|
|
|
$apiClass = "\\Api\\${apiEndpoint}";
|
|
|
|
$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
|
|
|
|
if ($this->checkClass($parentClass)) {
|
|
|
|
if (!$isNestedAPI || class_exists($apiClass)) {
|
|
|
|
$classFound = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-06-20 19:52:31 +02:00
|
|
|
$request = $apiClass->newInstanceArgs(array($router->getContext(), true));
|
|
|
|
$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);
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|