public function __invoke(Request $request, Response $response, Application $app) { if ($app->offsetExists("json-schema.describedBy") && !$response->isEmpty()) { if ($app["json-schema.correlationMechanism"] === "profile") { $contentType = $response->headers->get("Content-Type"); $response->headers->set("Content-Type", "{$contentType}; profile=\"{$app["json-schema.describedBy"]}\""); } elseif ($app["json-schema.correlationMechanism"] === "link") { $response->headers->set("Link", "<{$app["json-schema.describedBy"]}>; rel=\"describedBy\"", false); } else { $errorMessage = "json-schema.correlationMechanism must be either \"profile\" or \"link\""; throw new ServiceUnavailableHttpException(null, $errorMessage); } } }
public function testIsEmpty() { foreach (array(204, 304) as $code) { $response = new Response('', $code); $this->assertTrue($response->isEmpty()); } $response = new Response('', 200); $this->assertFalse($response->isEmpty()); }
/** * @param Request $request * @param Response $response * * @return bool|int Will return integer code if response cannot be cached or true if it's cacheable */ public function isCacheable(Request $request, Response $response) { if ($request->attributes->get('_supercache') === false) { return CacheManager::UNCACHEABLE_ROUTE; } if ($request->getMethod() !== 'GET') { return CacheManager::UNCACHEABLE_METHOD; } $queryString = $request->server->get('QUERY_STRING'); if (!empty($queryString)) { return CacheManager::UNCACHEABLE_QUERY; } //Response::isCacheable() is unusable here due to expiry & code settings if (!$response->isSuccessful() || $response->isEmpty()) { return CacheManager::UNCACHEABLE_CODE; } if ($response->headers->hasCacheControlDirective('no-store')) { return CacheManager::UNCACHEABLE_NO_STORE_POLICY; } if ($response->headers->hasCacheControlDirective('private')) { return CacheManager::UNCACHEABLE_PRIVATE; } $environment = $this->container->getParameter('kernel.environment'); if ($environment !== 'prod' && $environment !== 'dev' || !$this->container->getParameter('supercache.enable_' . $environment)) { return CacheManager::UNCACHEABLE_ENVIRONMENT; } return true; }
/** * @return bool */ protected function isEmptyResponse() { return !isset($this->response) || $this->response->isEmpty(); }