getMaxAge() public method

First, it checks for a s-maxage directive, then a max-age directive, and then it falls back on an expires header. It returns null when no maximum age can be established.
public getMaxAge ( ) : integer | null
return integer | null Number of seconds
Exemplo n.º 1
0
 /**
  * @return int
  */
 protected function getMaxAge()
 {
     if ($this->response === null) {
         return null;
     }
     return $this->response->getMaxAge();
 }
 /**
  * {@inheritdoc}
  */
 public function update(Response $response)
 {
     // if we have no embedded Response, do nothing
     if (0 === $this->embeddedResponses) {
         return;
     }
     // Remove validation related headers in order to avoid browsers using
     // their own cache, because some of the response content comes from
     // at least one embedded response (which likely has a different caching strategy).
     if ($response->isValidateable()) {
         $response->setEtag(null);
         $response->setLastModified(null);
         $this->cacheable = false;
     }
     if (!$this->cacheable) {
         $response->headers->set('Cache-Control', 'no-cache, must-revalidate');
         return;
     }
     $this->ttls[] = $response->getTtl();
     $this->maxAges[] = $response->getMaxAge();
     if (null !== ($maxAge = min($this->maxAges))) {
         $response->setSharedMaxAge($maxAge);
         $response->headers->set('Age', $maxAge - min($this->ttls));
     }
     $response->setMaxAge(0);
 }
 /**
  * Adds a Response.
  *
  * @param Response $response
  */
 public function add(Response $response)
 {
     if ($response->isValidateable()) {
         $this->cacheable = false;
     } else {
         $this->ttls[] = $response->getTtl();
         $this->maxAges[] = $response->getMaxAge();
     }
 }
Exemplo n.º 4
0
 public function testGetMaxAge()
 {
     $response = new Response();
     $response->headers->set('Cache-Control', 's-maxage=600, max-age=0');
     $this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() uses s-maxage cache control directive when present');
     $response = new Response();
     $response->headers->set('Cache-Control', 'max-age=600');
     $this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() falls back to max-age when no s-maxage directive present');
     $response = new Response();
     $response->headers->set('Cache-Control', 'must-revalidate');
     $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
     $this->assertEquals(3600, $response->getMaxAge(), '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present');
     $response = new Response();
     $this->assertNull($response->getMaxAge(), '->getMaxAge() returns null if no freshness information available');
 }
 protected function logResponse(Response $response, Request $request)
 {
     if ($response->getStatusCode() >= 500) {
         $color = LogLevel::ERROR;
     } elseif ($response->getStatusCode() >= 400) {
         $color = LogLevel::WARNING;
     } elseif ($response->getStatusCode() >= 300) {
         $color = LogLevel::NOTICE;
     } elseif ($response->getStatusCode() >= 200) {
         $color = LogLevel::INFO;
     } else {
         $color = LogLevel::INFO;
     }
     $msg = 'Response {response_status_code} for "{request_method} {request_uri}"';
     $context = array('request_method' => $request->getMethod(), 'request_uri' => $request->getRequestUri(), 'response_status_code' => $response->getStatusCode(), 'response_charset' => $response->getCharset(), 'response_date' => $response->getDate(), 'response_etag' => $response->getEtag(), 'response_expires' => $response->getExpires(), 'response_last_modified' => $response->getLastModified(), 'response_max_age' => $response->getMaxAge(), 'response_protocol_version' => $response->getProtocolVersion(), 'response_ttl' => $response->getTtl(), 'response_vary' => $response->getVary());
     $this->logger->log($color, $msg, $context);
 }
 /**
  * Send a media stored via the UploadedFileManager
  *
  * @Config\Route("/{key}", name="open_orchestra_media_get")
  * @Config\Method({"GET"})
  *
  * @return Response
  */
 public function getAction($key)
 {
     $mediaStorageManager = $this->get('open_orchestra_media_file.manager.storage');
     $fileContent = $mediaStorageManager->getFileContent($key);
     $finfo = finfo_open(FILEINFO_MIME);
     $mimetype = finfo_buffer($finfo, $fileContent);
     finfo_close($finfo);
     $response = new Response();
     $response->headers->set('Content-Type', $mimetype);
     $response->headers->set('Content-Length', strlen($fileContent));
     $response->setContent($fileContent);
     $response->setPublic();
     $response->setMaxAge(2629743);
     $date = new \DateTime();
     $date->modify('+' . $response->getMaxAge() . ' seconds');
     $response->setExpires($date);
     return $response;
 }
Exemplo n.º 7
0
 public function testSetCache()
 {
     $response = new Response();
     //array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public')
     try {
         $response->setCache(array("wrong option" => "value"));
         $this->fail('->setCache() throws an InvalidArgumentException if an option is not supported');
     } catch (\Exception $e) {
         $this->assertInstanceOf('InvalidArgumentException', $e, '->setCache() throws an InvalidArgumentException if an option is not supported');
         $this->assertContains('"wrong option"', $e->getMessage());
     }
     $options = array('etag' => '"whatever"');
     $response->setCache($options);
     $this->assertEquals($response->getEtag(), '"whatever"');
     $now = new \DateTime();
     $options = array('last_modified' => $now);
     $response->setCache($options);
     $this->assertEquals($response->getLastModified()->getTimestamp(), $now->getTimestamp());
     $options = array('max_age' => 100);
     $response->setCache($options);
     $this->assertEquals($response->getMaxAge(), 100);
     $options = array('s_maxage' => 200);
     $response->setCache($options);
     $this->assertEquals($response->getMaxAge(), 200);
     $this->assertTrue($response->headers->hasCacheControlDirective('public'));
     $this->assertFalse($response->headers->hasCacheControlDirective('private'));
     $response->setCache(array('public' => true));
     $this->assertTrue($response->headers->hasCacheControlDirective('public'));
     $this->assertFalse($response->headers->hasCacheControlDirective('private'));
     $response->setCache(array('public' => false));
     $this->assertFalse($response->headers->hasCacheControlDirective('public'));
     $this->assertTrue($response->headers->hasCacheControlDirective('private'));
     $response->setCache(array('private' => true));
     $this->assertFalse($response->headers->hasCacheControlDirective('public'));
     $this->assertTrue($response->headers->hasCacheControlDirective('private'));
     $response->setCache(array('private' => false));
     $this->assertTrue($response->headers->hasCacheControlDirective('public'));
     $this->assertFalse($response->headers->hasCacheControlDirective('private'));
 }
Exemplo n.º 8
0
 /**
  * @param Response $response
  * @param Request  $request
  *
  * @return array
  */
 protected function createContext(Response $response, Request $request)
 {
     $context = array('response_status_code' => $response->getStatusCode(), 'response_charset' => $response->getCharset(), 'response_date' => $response->getDate(), 'response_etag' => $response->getEtag(), 'response_expires' => $response->getExpires(), 'response_last_modified' => $response->getLastModified(), 'response_max_age' => $response->getMaxAge(), 'response_protocol_version' => $response->getProtocolVersion(), 'response_ttl' => $response->getTtl(), 'response_vary' => $response->getVary(), 'request_method' => $request->getMethod(), 'request_uri' => $request->getRequestUri(), 'request_route' => $request->attributes->get('_route'), 'response_time' => $this->getTime($request), 'response_memory' => $this->getMemory());
     return $context;
 }