2020-07-29 21:42:36 +00:00
|
|
|
<?php
|
2020-07-30 23:03:51 +00:00
|
|
|
require_once 'Database.php';
|
2020-07-29 21:42:36 +00:00
|
|
|
|
|
|
|
abstract class RequestBase {
|
|
|
|
private array $actions = array();
|
|
|
|
protected mysqli $conn;
|
|
|
|
|
2020-07-30 23:03:51 +00:00
|
|
|
/**
|
|
|
|
* add the action handlers in this abstract method
|
|
|
|
*/
|
|
|
|
abstract function initHandlers();
|
2020-07-29 21:42:36 +00:00
|
|
|
|
2020-07-30 23:03:51 +00:00
|
|
|
/**
|
|
|
|
* adds a new action handler to the current api file
|
|
|
|
*
|
|
|
|
* @param $action string name of the action variable
|
|
|
|
* @param $callback Closure callback function to be called
|
|
|
|
*/
|
|
|
|
function addActionHandler($action, $callback) {
|
2020-07-29 21:42:36 +00:00
|
|
|
$this->actions[$action] = $callback;
|
|
|
|
}
|
|
|
|
|
2020-07-30 23:03:51 +00:00
|
|
|
/**
|
|
|
|
* runs the correct handler
|
|
|
|
* should be called once within the api request
|
|
|
|
*/
|
2020-07-29 21:42:36 +00:00
|
|
|
function handleAction() {
|
|
|
|
$this->conn = Database::getInstance()->getConnection();
|
|
|
|
|
|
|
|
if (isset($_POST['action'])) {
|
2020-07-30 23:03:51 +00:00
|
|
|
$this->initHandlers();
|
2020-07-29 21:42:36 +00:00
|
|
|
|
|
|
|
$action = $_POST['action'];
|
2020-07-30 23:03:51 +00:00
|
|
|
|
|
|
|
// call the right handler
|
|
|
|
$this->actions[$action]();
|
|
|
|
} else {
|
|
|
|
echo('{data:"error"}');
|
2020-07-29 21:42:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|