CODE STYLE, workflow/engine/methods/services/Rest/

FILES:
CURLMessage.php
FormatedMessage.php
JsonMessage.php
RestMessage.php
SimpleMessage.php
XmlMessage.php
testing.php
This commit is contained in:
jennylee
2012-10-17 13:10:12 -04:00
parent d435f51203
commit 9b55347f30
7 changed files with 286 additions and 263 deletions

View File

@@ -30,6 +30,7 @@ abstract class CURLMessage
protected $ch; //curl handle instance. protected $ch; //curl handle instance.
protected $output; // contains the output returned by the curl_exec function. protected $output; // contains the output returned by the curl_exec function.
/** /**
* Setting the remote host and init the Curl handle options * Setting the remote host and init the Curl handle options
*/ */
@@ -39,6 +40,7 @@ abstract class CURLMessage
$serverDNS = array_reverse( $serverDNS ); $serverDNS = array_reverse( $serverDNS );
$workspace = array_pop( $serverDNS ); //***aware this must contains the workspace name*** $workspace = array_pop( $serverDNS ); //***aware this must contains the workspace name***
$this->restServer = PROTOCOL_HTTP . COLON . PATH_SEP . PATH_SEP; $this->restServer = PROTOCOL_HTTP . COLON . PATH_SEP . PATH_SEP;
$this->restServer .= $_SERVER['SERVER_NAME'] . PATH_SEP; $this->restServer .= $_SERVER['SERVER_NAME'] . PATH_SEP;
$this->restServer .= $this->serviceTechnic . PATH_SEP . $workspace . PATH_SEP; $this->restServer .= $this->serviceTechnic . PATH_SEP . $workspace . PATH_SEP;
@@ -48,14 +50,17 @@ abstract class CURLMessage
curl_setopt( $this->ch, CURLOPT_POST, 1 ); curl_setopt( $this->ch, CURLOPT_POST, 1 );
curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, 1 );
} }
/** /**
* set the message in order to follow the message format * set the message in order to follow the message format
*/ */
abstract protected function format (array $message); 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. * Set properties used in a simpleMessage Class like a set in a URI, or formatted as a JSon msg.
*/ */
abstract protected function setMoreProperties ($messageFormated); abstract protected function setMoreProperties ($messageFormated);
/** /**
* Attach the method to the restServer path, set the type of the message, and the message itself. * Attach the method to the restServer path, set the type of the message, and the message itself.
*/ */
@@ -65,15 +70,17 @@ abstract class CURLMessage
$this->server_method = $this->restServer . $method; $this->server_method = $this->restServer . $method;
$this->setMoreProperties( $messageFormated ); $this->setMoreProperties( $messageFormated );
} }
/** /**
* Send or execute(curl notation) the message using a rest method * Send or execute(curl notation) the message using a rest method
**/ */
public function send ($method, array $message) public function send ($method, array $message)
{ {
self::setMessageProperties( $method, $message ); self::setMessageProperties( $method, $message );
$this->output = curl_exec( $this->ch ); $this->output = curl_exec( $this->ch );
return $this->output; return $this->output;
} }
/** /**
* Set the message to GET method type * Set the message to GET method type
*/ */
@@ -82,6 +89,7 @@ abstract class CURLMessage
curl_setopt( $this->ch, CURLOPT_HTTPGET, true ); curl_setopt( $this->ch, CURLOPT_HTTPGET, true );
return $this->send( $method, $message ); return $this->send( $method, $message );
} }
/** /**
* Set the message to POST method type * Set the message to POST method type
*/ */
@@ -90,6 +98,7 @@ abstract class CURLMessage
curl_setopt( $this->ch, CURLOPT_POST, true ); curl_setopt( $this->ch, CURLOPT_POST, true );
return $this->send( $method, $message ); return $this->send( $method, $message );
} }
/** /**
* Set the message to PUT method type * Set the message to PUT method type
*/ */
@@ -98,6 +107,7 @@ abstract class CURLMessage
curl_setopt( $this->ch, CURLOPT_PUT, true ); curl_setopt( $this->ch, CURLOPT_PUT, true );
return $this->send( $method, $message ); return $this->send( $method, $message );
} }
/** /**
* Set the message to DELETE method type * Set the message to DELETE method type
*/ */
@@ -106,17 +116,15 @@ abstract class CURLMessage
curl_setopt( $this->ch, CURLOPT_CUSTOMREQUEST, "DELETE" ); curl_setopt( $this->ch, CURLOPT_CUSTOMREQUEST, "DELETE" );
return $this->send( $method, $message ); return $this->send( $method, $message );
} }
/** /**
* Display all the data that the response could got. * Display all the data that the response could got.
*/ */
public function displayResponse () public function displayResponse ()
{ {
$error = curl_error( $this->ch ); $error = curl_error( $this->ch );
$result = array( 'header' => '', $result = array ('header' => '','body' => '','curl_error' => '','http_code' => '','last_url' => ''
'body' => '', );
'curl_error' => '',
'http_code' => '',
'last_url' => '');
if ($error != "") { if ($error != "") {
$result['curl_error'] = $error; $result['curl_error'] = $error;
return $result; return $result;
@@ -129,14 +137,14 @@ abstract class CURLMessage
$result['last_url'] = curl_getinfo( $this->ch, CURLINFO_EFFECTIVE_URL ); $result['last_url'] = curl_getinfo( $this->ch, CURLINFO_EFFECTIVE_URL );
echo $this->type . " Response: " . $response . "<BR>"; echo $this->type . " Response: " . $response . "<BR>";
foreach($result as $index => $data) foreach ($result as $index => $data) {
{
if ($data != "") { if ($data != "") {
echo $index . "=" . $data . "<BR>"; echo $index . "=" . $data . "<BR>";
} }
} }
echo "<BR>"; echo "<BR>";
} }
/** /**
* Close the Curl session using the Curl Handle set by curl_init() function. * Close the Curl session using the Curl Handle set by curl_init() function.
*/ */
@@ -146,4 +154,3 @@ abstract class CURLMessage
} }
} }
?>

