diff --git a/src/APIlite.php b/src/APIlite.php index 92ef7fe..ac37138 100644 --- a/src/APIlite.php +++ b/src/APIlite.php @@ -18,23 +18,40 @@ class APIlite { private string $apiName = ''; + private string $endpoint = ''; private $methods = array(); - public function __construct() + public function __construct(?string $format = null, ?string $endpoint = null) { register_shutdown_function(array($this, '_shutdownHandler')); + $this->endpoint = $endpoint ?? $this->getCurrentUrl(); $this->analyzeClass(); if (isset($_REQUEST['action'])) { $this->doAction($_REQUEST['action']); } else { global $argv; - $switches = array_map('strtolower', is_array($argv) ? $argv : []); - if ((isset($_REQUEST['format']) && $_REQUEST['format'] == 'html') || in_array('--html', $switches)) { - $this->printHelpHTML(); - } elseif ((isset($_REQUEST['format']) && $_REQUEST['format'] == 'typescript') || in_array('--typescript', $switches)) { - $this->printHelpTypescript(); - } else { - $this->printHelpJSON(); + if (isset($_REQUEST['format'])) { + $format = $_REQUEST['format']; + } + if (isset($argv)) { + $switches = array_map('strtolower', is_array($argv) ? $argv : []); + if (in_array('--html', $switches)) { + $format = 'html'; + } + if (in_array('--typescript', $switches)) { + $format = 'typescript'; + } + } + switch ($format) { + case 'html': + $this->printHelpHTML(); + break; + case 'typescript': + $this->printHelpTypescript(); + break; + default: + $this->printHelpJSON(); + break; } } } @@ -50,7 +67,7 @@ class APIlite private function analyzeClass(): bool { $refClass = new \ReflectionClass($this); - $this->apiName = $refClass->getName(); + $this->apiName = $refClass->getShortName(); $this->methods = array(); foreach ($refClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $ref_method) { $method_name = $ref_method->getName(); @@ -81,11 +98,28 @@ class APIlite if ($ref_param->isOptional()) { $param['default'] = $ref_param->getDefaultValue(); } - $param['doc'] = $this->parseParamDoc($method['doc'], $param['name']); + if (!is_null($method['doc'])) { + $param['doc'] = $this->parseParamDoc($method['doc'], $param['name']); + } $method['params'][] = $param; } if ($ref_method->hasReturnType()) { - $method['return'] = $ref_method->getReturnType()->getName(); + $ref_type = $ref_method->getReturnType(); + if ($ref_type instanceof \ReflectionNamedType) { + $method['return'] = $ref_type->getName(); + } + if ($ref_type instanceof \ReflectionUnionType + || $ref_type instanceof \ReflectionIntersectionType ) + { + $types = $ref_type->getTypes(); + $method['return'] = []; + foreach ($types as $type) { + if ($type instanceof \ReflectionNamedType) { + $method['return'][] = $type->getName(); + } + } + + } } $this->methods[] = $method; } diff --git a/src/help.tpl.php b/src/help.tpl.php index 0602b46..7860fb0 100644 --- a/src/help.tpl.php +++ b/src/help.tpl.php @@ -306,6 +306,16 @@ + +
+
+
+
+
+
+
diff --git a/src/typescript.tpl.php b/src/typescript.tpl.php
index 5da2739..3f70439 100644
--- a/src/typescript.tpl.php
+++ b/src/typescript.tpl.php
@@ -5,8 +5,8 @@
*
*/
-export const backend = {
- endpont: window.location.origin + "apiName; ?>.php",
+class apiName; ?> {
+ endpont = "endpoint; ?>";
/* ----------------------------------------------------
* General API call
@@ -31,7 +31,7 @@ export const backend = {
});
xhttp.open('POST', this.endpont + '?action=' + method);
xhttp.send(form_data);
- },
+ }
callPromise(method, data) {
return new Promise((resolve, reject) => {
@@ -43,20 +43,22 @@ export const backend = {
}
});
})
- },
+ }
/* ----------------------------------------------------
* API actions
*/
help() {
return this.callPromise('__HELP__', {});
- },
+ }
methods)) foreach ($this->methods as $method) {
echo "\t".$method['name'].'('.implode(', ', array_map(function($param) { return $param['name']; }, $method['params'])).') {';
echo "\n\t\treturn this.callPromise('".$method['name']."', {".implode(', ', array_map(function($param) { return $param['name'].': '.$param['name']; }, $method['params']))."});";
- echo "\n\t},\n\n";
+ echo "\n\t}\n\n";
}
?>
};
+
+export default new BackendAPI();