From 9b55347f3013ff51d703a39ddd82b988c9965825 Mon Sep 17 00:00:00 2001 From: jennylee Date: Wed, 17 Oct 2012 13:10:12 -0400 Subject: [PATCH] CODE STYLE, workflow/engine/methods/services/Rest/ FILES: CURLMessage.php FormatedMessage.php JsonMessage.php RestMessage.php SimpleMessage.php XmlMessage.php testing.php --- .../methods/services/Rest/CURLMessage.php | 209 +++++++++--------- .../methods/services/Rest/FormatedMessage.php | 56 ++--- .../methods/services/Rest/JsonMessage.php | 56 ++--- .../methods/services/Rest/RestMessage.php | 53 ++--- .../methods/services/Rest/SimpleMessage.php | 85 +++---- .../methods/services/Rest/XmlMessage.php | 56 ++--- .../engine/methods/services/Rest/testing.php | 34 +-- 7 files changed, 286 insertions(+), 263 deletions(-) diff --git a/workflow/engine/methods/services/Rest/CURLMessage.php b/workflow/engine/methods/services/Rest/CURLMessage.php index ae2ed0dbe..3017ade84 100644 --- a/workflow/engine/methods/services/Rest/CURLMessage.php +++ b/workflow/engine/methods/services/Rest/CURLMessage.php @@ -1,149 +1,156 @@ restServer = PROTOCOL_HTTP . COLON.PATH_SEP . PATH_SEP; + + /** + * Setting the remote host and init the Curl handle options + */ + function __construct () + { + $serverDNS = explode( DOT, $_SERVER['SERVER_NAME'] ); + $serverDNS = array_reverse( $serverDNS ); + $workspace = array_pop( $serverDNS ); //***aware this must contains the workspace name*** + + + $this->restServer = PROTOCOL_HTTP . COLON . PATH_SEP . PATH_SEP; $this->restServer .= $_SERVER['SERVER_NAME'] . PATH_SEP; $this->restServer .= $this->serviceTechnic . PATH_SEP . $workspace . PATH_SEP; $this->ch = curl_init(); - curl_setopt($this->ch,CURLOPT_TIMEOUT, 2); - curl_setopt($this->ch,CURLOPT_POST, 1); - curl_setopt($this->ch,CURLOPT_RETURNTRANSFER, 1); + curl_setopt( $this->ch, CURLOPT_TIMEOUT, 2 ); + curl_setopt( $this->ch, CURLOPT_POST, 1 ); + curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, 1 ); } + /** - * set the message in order to follow the message format - */ - abstract protected function format(array $message); + * set the message in order to follow the message format + */ + abstract protected function format (array $message); + /** - * Set properties used in a simpleMessage Class like a set in a URI, or formatted as a JSon msg. - */ - abstract protected function setMoreProperties($messageFormated); + * Set properties used in a simpleMessage Class like a set in a URI, or formatted as a JSon msg. + */ + abstract protected function setMoreProperties ($messageFormated); + /** - * Attach the method to the restServer path, set the type of the message, and the message itself. - */ - protected function setMessageProperties($method,array $message) + * Attach the method to the restServer path, set the type of the message, and the message itself. + */ + protected function setMessageProperties ($method, array $message) { - $messageFormated = $this->format($message); + $messageFormated = $this->format( $message ); $this->server_method = $this->restServer . $method; - $this->setMoreProperties($messageFormated); + $this->setMoreProperties( $messageFormated ); } + /** - * Send or execute(curl notation) the message using a rest method - **/ - public function send($method,array $message) - { - self::setMessageProperties($method,$message); - $this->output = curl_exec($this->ch); + * Send or execute(curl notation) the message using a rest method + */ + public function send ($method, array $message) + { + self::setMessageProperties( $method, $message ); + $this->output = curl_exec( $this->ch ); return $this->output; } + /** - * Set the message to GET method type - */ - public function sendGET($method,array $message) - { - curl_setopt($this->ch, CURLOPT_HTTPGET,true); - return $this->send($method,$message); - } - /** - * Set the message to POST method type - */ - public function sendPOST($method,array $message) + * Set the message to GET method type + */ + public function sendGET ($method, array $message) { - curl_setopt($this->ch,CURLOPT_POST,true); - return $this->send($method,$message); + curl_setopt( $this->ch, CURLOPT_HTTPGET, true ); + return $this->send( $method, $message ); } + /** - * Set the message to PUT method type - */ - public function sendPUT($method,array $message) + * Set the message to POST method type + */ + public function sendPOST ($method, array $message) { - curl_setopt($this->ch,CURLOPT_PUT,true); - return $this->send($method,$message); + curl_setopt( $this->ch, CURLOPT_POST, true ); + return $this->send( $method, $message ); } + /** - * Set the message to DELETE method type - */ - public function sendDELETE($method,array $message) + * Set the message to PUT method type + */ + public function sendPUT ($method, array $message) { - curl_setopt($this->ch,CURLOPT_CUSTOMREQUEST,"DELETE"); - return $this->send($method,$message); + curl_setopt( $this->ch, CURLOPT_PUT, true ); + return $this->send( $method, $message ); } + /** - * Display all the data that the response could got. - */ - public function displayResponse() + * Set the message to DELETE method type + */ + public function sendDELETE ($method, array $message) { - $error = curl_error($this->ch); - $result = array( 'header' => '', - 'body' => '', - 'curl_error' => '', - 'http_code' => '', - 'last_url' => ''); - if ($error != ""){ + curl_setopt( $this->ch, CURLOPT_CUSTOMREQUEST, "DELETE" ); + return $this->send( $method, $message ); + } + + /** + * Display all the data that the response could got. + */ + public function displayResponse () + { + $error = curl_error( $this->ch ); + $result = array ('header' => '','body' => '','curl_error' => '','http_code' => '','last_url' => '' + ); + if ($error != "") { $result['curl_error'] = $error; return $result; } $response = $this->output; - $header_size = curl_getinfo($this->ch,CURLINFO_HEADER_SIZE); - $result['header'] = substr($response,0,$header_size); - $result['body'] = substr($response,$header_size); - $result['http_code'] = curl_getinfo($this -> ch,CURLINFO_HTTP_CODE); - $result['last_url'] = curl_getinfo($this -> ch,CURLINFO_EFFECTIVE_URL); + $header_size = curl_getinfo( $this->ch, CURLINFO_HEADER_SIZE ); + $result['header'] = substr( $response, 0, $header_size ); + $result['body'] = substr( $response, $header_size ); + $result['http_code'] = curl_getinfo( $this->ch, CURLINFO_HTTP_CODE ); + $result['last_url'] = curl_getinfo( $this->ch, CURLINFO_EFFECTIVE_URL ); - echo $this->type." Response: ".$response."
"; - foreach($result as $index => $data) - { - if($data != ""){ + echo $this->type . " Response: " . $response . "
"; + foreach ($result as $index => $data) { + if ($data != "") { echo $index . "=" . $data . "
"; } } echo "
"; } + /** - * Close the Curl session using the Curl Handle set by curl_init() function. - */ - public function close() + * Close the Curl session using the Curl Handle set by curl_init() function. + */ + public function close () { - curl_close($this->ch); + curl_close( $this->ch ); } } -?> diff --git a/workflow/engine/methods/services/Rest/FormatedMessage.php b/workflow/engine/methods/services/Rest/FormatedMessage.php index 191a693fe..15661314c 100644 --- a/workflow/engine/methods/services/Rest/FormatedMessage.php +++ b/workflow/engine/methods/services/Rest/FormatedMessage.php @@ -1,46 +1,50 @@ server_method . PATH_SEP . $messageFormated . "
"; // - curl_setopt($this->ch,CURLOPT_URL,$this->server_method); + curl_setopt( $this->ch, CURLOPT_URL, $this->server_method ); $contentSelected = $this->content . $this->type; - curl_setopt($this->ch,CURLOPT_HTTPHEADER,array($contentSelected)); - curl_setopt($this->ch,CURLOPT_POSTFIELDS,$messageFormated); + curl_setopt( $this->ch, CURLOPT_HTTPHEADER, array ($contentSelected + ) ); + curl_setopt( $this->ch, CURLOPT_POSTFIELDS, $messageFormated ); } } -?> \ No newline at end of file + diff --git a/workflow/engine/methods/services/Rest/JsonMessage.php b/workflow/engine/methods/services/Rest/JsonMessage.php index 2a8d8f332..34d06135b 100644 --- a/workflow/engine/methods/services/Rest/JsonMessage.php +++ b/workflow/engine/methods/services/Rest/JsonMessage.php @@ -1,43 +1,45 @@ type = "json"; } + /** - * Format the array parameter to a json format. - */ - protected function format(array $message) + * Format the array parameter to a json format. + */ + protected function format (array $message) { - if (empty($message)){ - return ; + if (empty( $message )) { + return; } - if (is_array($message)){ - $jsonMessage = json_encode( $message); + if (is_array( $message )) { + $jsonMessage = json_encode( $message ); } return $jsonMessage; } } -?> \ No newline at end of file diff --git a/workflow/engine/methods/services/Rest/RestMessage.php b/workflow/engine/methods/services/Rest/RestMessage.php index f6c7488b6..0c9e4dcee 100644 --- a/workflow/engine/methods/services/Rest/RestMessage.php +++ b/workflow/engine/methods/services/Rest/RestMessage.php @@ -1,40 +1,42 @@ type = "rest"; } + /** - * Format the array parameter to a single rest line format separed by '/'. - */ - protected function format(array $message) + * Format the array parameter to a single rest line format separed by '/'. + */ + protected function format (array $message) { $rest = ""; - if (!empty($message)){ - if (is_array($message)){ - foreach($message as $index => $data) - { + if (! empty( $message )) { + if (is_array( $message )) { + foreach ($message as $index => $data) { $rest .= "/" . $data; } $rest .= "/"; @@ -44,4 +46,3 @@ class RestMessage extends SimpleMessage } } -?> diff --git a/workflow/engine/methods/services/Rest/SimpleMessage.php b/workflow/engine/methods/services/Rest/SimpleMessage.php index 94cfd3ec1..5a67123ec 100644 --- a/workflow/engine/methods/services/Rest/SimpleMessage.php +++ b/workflow/engine/methods/services/Rest/SimpleMessage.php @@ -1,43 +1,46 @@ -server_method . PATH_SEP . $messageFormated . "
"; - // - curl_setopt($this->ch,CURLOPT_URL,$this->server_method.$messageFormated); - } -} -?> \ No newline at end of file + } + + /** + * Set the message in order to follow the message format, empty caused this class should not be instanced + */ + protected function format (array $message) + { + } + + /** + * Setting CURL Url, enough to attach a message. + */ + protected function setMoreProperties ($messageFormated) + { + //Please, remove this comment, is only for looging proposes. + // + echo "Request: " . $this->server_method . PATH_SEP . $messageFormated . "
"; + // + curl_setopt( $this->ch, CURLOPT_URL, $this->server_method . $messageFormated ); + } +} + diff --git a/workflow/engine/methods/services/Rest/XmlMessage.php b/workflow/engine/methods/services/Rest/XmlMessage.php index 9d33db4fd..f6c597100 100644 --- a/workflow/engine/methods/services/Rest/XmlMessage.php +++ b/workflow/engine/methods/services/Rest/XmlMessage.php @@ -1,42 +1,45 @@ type = "xml"; } + /** - * Format the array parameter to a xml valid format. TODO: Need to find out a better way to do it. - */ - protected function format(array $message) + * Format the array parameter to a xml valid format. + * TODO: Need to find out a better way to do it. + */ + protected function format (array $message) { - if (empty($message)){ - return ; + if (empty( $message )) { + return; } - if (is_array($message)){ + if (is_array( $message )) { $xml = ""; - foreach($message as $index => $data) - { + foreach ($message as $index => $data) { $xml .= "<" . $index . ">" . $data . ""; } $xml .= ""; @@ -45,4 +48,3 @@ class XmlMessage extends FormatedMessage } } -?> diff --git a/workflow/engine/methods/services/Rest/testing.php b/workflow/engine/methods/services/Rest/testing.php index 3b93426e0..327c9c0c2 100644 --- a/workflow/engine/methods/services/Rest/testing.php +++ b/workflow/engine/methods/services/Rest/testing.php @@ -1,37 +1,41 @@ 'admin' , 'password'=>'admin'); +$msg = array ('user' => 'admin','password' => 'admin' +); $method = "login"; $jsonm = new JsonMessage(); -$jsonm->send($method,$msg); +$jsonm->send( $method, $msg ); $jsonm->displayResponse(); $xmlm = new XmlMessage(); -$xmlm->send($method, $msg); +$xmlm->send( $method, $msg ); $xmlm->displayResponse(); -$msg = array( "LABEL", "LOGIN", "en"); +$msg = array ("LABEL","LOGIN","en" +); $table = "TRANSLATION"; $rest = new RestMessage(); -$rest->sendGET($table,$msg); +$rest->sendGET( $table, $msg ); $rest->displayResponse(); -$msg = array( "HOUSE", "PUSHIN", "en", "sample", "2012-06-06" ); -$rest->sendPOST($table,$msg); +$msg = array ("HOUSE","PUSHIN","en","sample","2012-06-06" +); +$rest->sendPOST( $table, $msg ); $rest->displayResponse(); -$msg = array( "HOUSE", "PUSHIN", "en", "samplemod", "2012-07-06" ); -$rest->sendPUT($table,$msg); +$msg = array ("HOUSE","PUSHIN","en","samplemod","2012-07-06" +); +$rest->sendPUT( $table, $msg ); $rest->displayResponse(); -$msg = array( "HOUSE", "PUSHIN", "en"); -$rest->sendDELETE($table,$msg); +$msg = array ("HOUSE","PUSHIN","en" +); +$rest->sendDELETE( $table, $msg ); $rest->displayResponse(); -?>