View File

@@ -18,16 +18,19 @@ require_once("CURLMessage.php");
*/ */
class FormatedMessage extends CURLMessage class FormatedMessage extends CURLMessage
{ {
public function FormatedMessage () public function FormatedMessage ()
{ {
parent::__construct(); parent::__construct();
} }
/** /**
* Set the message in order to follow the message format, empty caused this class should not be instanced * Set the message in order to follow the message format, empty caused this class should not be instanced
*/ */
protected function format (array $message) protected function format (array $message)
{ {
} }
/** /**
* Setting CURL Url, Content and the message to be send * Setting CURL Url, Content and the message to be send
*/ */
@@ -39,8 +42,9 @@ class FormatedMessage extends CURLMessage
// //
curl_setopt( $this->ch, CURLOPT_URL, $this->server_method ); curl_setopt( $this->ch, CURLOPT_URL, $this->server_method );
$contentSelected = $this->content . $this->type; $contentSelected = $this->content . $this->type;
curl_setopt($this->ch,CURLOPT_HTTPHEADER,array($contentSelected)); curl_setopt( $this->ch, CURLOPT_HTTPHEADER, array ($contentSelected
) );
curl_setopt( $this->ch, CURLOPT_POSTFIELDS, $messageFormated ); curl_setopt( $this->ch, CURLOPT_POSTFIELDS, $messageFormated );
} }
} }
?>

View File

@@ -12,11 +12,13 @@
*/ */
require_once ("FormatedMessage.php"); require_once ("FormatedMessage.php");
/** /**
* Class defined to be instanced and handle Json messages * Class defined to be instanced and handle Json messages
*/ */
class JsonMessage extends FormatedMessage class JsonMessage extends FormatedMessage
{ {
/** /**
* Call the parent Curl initialization and set the type of the message * Call the parent Curl initialization and set the type of the message
*/ */
@@ -25,6 +27,7 @@ class JsonMessage extends FormatedMessage
parent::__construct(); parent::__construct();
$this->type = "json"; $this->type = "json";
} }
/** /**
* Format the array parameter to a json format. * Format the array parameter to a json format.
*/ */
@@ -40,4 +43,3 @@ class JsonMessage extends FormatedMessage
} }
} }
?>

View File

@@ -12,11 +12,13 @@
*/ */
require_once ("SimpleMessage.php"); require_once ("SimpleMessage.php");
/** /**
* Class defined to be instanced and handle rest single parameters * Class defined to be instanced and handle rest single parameters
*/ */
class RestMessage extends SimpleMessage class RestMessage extends SimpleMessage
{ {
/** /**
* Call the parent Curl initialization and set the type of the message * Call the parent Curl initialization and set the type of the message
*/ */
@@ -25,6 +27,7 @@ class RestMessage extends SimpleMessage
parent::__construct(); parent::__construct();
$this->type = "rest"; $this->type = "rest";
} }
/** /**
* Format the array parameter to a single rest line format separed by '/'. * Format the array parameter to a single rest line format separed by '/'.
*/ */
@@ -33,8 +36,7 @@ class RestMessage extends SimpleMessage
$rest = ""; $rest = "";
if (! empty( $message )) { if (! empty( $message )) {
if (is_array( $message )) { if (is_array( $message )) {
foreach($message as $index => $data) foreach ($message as $index => $data) {
{
$rest .= "/" . $data; $rest .= "/" . $data;
} }
$rest .= "/"; $rest .= "/";
@@ -44,4 +46,3 @@ class RestMessage extends SimpleMessage
} }
} }
?>

