Skip to content
Open
Changes from all 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
19 changes: 14 additions & 5 deletions source/Jacwright/RestServer/RestServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public function options() {
public function handle() {
$this->url = $this->getPath();
$this->method = $this->getMethod();
$this->format = $this->getFormat();

if (($this->useCors) && ($this->method == 'OPTIONS')) {
$this->corsHeaders();
Expand All @@ -132,7 +131,9 @@ public function handle() {
$this->sendData($this->options());
}

list($obj, $method, $params, $this->params, $noAuth) = $this->findUrl();
list($obj, $method, $params, $this->params, $noAuth, $contentType) = $this->findUrl();

$this->format = $this->getFormat($contentType);

if ($obj) {
if (is_string($obj) && !($newObj = $this->instantiateClass($obj))) {
Expand Down Expand Up @@ -361,6 +362,9 @@ protected function generateMap($class, $basePath) {
foreach ($methods as $method) {
$doc = $method->getDocComment();
$noAuth = strpos($doc, '@noAuth') !== false;
if (preg_match_all('/@contentType[ \t]+\/?(\S*)/s', $doc, $matches, PREG_SET_ORDER)) {
$contentType = $matches[0][1];
}
if (preg_match_all('/@url[ \t]+(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)[ \t]+\/?(\S*)/s', $doc, $matches, PREG_SET_ORDER)) {
$params = $method->getParameters();

Expand All @@ -381,6 +385,7 @@ protected function generateMap($class, $basePath) {
$call[] = $args;
$call[] = null;
$call[] = $noAuth;
$call[] = $contentType ?? "";

$this->map[$httpMethod][$url] = $call;
}
Expand Down Expand Up @@ -432,7 +437,10 @@ public function getMethod() {
return $method;
}

public function getFormat() {
public function getFormat($contentType = "") {
if(!empty($contentType)){
return $contentType;
}
$format = RestFormat::PLAIN;
$accept_mod = null;

Expand Down Expand Up @@ -463,7 +471,7 @@ public function getFormat() {
$format = RestFormat::JSON;
}

return $format;
return $contentType ?? $format;
}

public function getData() {
Expand All @@ -489,9 +497,10 @@ public function sendData($data) {
foreach ($data->__keepOut() as $prop) {
unset($data->$prop);
}
$this->xml_encode($data);
}

$this->xml_encode($data);
echo $data;
} else {
if (is_object($data) && method_exists($data, '__keepOut')) {
$data = clone $data;
Expand Down