diff --git a/RestService/Client.php b/RestService/Client.php index 67a5576..4e4bf08 100644 --- a/RestService/Client.php +++ b/RestService/Client.php @@ -83,6 +83,7 @@ class Client 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', + 418 => 'I\'m a Tea Pot', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', @@ -126,7 +127,7 @@ public function getController() * @param string $pHttpCode * @param $pMessage */ - public function sendResponse($pHttpCode = '200', $pMessage) + public function sendResponse($pMessage, $pHttpCode = '200') { $suppressStatusCode = isset($_GET['_suppress_status_code']) ? $_GET['_suppress_status_code'] : false; if ($this->controller->getHttpStatusCodes() && diff --git a/RestService/InternalClient.php b/RestService/InternalClient.php index e9eda18..753aea7 100644 --- a/RestService/InternalClient.php +++ b/RestService/InternalClient.php @@ -10,7 +10,7 @@ */ class InternalClient extends Client { - public function sendResponse($pHttpCode = '200', $pMessage) + public function sendResponse($pMessage, $pHttpCode = '200') { $pMessage = array_reverse($pMessage, true); $pMessage['status'] = $pHttpCode+0; diff --git a/RestService/Server.php b/RestService/Server.php index 8436792..0ed98e4 100644 --- a/RestService/Server.php +++ b/RestService/Server.php @@ -123,6 +123,13 @@ class Server * @var boolean */ protected $withStatusCode = true; + + /** + * Custom user httpCode + * + * @var integer + */ + protected $httpStatusCode = 200; /** * @var callable @@ -163,6 +170,7 @@ public function __construct($pTriggerUrl, $pControllerClass = null, $pParentCont } else { $this->setClient(new Client($this)); + $this->parsePhpInput(); } $this->setClass($pControllerClass); @@ -217,6 +225,19 @@ public function setHttpStatusCodes($pWithStatusCode) return $this; } + + /** + * Setting up http code + * + * @param integer $httpStatusCode + * @return Server $this + */ + + public function setHttpStatusCode($httpStatusCode) + { + $this->httpStatusCode = $httpStatusCode; + return $this; + } /** * @@ -226,6 +247,15 @@ public function getHttpStatusCodes() { return $this->withStatusCode; } + + /** + * + * @return integer + */ + public function getHttpStatusCode() + { + return $this->httpStatusCode; + } /** * Set the check access function/method. @@ -422,10 +452,14 @@ public function getClient() */ public function sendBadRequest($pCode, $pMessage) { - if (is_object($pMessage) && $pMessage->xdebug_message) $pMessage = $pMessage->xdebug_message; - $msg = array('error' => $pCode, 'message' => $pMessage); - if (!$this->getClient()) throw new \Exception('client_not_found_in_ServerController'); - return $this->getClient()->sendResponse('400', $msg); + $httpCode = ($this->httpStatusCode && substr($this->httpStatusCode, 0, 1) === '4')? $this->httpStatusCode : 400; + if (is_object($pMessage) && $pMessage->xdebug_message) { + $pMessage = $pMessage->xdebug_message; + } + if (!$this->getClient()) { + throw new \Exception('client_not_found_in_ServerController'); + } + return $this->setHttpStatusCode($httpCode)->send(array('error' => $pCode, 'message' => $pMessage)); } /** @@ -437,10 +471,14 @@ public function sendBadRequest($pCode, $pMessage) */ public function sendError($pCode, $pMessage) { - if (is_object($pMessage) && $pMessage->xdebug_message) $pMessage = $pMessage->xdebug_message; - $msg = array('error' => $pCode, 'message' => $pMessage); - if (!$this->getClient()) throw new \Exception('client_not_found_in_ServerController'); - return $this->getClient()->sendResponse('500', $msg); + $httpCode = ($this->httpStatusCode && substr($this->httpStatusCode, 0, 1) === '5')? $this->httpStatusCode : 400; + if (is_object($pMessage) && $pMessage->xdebug_message) { + $pMessage = $pMessage->xdebug_message; + } + if (!$this->getClient()) { + throw new \Exception('client_not_found_in_ServerController'); + } + return $this->setHttpStatusCode($httpCode)->send(array('error' => $pCode, 'message' => $pMessage)); } /** @@ -455,7 +493,9 @@ public function sendException($pException) } $message = $pException->getMessage(); - if (is_object($message) && $message->xdebug_message) $message = $message->xdebug_message; + if (is_object($message) && $message->xdebug_message) { + $message = $message->xdebug_message; + } $msg = array('error' => get_class($pException), 'message' => $message); @@ -465,9 +505,10 @@ public function sendException($pException) $msg['trace'] = $pException->getTraceAsString(); } - if (!$this->getClient()) throw new \Exception('Client not found in ServerController'); - return $this->getClient()->sendResponse('500', $msg); - + if (!$this->getClient()) { + throw new \Exception('Client not found in ServerController'); + } + return $this->setHttpStatusCode(500)->send($msg); } /** @@ -682,7 +723,15 @@ public function normalizeUrl(&$pUrl) */ public function send($pData) { - return $this->getClient()->sendResponse(200, array('data' => $pData)); + $msg = array(); + $httpCode = ($this->httpStatusCode? $this->httpStatusCode : 200); + if( substr($this->httpStatusCode, 0, 1) === '2' ) + { + $msg['data'] = $pData; + } else { + $msg = $pData; + } + return $this->getClient()->sendResponse($msg, $httpCode); } /** @@ -1207,5 +1256,28 @@ public function findRoute($pUri, $pMethod = '_all_') return false; } + + protected function parsePhpInput() + { + $input = $this->getPhpInput(); + if($input) + { + $data = array(); + if(isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) + { + $data = (array) json_decode($input); + } + //xml parser ? + if( empty($data) ) + { + parse_str($input, $data); + } + $_POST = array_merge($_POST, $data); + } + } + protected function getPhpInput() + { + return file_get_contents('php://input'); + } }