View File

@@ -18,16 +18,19 @@ require_once("CURLMessage.php");
*/ */
class SimpleMessage extends CURLMessage class SimpleMessage extends CURLMessage
{ {
public function SimpleMessage () public function SimpleMessage ()
{ {
parent::__construct(); parent::__construct();
} }
/** /**
* Set the message in order to follow the message format, empty caused this class should not be instanced * Set the message in order to follow the message format, empty caused this class should not be instanced
*/ */
protected function format (array $message) protected function format (array $message)
{ {
} }
/** /**
* Setting CURL Url, enough to attach a message. * Setting CURL Url, enough to attach a message.
*/ */
@@ -40,4 +43,4 @@ class SimpleMessage extends CURLMessage
curl_setopt( $this->ch, CURLOPT_URL, $this->server_method . $messageFormated ); curl_setopt( $this->ch, CURLOPT_URL, $this->server_method . $messageFormated );
} }
} }
?>

View File

@@ -12,11 +12,13 @@
*/ */
require_once ("FormatedMessage.php"); require_once ("FormatedMessage.php");
/** /**
* Class defined to be instanced and handle XML format messages. * Class defined to be instanced and handle XML format messages.
*/ */
class XmlMessage extends FormatedMessage class XmlMessage extends FormatedMessage
{ {
/** /**
* Call the parent Curl initialization and set the type of the message * Call the parent Curl initialization and set the type of the message
*/ */
@@ -25,8 +27,10 @@ class XmlMessage extends FormatedMessage
parent::__construct(); parent::__construct();
$this->type = "xml"; $this->type = "xml";
} }
/** /**
* Format the array parameter to a xml valid format. TODO: Need to find out a better way to do it. * 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) protected function format (array $message)
{ {
@@ -35,8 +39,7 @@ class XmlMessage extends FormatedMessage
} }
if (is_array( $message )) { if (is_array( $message )) {
$xml = "<?xml version='1.0'?><request>"; $xml = "<?xml version='1.0'?><request>";
foreach($message as $index => $data) foreach ($message as $index => $data) {
{
$xml .= "<" . $index . ">" . $data . "</" . $index . ">"; $xml .= "<" . $index . ">" . $data . "</" . $index . ">";
} }
$xml .= "</request>"; $xml .= "</request>";
@@ -45,4 +48,3 @@ class XmlMessage extends FormatedMessage
} }
} }
?>

View File

@@ -4,7 +4,8 @@ require_once("JsonMessage.php");
require_once ("XmlMessage.php"); require_once ("XmlMessage.php");
require_once ("RestMessage.php"); require_once ("RestMessage.php");
$msg = array( 'user'=>'admin' , 'password'=>'admin'); $msg = array ('user' => 'admin','password' => 'admin'
);
$method = "login"; $method = "login";
$jsonm = new JsonMessage(); $jsonm = new JsonMessage();
@@ -15,23 +16,26 @@ $xmlm = new XmlMessage();
$xmlm->send( $method, $msg ); $xmlm->send( $method, $msg );
$xmlm->displayResponse(); $xmlm->displayResponse();
$msg = array( "LABEL", "LOGIN", "en"); $msg = array ("LABEL","LOGIN","en"
);
$table = "TRANSLATION"; $table = "TRANSLATION";
$rest = new RestMessage(); $rest = new RestMessage();
$rest->sendGET( $table, $msg ); $rest->sendGET( $table, $msg );
$rest->displayResponse(); $rest->displayResponse();
$msg = array( "HOUSE", "PUSHIN", "en", "sample", "2012-06-06" ); $msg = array ("HOUSE","PUSHIN","en","sample","2012-06-06"
);
$rest->sendPOST( $table, $msg ); $rest->sendPOST( $table, $msg );
$rest->displayResponse(); $rest->displayResponse();
$msg = array( "HOUSE", "PUSHIN", "en", "samplemod", "2012-07-06" ); $msg = array ("HOUSE","PUSHIN","en","samplemod","2012-07-06"
);
$rest->sendPUT( $table, $msg ); $rest->sendPUT( $table, $msg );
$rest->displayResponse(); $rest->displayResponse();
$msg = array( "HOUSE", "PUSHIN", "en"); $msg = array ("HOUSE","PUSHIN","en"
);
$rest->sendDELETE( $table, $msg ); $rest->sendDELETE( $table, $msg );
$rest->displayResponse(); $rest->displayResponse();
?>