Dispatched 'Request::remote' to 'Loader::remote' as a builder adapter
This commit is contained in:
parent
0fed2ee61e
commit
dfdb84e8a0
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -6,7 +6,6 @@
|
|||
use \api\core\ModuleFactory;
|
||||
use \error\core\Error;
|
||||
use \error\core\Err;
|
||||
use \http\core\HttpRequest;
|
||||
|
||||
|
||||
class Request{
|
||||
|
@ -335,88 +334,6 @@
|
|||
}
|
||||
|
||||
|
||||
/* DESERIALISATION A PARTIR DE L'URL ET DES DONNEES POST (OPT)
|
||||
*
|
||||
* @url<String> Contenu de l'url formatté (commence à "/module/methode")
|
||||
*
|
||||
* @return instance<Request> Retourne un objet de type <Request>
|
||||
*
|
||||
* @note
|
||||
* 1. `path` peut être dans l'url : /method/module
|
||||
* `path` peut être dans les données $_POST
|
||||
* 2. les données peuvent être dans l'url : /module/method/data1/data2/...
|
||||
* les données peuvent être dans les données $_POST
|
||||
*
|
||||
*/
|
||||
public static function remote($url){
|
||||
|
||||
/* [1] Fetch HttpRequest correct data
|
||||
=========================================================*/
|
||||
/* (1) Parse HttpRequest data because php doesn't parse it for non-POST HTTP method */
|
||||
$httprequest = new HttpRequest();
|
||||
$_POST = $httprequest->POST();
|
||||
|
||||
$data = $_POST;
|
||||
|
||||
/* [2] On verifie que le @path est renseigne
|
||||
=========================================================*/
|
||||
/* (1) Si le path est dans @url */
|
||||
$pathInUrl = is_string($url) && preg_match('#^/?([\w_-]+/[\w_-]+)(?:/?|/((?:\w+/)*(?:\w+/?)))$#', $url, $urlMatches);
|
||||
|
||||
/* (2) On récupère le @path + les arguments dans l'URL */
|
||||
if( $pathInUrl ){
|
||||
// {1} On ajoute le @path aux données //
|
||||
$data['path'] = $urlMatches[1];
|
||||
|
||||
// {2} On ajoute les arguments d'URL aux données //
|
||||
if( count($urlMatches) > 2 ){
|
||||
|
||||
$urlParams = explode('/', trim($urlMatches[2], '/'));
|
||||
foreach($urlParams as $k=>$v)
|
||||
$data["URL_$k"] = $v;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* (2) On vérifie dans tous les cas si le path existe */
|
||||
if( !isset($data['path']) )
|
||||
return new Request();
|
||||
|
||||
|
||||
|
||||
/* [3] On met les paramètres en JSON
|
||||
=========================================================*/
|
||||
/* (1) On initialise les paramètres*/
|
||||
$params = [];
|
||||
|
||||
/* (2) On met tous les paramètres en json (sauf @path) */
|
||||
foreach($data as $name=>$value){
|
||||
if( $name === 'path' )
|
||||
continue;
|
||||
|
||||
// {1} On met en JSON //
|
||||
$json = json_decode( $value, true );
|
||||
|
||||
// {2} Si ok -> on remplace //
|
||||
if( !is_null($json) )
|
||||
$params[$name] = $json;
|
||||
|
||||
// {3} Sinon, on laisse tel quel //
|
||||
else
|
||||
$params[$name] = $value;
|
||||
}
|
||||
|
||||
/* [4] On retourne une instance de <Request>
|
||||
=========================================================*/
|
||||
return new Request($data['path'], $params);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* VERIFICATION DU FORMAT ET DE LA COHERENCE DU CHEMIN SPECIFIE
|
||||
*
|
||||
* @path<String> String correspondant au chemin de delegation ("module/methode")
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
require_once '../vendor/autoload.php';
|
||||
|
||||
use \router\core\Router;
|
||||
use \api\core\Loader;
|
||||
use \api\core\Request;
|
||||
use \api\core\Response;
|
||||
use \database\core\DatabaseDriver;
|
||||
|
@ -203,7 +204,7 @@
|
|||
if( !$GLOBALS['session_guard']->init_child() )
|
||||
die(json_encode([ 'error' => 100, 'ErrorDescription' => 'session_guard.child error' ]));
|
||||
|
||||
$request = Request::remote($url[0]);
|
||||
$request = Loader::remote($url[0]);
|
||||
$answer = $request->dispatch();
|
||||
|
||||
// Si c'est une réponse (et non un download)
|
||||
|
|
Loading…
Reference in New Issue