Skip to content
This repository was archived by the owner on Jul 6, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion RestService/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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() &&
Expand Down
2 changes: 1 addition & 1 deletion RestService/InternalClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
99 changes: 86 additions & 13 deletions RestService/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ class Server
* @var boolean
*/
protected $withStatusCode = true;

/**
* Custom user httpCode
*
* @var integer
*/
protected $httpStatusCode = 200;

/**
* @var callable
Expand Down Expand Up @@ -163,6 +170,7 @@ public function __construct($pTriggerUrl, $pControllerClass = null, $pParentCont

} else {
$this->setClient(new Client($this));
$this->parsePhpInput();
}

$this->setClass($pControllerClass);
Expand Down Expand Up @@ -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;
}

/**
*
Expand All @@ -226,6 +247,15 @@ public function getHttpStatusCodes()
{
return $this->withStatusCode;
}

/**
*
* @return integer
*/
public function getHttpStatusCode()
{
return $this->httpStatusCode;
}

/**
* Set the check access function/method.
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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);

Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -1207,5 +1256,29 @@ public function findRoute($pUri, $pMethod = '_all_')

return false;
}

protected function parsePhpInput()
{
$input = $this->getPhpInput();
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong indentation.

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');
}

}