From 52ec9908bbf61d08b9036ff8a4c39d315c495172 Mon Sep 17 00:00:00 2001 From: Johannes Rudolph Date: Sat, 8 Sep 2018 13:14:52 +0200 Subject: [PATCH 1/4] Add ContentType in generateMap --- source/Jacwright/RestServer/RestServer.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/Jacwright/RestServer/RestServer.php b/source/Jacwright/RestServer/RestServer.php index 9e84b47..434a62b 100755 --- a/source/Jacwright/RestServer/RestServer.php +++ b/source/Jacwright/RestServer/RestServer.php @@ -361,6 +361,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(); @@ -381,6 +384,7 @@ protected function generateMap($class, $basePath) { $call[] = $args; $call[] = null; $call[] = $noAuth; + $call[] = $contentType ?? ""; $this->map[$httpMethod][$url] = $call; } From cb22e615cb7f552f9693afec35f2f4a5beb124a2 Mon Sep 17 00:00:00 2001 From: Johannes Rudolph Date: Sat, 8 Sep 2018 13:18:57 +0200 Subject: [PATCH 2/4] Add forceContentType to getFormat --- source/Jacwright/RestServer/RestServer.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/source/Jacwright/RestServer/RestServer.php b/source/Jacwright/RestServer/RestServer.php index 434a62b..f260a5b 100755 --- a/source/Jacwright/RestServer/RestServer.php +++ b/source/Jacwright/RestServer/RestServer.php @@ -436,7 +436,10 @@ public function getMethod() { return $method; } - public function getFormat() { + public function getFormat($contentType = "") { + if(!empty($contentType)){ + return $contentType; + } $format = RestFormat::PLAIN; $accept_mod = null; @@ -467,7 +470,7 @@ public function getFormat() { $format = RestFormat::JSON; } - return $format; + return $contentType ?? $format; } public function getData() { From 14137963ff6e40a8a2719686cae4425fea0e3bf3 Mon Sep 17 00:00:00 2001 From: Johannes Rudolph Date: Sat, 8 Sep 2018 13:19:40 +0200 Subject: [PATCH 3/4] Add ContentType to getFormat --- source/Jacwright/RestServer/RestServer.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/Jacwright/RestServer/RestServer.php b/source/Jacwright/RestServer/RestServer.php index f260a5b..49495e0 100755 --- a/source/Jacwright/RestServer/RestServer.php +++ b/source/Jacwright/RestServer/RestServer.php @@ -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(); @@ -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))) { From 7b14b2fc44a74c5e408f0057dc1dd65e0bc3f6fd Mon Sep 17 00:00:00 2001 From: Johannes Rudolph Date: Sat, 8 Sep 2018 13:20:53 +0200 Subject: [PATCH 4/4] If XML and $data is not an object print it directly --- source/Jacwright/RestServer/RestServer.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/Jacwright/RestServer/RestServer.php b/source/Jacwright/RestServer/RestServer.php index 49495e0..e3c100d 100755 --- a/source/Jacwright/RestServer/RestServer.php +++ b/source/Jacwright/RestServer/RestServer.php @@ -497,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;