logger = $logger; $this->logger->debug(sprintf('Entered Controller in %sms', number_format((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000, 2))); } public function getService(): AbstractService { return $this->service; } public function setService(AbstractService $service): self { $this->service = $service; return $this; } public function isApiExplorerEnabled(): bool { return $this->apiExplorerEnabled; } public function setApiExplorerEnabled(bool $apiExplorerEnabled): self { $this->apiExplorerEnabled = $apiExplorerEnabled; return $this; } public function xmlResponse(\SimpleXMLElement $root, Request $request, Response $response): Response { $response->getBody()->write($root->asXML()); return $response->withHeader('Content-type', 'text/xml'); } public function jsonResponse($json, Request $request, Response $response): Response { $content = json_encode($json, JSON_PRETTY_PRINT); $response->getBody()->write($content); return $response->withHeader('Content-type', 'application/json'); } public function jsonResponseException(\Exception $e, Request $request, Response $response): Response { return $this->jsonResponse( [ 'Status' => 'Fail', 'Reason' => $e->getMessage(), ], $request, $response ); } /** * Decide if a request has a filter attached to it. * * @throws FilterDecodeException */ protected function requestHasFilters(Request $request, Response $response): bool { if ($request->hasHeader('Filter')) { $filterText = trim($request->getHeader('Filter')[0]); if (!empty($filterText)) { $decode = json_decode($filterText); if (null !== $decode) { return true; } throw new FilterDecodeException('Could not decode given Filter. Reason: Not JSON. Given: "'.$filterText.'"'); } } return false; } /** * Parse filters header into filter objects. */ protected function parseFilters(Request $request, Response $response): Filter { $filter = new Filter(); $filter->parseFromHeader(json_decode($request->getHeader('Filter')[0], true)); return $filter; } protected function pageNotFound(): Response { return (new Response()) ->withStatus(404) ; } protected function returnFile(Filesystem $filesystem, string $filename): Response { $response = new Response(); if (!$filesystem->has($filename)) { !\Kint::dump($filesystem->listContents(), $filesystem->has($filename)); exit; return $this->pageNotFound(); } // Crappy detection of content-type $path = $filesystem->getMetadata($filename)['path']; if (str_ends_with($path, '.css')) { $response = $response->withHeader('Content-Type', 'text/css'); } elseif (str_ends_with($path, '.js')) { $response = $response->withHeader('Content-Type', 'text/javascript'); } elseif (str_ends_with($path, '.png')) { $response = $response->withHeader('Content-Type', 'image/png'); } elseif (str_ends_with($path, '.jpg')) { $response = $response->withHeader('Content-Type', 'image/jpeg'); } elseif (str_ends_with($path, '.svg')) { $response = $response->withHeader('Content-Type', 'image/svg+xml'); } $response->getBody() ->write($filesystem->read($filename)) ; return $response; } }