106 lines
2.4 KiB
PHP
Executable File
106 lines
2.4 KiB
PHP
Executable File
<?php
|
|
|
|
/**************************
|
|
* API Loader *
|
|
* 18-07-2017 *
|
|
***************************
|
|
* Designed & Developed by *
|
|
* xdrm-brackets *
|
|
***************************
|
|
* https://xdrm.io/ *
|
|
**************************/
|
|
|
|
namespace api\core;
|
|
|
|
use \error\core\Error;
|
|
use \error\core\Err;
|
|
use \http\core\HttpRequest;
|
|
use \api\core\Request;
|
|
|
|
|
|
class Loader{
|
|
|
|
|
|
/* (1) Build an API Request from the HTTP Request
|
|
*
|
|
* @uri<String> URI
|
|
*
|
|
* @return outName<outType> outDesc
|
|
*
|
|
---------------------------------------------------------*/
|
|
public static function remote($uri){
|
|
|
|
/* (1) Fetch HttpRequest correct data
|
|
---------------------------------------------------------*/
|
|
/* (1) Parse HttpRequest data because php doesn't parse it for non-POST HTTP method */
|
|
$httprequest = new HttpRequest();
|
|
|
|
/* (2) For later use -> replace default @_POST global */
|
|
$_POST = $httprequest->POST();
|
|
|
|
/* (3) Get @data from @_POST values */
|
|
$data = $_POST;
|
|
|
|
|
|
/* (2) Check if @path var is set
|
|
---------------------------------------------------------*/
|
|
/* (1) If is in @uri */
|
|
$pathInUrl = is_string($uri) && preg_match('#^/?([\w_-]+/[\w_-]+)(?:/?|/((?:\w+/)*(?:\w+/?)))$#', $uri, $uriMatches);
|
|
|
|
/* (2) Get @path from @uri + @uri arguments if there is */
|
|
if( $pathInUrl ){
|
|
|
|
// {1} Add @path as data //
|
|
$data['path'] = $uriMatches[1];
|
|
|
|
// {2} Add $uri arguments as data 'URL_@i' (@i is the order beginnint at 0) //
|
|
if( count($uriMatches) > 2 ){
|
|
|
|
$uriParams = explode('/', trim($uriMatches[2], '/'));
|
|
|
|
foreach($uriParams as $k=>$v)
|
|
$data["URL_$k"] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* (3) If @path haven't been found -> error */
|
|
if( !isset($data['path']) )
|
|
return new Request();
|
|
|
|
|
|
|
|
/* (3) Parse arguments from JSON
|
|
---------------------------------------------------------*/
|
|
/* (1) Init. arguments */
|
|
$params = [];
|
|
|
|
/* (2) Parse each arg (except @path) */
|
|
foreach($data as $name=>$value){
|
|
|
|
if( $name === 'path' )
|
|
continue;
|
|
|
|
// {1} Json parse //
|
|
$json = json_decode( $value, true );
|
|
|
|
// {2} if valid -> set the parsed value //
|
|
if( !is_null($json) )
|
|
$params[$name] = $json;
|
|
|
|
// {3} else -> leave it like it was //
|
|
else
|
|
$params[$name] = $value;
|
|
}
|
|
|
|
|
|
/* (4) Build an API Request object
|
|
---------------------------------------------------------*/
|
|
return new Request($data['path'], $params);
|
|
}
|
|
|
|
|
|
|
|
|
|
} |