public function fromHTTPRequest(Request $request) { $body = $request->getBody(); $meta = array(); // override method $rqMethodHeader = 'X-Psc-Cms-Request-Method'; if (in_array($method = $request->getHeaderField($rqMethodHeader), array(Service::PUT, Service::DELETE))) { $this->log('Request-Method durch HTTP-Header ' . $rqMethodHeader . ' überschrieben zu: ' . $method); unset($body->{$rqMethodHeader}); } else { $method = constant('Psc\\Net\\Service::' . $request->getMethod()); // Request::GET => ServiceRequest::GET } // revision $revisionHeader = 'X-Psc-Cms-Revision'; if (($revision = $request->getHeaderField($revisionHeader)) != '') { $meta['revision'] = $revision; unset($body->{$revisionHeader}); } // convert bodyAsJSON to native if (is_object($body) && count($body) === 1 && isset($body->bodyAsJSON)) { $body = $this->jsonConverter->parse($body->bodyAsJSON); } // convert json request to native if (is_string($body) && $request->isContentType('application/json')) { $body = $this->jsonConverter->parse($body); } return new ServiceRequest($method, $request->getParts(), $body, $request->getQuery(), $request->getFiles(), $meta); }
public function testApi() { $request = new Request(Request::GET, '/episodes/8/status'); $this->assertEquals('episodes', $request->part(1)); $this->assertEquals('8', $request->part(2)); $this->assertEquals('status', $request->part(3)); $this->assertEquals(NULL, $request->part(4)); $this->assertEquals(Request::GET, $request->getMethod()); }
/** * @return Response */ public function handle(Request $request) { $this->log($request->debug()); $this->request = $request; try { $serviceRequest = $this->createServiceRequest($this->request); $serviceResponse = $this->route($serviceRequest); $this->response = $this->convertResponse($serviceResponse); } catch (HTTPResponseException $e) { $this->response = Response::create($e->getCode(), $e->getResponseBody(), $e->getHeaders()); } catch (HTTPException $e) { // die darstellung sollte eigentlich woanders sein, nicht hier // eigentlich müssten wir hier auch request->content-type auswerten und response umwandeln $this->response = Response::create($e->getCode(), sprintf("%s\n\n%s\n%s", $e->getResponseBody(), $request->getResource(), $this->getDebugInfo()), $e->getHeaders()); } catch (NoServiceFoundException $e) { $this->logError($e, $this->debugLevel, 1); $e->setCode(404); $this->response = Response::create(404, sprintf("Es wurde kein Service für: %s gefunden\n\n%s", $request->getResource(), $this->getDebugInfo())); } catch (\Psc\CMS\Service\ControllerRouteException $e) { $this->logError($e, $this->debugLevel, 5); // nicht 1 da das mit dem Klassennamen ja schon "interna" sind $e->setCode(404); $this->response = Response::create(404, sprintf($e->getMessage() . "\n\n%s", $this->getDebugInfo())); } catch (\Exception $e) { $this->logError($e, $this->debugLevel, 1); $this->response = Response::create(500, sprintf("Es ist ein Fehler aufgetreten. URL: %s%s\n\n%s", $request->getResource(), $this->debugLevel >= 5 ? "\nFehler: " . $e->getMessage() : NULL, $this->getDebugInfo()), $this->getErrorMessageHeaders($e)); } if (isset($e)) { // oder halt code >= 400 if (!$this->isIgnoredError($e)) { $contextInfo = 'im RequestHandler. ' . "\n"; $contextInfo .= ' ' . $request->getMethod() . ' /' . implode('/', $request->getParts()) . "\n"; $contextInfo .= ' Accept: ' . $request->getHeaderField('Accept') . "\n"; $contextInfo .= ' Referer: ' . $request->getReferer() . "\n"; $contextInfo .= ' User-Agent: ' . $request->getUserAgent(); if (isset($this->contextInfo)) { $contextInfo .= "\n" . $this->contextInfo; } $contextInfo .= "\n\n" . $this->dumpRequest($request); \Psc\PSC::getEnvironment()->getErrorHandler()->handleCaughtException($e, $contextInfo); } } return $this->response; }