protected function convertResponse(ServiceResponse $response, $format, $mimeContentType, $request) { // @TODO Event Triggern? $body = $response->getBody(); $isJsonRequest = mb_strpos($request->getHeader()->getField('Accept'), 'application/json') === 0; /* Sonderbehandlung JSON uploads */ if ($format === ServiceResponse::JSON_UPLOAD_RESPONSE) { if (!$isJsonRequest) { // firefox und alle xhr browser bekommen den richtigen ContentType // ie und so bekommen plain: $this->headers['Content-Type'] = 'text/plain'; $this->headers['Content-Disposition'] = 'inline; filename="files.json"'; $this->headers['X-Content-Type-Options'] = 'nosniff'; $this->headers['Vary'] = 'Accept'; } $format = ServiceResponse::JSON; // weiter } /* ReponseOutputting - Body */ if ($body instanceof ResponseOutputting) { return $this->createOutputtingResponse($body, $response, $request); } /* JSON */ if ($format === ServiceResponse::JSON) { if (($json = $this->createJSONResponse($body)) != NULL) { return $json; } /* ICAL */ } elseif ($format === ServiceResponse::ICAL) { return $this->createResponse($body); /* HTML */ } elseif ($format === ServiceResponse::HTML) { if (is_string($body)) { return $this->createResponse($body); } elseif ($body instanceof \Psc\HTML\HTMLInterface) { return $this->createResponse($body->html()); } /* plain text */ } elseif ($format === ServiceResponse::TEXT) { $this->headers['Content-Type'] = $this->getContentType($format) . '; charset=utf-8'; return $this->createResponse($body); /* XLSX */ } elseif ($format === ServiceResponse::XLSX) { $excel = $body; $output = function () use($excel) { $writer = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007'); $writer->save('php://output'); }; $this->headers['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; $this->headers['Content-Disposition'] = 'attachment;filename="' . $excel->getProperties()->getCustomPropertyValue('filename') . '.xlsx"'; $this->headers['Cache-Control'] = 'max-age=0'; $response = Response::create(200, NULL, $this->headers); $response->setOutputClosure($output); return $response; } elseif ($format === ServiceResponse::SOME_FILE) { if ($body instanceof \Psc\CMS\UploadedFile) { $uplFile = $body; if ($isJsonRequest) { return $this->createJSONResponse($uplFile); } else { // sende einen application download krams $this->headers['Content-Type'] = 'application/octet-stream'; $this->headers['Content-Disposition'] = 'attachment;filename="' . $uplFile->getDownloadFilename() . '"'; $this->headers['Content-Transfer-Encoding'] = 'binary'; $this->headers['Pragma'] = 'public'; $this->headers['Content-Length'] = $uplFile->getFile()->getSize(); $output = function () use($uplFile) { readfile((string) $uplFile->getFile()); }; $response = Response::create(200, NULL, $this->headers); $response->setOutputClosure($output); return $response; } } } throw new ResponseConverterException(sprintf("Der Inhalt der Service-Response konnte nicht in eine HTTP-Response umgewandelt werden. Der Body der ServiceResponse ist: %s. Format wurde ermittelt als: '%s' ('%s').", Code::varInfo($body), $format, $mimeContentType)); }