Implemented the API interface. Can now be used by direct dependency insertion

This commit is contained in:
Abel Hoogeveen 2015-07-07 14:09:38 +02:00
parent 00eff0480e
commit a688821c65
4 changed files with 160 additions and 1 deletions

View File

@ -0,0 +1,15 @@
<?php
namespace Module\Api;
use \FuzeWorks\Module;
class Main extends Module {
public function __construct(&$core){
parent::__construct($core);
}
public function onLoad() {
require_once($this->getModulePath() . "/class.rest.php");
}
}

121
Modules/api/class.rest.php Normal file
View File

@ -0,0 +1,121 @@
<?php
namespace Module\Api;
use \FuzeWorks\Module;
class RestApi extends Module {
public function __construct(&$core){
parent::__construct($core);
}
public function onLoad() {}
/**
* Property: method
* The HTTP method this request was made in, either GET, POST, PUT or DELETE
*/
public $method = '';
/**
* Property: endpoint
* The Model requested in the URI. eg: /files
*/
public $endpoint = '';
/**
* Property: verb
* An optional additional descriptor about the endpoint, used for things that can
* not be handled by the basic methods. eg: /files/process
*/
public $verb = '';
/**
* Property: args
* Any additional URI components after the endpoint and verb have been removed, in our
* case, an integer ID for the resource. eg: /<endpoint>/<verb>/<arg0>/<arg1>
* or /<endpoint>/<arg0>
*/
public $args = Array();
/**
* Property: file
* Stores the input of the PUT request
*/
public $file = Null;
/**
* Request
* Allow for CORS, assemble and pre-process the data
*/
public function request($request) {
header("Access-Control-Allow-Orgin: *");
header("Access-Control-Allow-Methods: *");
header("Content-Type: application/json");
$this->args = explode('/', rtrim($request, '/'));
$this->endpoint = array_shift($this->args);
if (array_key_exists(0, $this->args) && !is_numeric($this->args[0])) {
$this->verb = array_shift($this->args);
}
$this->method = $_SERVER['REQUEST_METHOD'];
if ($this->method == 'POST' && array_key_exists('HTTP_X_HTTP_METHOD', $_SERVER)) {
if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'DELETE') {
$this->method = 'DELETE';
} else if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'PUT') {
$this->method = 'PUT';
} else {
throw new Exception("Unexpected Header");
}
}
switch($this->method) {
case 'DELETE':
case 'POST':
$this->request = $this->_cleanInputs($_POST);
break;
case 'GET':
$this->request = $this->_cleanInputs($_GET);
break;
case 'PUT':
$this->request = $this->_cleanInputs($_GET);
$this->file = file_get_contents("php://input");
break;
default:
$this->_response('Invalid Method', 405);
break;
}
}
public function processAPI() {
if ((int)method_exists($this, $this->endpoint) > 0) {
return $this->_response($this->{$this->endpoint}($this->args));
}
return $this->_response("No Endpoint: $this->endpoint", 404);
}
private function _response($data, $status = 200) {
header("HTTP/1.1 " . $status . " " . $this->_requestStatus($status));
return json_encode($data);
}
private function _cleanInputs($data) {
$clean_input = Array();
if (is_array($data)) {
foreach ($data as $k => $v) {
$clean_input[$k] = $this->_cleanInputs($v);
}
} else {
$clean_input = trim(strip_tags($data));
}
return $clean_input;
}
private function _requestStatus($code) {
$status = array(
200 => 'OK',
404 => 'Not Found',
405 => 'Method Not Allowed',
500 => 'Internal Server Error',
);
return ($status[$code])?$status[$code]:$status[500];
}
}

View File

@ -0,0 +1,23 @@
<?php
return array(
'module_class' => 'Module\Api\Main',
'module_file' => 'class.main.php',
'module_name' => 'Api',
'abstract' => false,
'dependencies' => array(),
'events' => array(),
'sections' => array(),
'name' => 'FuzeWorks Api Module',
'description' => 'A Controller server for multiple types of API\'s like REST and SOAP',
'author' => 'TechFuze',
'version' => '1.0.0',
'website' => 'http://fuzeworks.techfuze.net/',
'date_created' => '03-05-2015',
'date_updated' => '03-05-2015',
'enabled' => true,
);

View File

@ -14,7 +14,7 @@ class Main extends Module {
* @access public
*/
public function onLoad() {
// Do Something
}
}