Adicion de Multipart

This commit is contained in:
Brayan Osmar Pereyra Suxo
2014-04-01 16:29:02 -04:00
parent 63f180271f
commit 3e67779f09
2 changed files with 123 additions and 4 deletions

View File

@@ -2,7 +2,7 @@
namespace Maveriks\Extension; namespace Maveriks\Extension;
use Luracast\Restler\Defaults; use Luracast\Restler\Defaults;
use Luracast\Restler\Format\UrlEncodedFormat;
/** /**
* Class Restler * Class Restler
* Extension Restler class to implement in ProcessMaker * Extension Restler class to implement in ProcessMaker
@@ -11,6 +11,35 @@ use Luracast\Restler\Defaults;
*/ */
class Restler extends \Luracast\Restler\Restler class Restler extends \Luracast\Restler\Restler
{ {
public $flagMultipart = false;
public $responseMultipart = array();
public $inputExecute = '';
/**
* This method to set the value flag Multipart
*
* @param boolean $multipart. flag Multipart
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return void
*/
public function setFlagMultipart($multipart = false)
{
$this->flagMultipart = ($multipart === true) ? true : false;
}
/**
* This method to print or save the api response
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return void
*/
protected function respond() protected function respond()
{ {
$this->dispatch('respond'); $this->dispatch('respond');
@@ -21,7 +50,57 @@ class Restler extends \Luracast\Restler\Restler
usleep(1e6 * (Defaults::$throttle / 1e3 - $elapsed)); usleep(1e6 * (Defaults::$throttle / 1e3 - $elapsed));
} }
} }
echo $this->responseData; if ($this->flagMultipart === true) {
$responseTemp['status'] = $this->responseCode;
$responseTemp['response'] = \G::json_decode($this->responseData);
$this->responseMultipart = $responseTemp;
} else {
echo $this->responseData;
}
$this->dispatch('complete'); $this->dispatch('complete');
} }
/**
* This method to set request data
*
* @param boolean $includeQueryParameters.
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return void
*/
public function getRequestData($includeQueryParameters = true)
{
$get = UrlEncodedFormat::decoderTypeFix($_GET);
if ($this->requestMethod == 'PUT'
|| $this->requestMethod == 'PATCH'
|| $this->requestMethod == 'POST'
) {
if (!empty($this->requestData)) {
return $includeQueryParameters
? $this->requestData + $get
: $this->requestData;
}
if ($this->flagMultipart === false) {
$r = file_get_contents('php://input');
if (is_null($r)) {
return array(); //no body
}
} else {
$r = $this->inputExecute;
}
$r = $this->requestFormat->decode($r);
$r = is_array($r)
? array_merge($r, array(Defaults::$fullRequestDataName => $r))
: array(Defaults::$fullRequestDataName => $r);
return $includeQueryParameters
? $r + $get
: $r;
}
return $includeQueryParameters ? $get : array(); //no body
}
} }

View File

@@ -8,6 +8,7 @@ class WebApplication
protected $rootDir = ""; protected $rootDir = "";
protected $workflowDir = ""; protected $workflowDir = "";
protected $requestUri = ""; protected $requestUri = "";
protected $responseMultipart = array();
const RUNNING_DEFAULT = "default.running"; const RUNNING_DEFAULT = "default.running";
const RUNNING_INDEX = "index.running"; const RUNNING_INDEX = "index.running";
@@ -78,18 +79,51 @@ class WebApplication
$this->loadEnvironment($request["workspace"]); $this->loadEnvironment($request["workspace"]);
Util\Logger::log("API::Dispatching ".$_SERVER["REQUEST_METHOD"]." ".$request["uri"]); Util\Logger::log("API::Dispatching ".$_SERVER["REQUEST_METHOD"]." ".$request["uri"]);
$this->dispatchApiRequest($request["uri"], $request["version"]); if (isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && \G::toUpper($_SERVER["HTTP_X_REQUESTED_WITH"]) == 'MULTYPART') {
$this->multipart($request["uri"], $request["version"]);
} else {
$this->dispatchApiRequest($request["uri"], $request["version"]);
}
Util\Logger::log("API::End Dispatching ".$_SERVER["REQUEST_METHOD"]." ".$request["uri"]); Util\Logger::log("API::End Dispatching ".$_SERVER["REQUEST_METHOD"]." ".$request["uri"]);
break; break;
} }
} }
/**
* This method performs the functionality of multipart
*
* @param string $version. Version Api
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return void
*/
public function multipart($uri, $version = "1.0")
{
$stringInput = file_get_contents('php://input');
if (is_null($stringInput)) {
return array(); //no body
}
$input = json_decode($stringInput);
$baseUrl = (empty($input->base_url)) ? $uri : $input->base_url;
foreach($input->calls as $value) {
$_SERVER["REQUEST_METHOD"] = (empty($value->method)) ? 'GET' : $value->method;
$uriTemp = trim($baseUrl) . trim($value->url);
$inputExecute = (empty($value->data)) ? '' : \G::json_encode($value->data);
$this->responseMultipart[] = $this->dispatchApiRequest($uriTemp, $version, true, $inputExecute);
}
echo \G::json_encode($this->responseMultipart);
}
/** /**
* This method dispatch rest/api service * This method dispatch rest/api service
* *
* @author Erik Amaru Ortiz <erik@colosa.com> * @author Erik Amaru Ortiz <erik@colosa.com>
*/ */
public function dispatchApiRequest($uri, $version = "1.0") public function dispatchApiRequest($uri, $version = "1.0", $multipart = false, $inputExecute = '')
{ {
// to handle a request with "OPTIONS" method // to handle a request with "OPTIONS" method
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
@@ -134,6 +168,9 @@ class WebApplication
// create a new Restler instance // create a new Restler instance
//$rest = new \Luracast\Restler\Restler(); //$rest = new \Luracast\Restler\Restler();
$rest = new \Maveriks\Extension\Restler(); $rest = new \Maveriks\Extension\Restler();
// setting flag for multipart to Restler
$rest->setFlagMultipart($multipart);
$rest->inputExecute = $inputExecute;
// setting api version to Restler // setting api version to Restler
$rest->setAPIVersion($version); $rest->setAPIVersion($version);
// adding $authenticationClass to Restler // adding $authenticationClass to Restler
@@ -211,6 +248,9 @@ class WebApplication
} }
$rest->handle(); $rest->handle();
if ($rest->flagMultipart === true) {
return $rest->responseMultipart;
}
} }
public function parseApiRequestUri() public function parseApiRequestUri()