From 3e67779f096ba09e420096bbbc85411a53d8ebf8 Mon Sep 17 00:00:00 2001 From: Brayan Osmar Pereyra Suxo Date: Tue, 1 Apr 2014 16:29:02 -0400 Subject: [PATCH] Adicion de Multipart --- framework/src/Maveriks/Extension/Restler.php | 83 +++++++++++++++++++- framework/src/Maveriks/WebApplication.php | 44 ++++++++++- 2 files changed, 123 insertions(+), 4 deletions(-) diff --git a/framework/src/Maveriks/Extension/Restler.php b/framework/src/Maveriks/Extension/Restler.php index b211f77f3..e13402ecc 100644 --- a/framework/src/Maveriks/Extension/Restler.php +++ b/framework/src/Maveriks/Extension/Restler.php @@ -2,7 +2,7 @@ namespace Maveriks\Extension; use Luracast\Restler\Defaults; - +use Luracast\Restler\Format\UrlEncodedFormat; /** * Class Restler * Extension Restler class to implement in ProcessMaker @@ -11,6 +11,35 @@ use Luracast\Restler\Defaults; */ 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) + * @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) + * @copyright Colosa - Bolivia + * + * @return void + */ protected function respond() { $this->dispatch('respond'); @@ -21,7 +50,57 @@ class Restler extends \Luracast\Restler\Restler 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 method to set request data + * + * @param boolean $includeQueryParameters. + * + * @access public + * @author Brayan Pereyra (Cochalo) + * @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 + } } \ No newline at end of file diff --git a/framework/src/Maveriks/WebApplication.php b/framework/src/Maveriks/WebApplication.php index 1181216bb..04c8070e5 100644 --- a/framework/src/Maveriks/WebApplication.php +++ b/framework/src/Maveriks/WebApplication.php @@ -8,6 +8,7 @@ class WebApplication protected $rootDir = ""; protected $workflowDir = ""; protected $requestUri = ""; + protected $responseMultipart = array(); const RUNNING_DEFAULT = "default.running"; const RUNNING_INDEX = "index.running"; @@ -78,18 +79,51 @@ class WebApplication $this->loadEnvironment($request["workspace"]); 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"]); break; } } + /** + * This method performs the functionality of multipart + * + * @param string $version. Version Api + * + * @access public + * @author Brayan Pereyra (Cochalo) + * @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 * * @author Erik Amaru Ortiz */ - public function dispatchApiRequest($uri, $version = "1.0") + public function dispatchApiRequest($uri, $version = "1.0", $multipart = false, $inputExecute = '') { // to handle a request with "OPTIONS" method if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { @@ -134,6 +168,9 @@ class WebApplication // create a new Restler instance //$rest = new \Luracast\Restler\Restler(); $rest = new \Maveriks\Extension\Restler(); + // setting flag for multipart to Restler + $rest->setFlagMultipart($multipart); + $rest->inputExecute = $inputExecute; // setting api version to Restler $rest->setAPIVersion($version); // adding $authenticationClass to Restler @@ -211,6 +248,9 @@ class WebApplication } $rest->handle(); + if ($rest->flagMultipart === true) { + return $rest->responseMultipart; + } } public function parseApiRequestUri()