OpenMediaCenter/api/RequestBase.php

42 lines
1005 B
PHP
Raw Normal View History

<?php
2020-07-30 23:03:51 +00:00
require_once 'Database.php';
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-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) {
$this->actions[$action] = $callback;
}
2020-07-30 23:03:51 +00:00
/**
* runs the correct handler
* should be called once within the api request
*/
function handleAction() {
$this->conn = Database::getInstance()->getConnection();
if (isset($_POST['action'])) {
2020-07-30 23:03:51 +00:00
$this->initHandlers();
$action = $_POST['action'];
2020-07-30 23:03:51 +00:00
// call the right handler
$this->actions[$action]();
} else {
echo('{data:"error"}');
}
}
}