Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8502508a11 | |||
| a59044102c | |||
| b8ef58e132 | |||
| 2f7f63b620 |
@ -6,7 +6,7 @@ A set of tools to simplify the work of creating backend APIs for your frontend p
|
||||
|
||||
For example, we create an API for calculator. So we create class `APIcalculator` and store in file `test/APIcalculator.php` where we defined each actions for API as public method.
|
||||
|
||||
```
|
||||
```php
|
||||
<?php
|
||||
|
||||
/**
|
||||
@ -90,7 +90,7 @@ It is important to extend the `APIcalculator` class with the `\TPsoft\APIlite\AP
|
||||
|
||||
When you run this subfile through the webserver, you will see the JSON documentation in the browser
|
||||
|
||||
```
|
||||
```js
|
||||
{
|
||||
"name": "APIcalculator",
|
||||
"html_version": "http://localhost/APIlite/test/APIcalculator.php?format=html",
|
||||
@ -194,7 +194,7 @@ and there is also an HTML version available
|
||||
|
||||
To connect to the API from TypeScript (e.g. Vue application) it is possible to download the backend script
|
||||
|
||||
```
|
||||
```ts
|
||||
/**
|
||||
* Generated by APIlite
|
||||
* https://gitea.tpsoft.org/TPsoft.org/APIlite
|
||||
|
||||
@ -26,8 +26,7 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=8.2",
|
||||
"ext-pdo": "*"
|
||||
"php": ">=8.2"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
||||
@ -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;
|
||||
if (isset($_REQUEST['format'])) {
|
||||
$format = $_REQUEST['format'];
|
||||
}
|
||||
if (isset($argv)) {
|
||||
$switches = array_map('strtolower', is_array($argv) ? $argv : []);
|
||||
if ((isset($_REQUEST['format']) && $_REQUEST['format'] == 'html') || in_array('--html', $switches)) {
|
||||
if (in_array('--html', $switches)) {
|
||||
$format = 'html';
|
||||
}
|
||||
if (in_array('--typescript', $switches)) {
|
||||
$format = 'typescript';
|
||||
}
|
||||
}
|
||||
switch ($format) {
|
||||
case 'html':
|
||||
$this->printHelpHTML();
|
||||
} elseif ((isset($_REQUEST['format']) && $_REQUEST['format'] == 'typescript') || in_array('--typescript', $switches)) {
|
||||
break;
|
||||
case 'typescript':
|
||||
$this->printHelpTypescript();
|
||||
} else {
|
||||
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();
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
@ -306,6 +306,16 @@
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<h3 style="margin-bottom: 1rem; color: #2c3e50; display: inline-block;">Return:</h3>
|
||||
<?php if (is_string($method['return'])) { ?>
|
||||
<code class="parameter-type"><?php echo $method['return']; ?></code>
|
||||
<?php } ?>
|
||||
<?php if (is_array($method['return'])) foreach ($method['return'] as $return) { ?>
|
||||
<code class="parameter-type"><?php echo $return; ?></code>
|
||||
<?php } ?>
|
||||
|
||||
|
||||
</div>
|
||||
<?php } ?>
|
||||
</section>
|
||||
|
||||
@ -5,8 +5,8 @@
|
||||
* <?php echo date('Y-m-d H:i:s'); ?>
|
||||
*/
|
||||
|
||||
export const backend = {
|
||||
endpont: window.location.origin + "<?php echo $this->apiName; ?>.php",
|
||||
class <?php echo $this->apiName; ?> {
|
||||
endpont = "<?php echo $this->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__', {});
|
||||
},
|
||||
}
|
||||
|
||||
<?php if (is_array($this->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();
|
||||
|
||||
Reference in New Issue
Block a user