Exemplo n.º 1
0
 public function testCacheControlToStringReturnsHeaderFormattedString()
 {
     $this->markTestIncomplete('CacheControl needs to be completed');
     $cacheControlHeader = new CacheControl();
     // @todo set some values, then test output
     $this->assertEmpty('Cache-Control: xxx', $cacheControlHeader->toString());
 }
Exemplo n.º 2
0
 public function testCacheControlParse()
 {
     $cacheControlHeader = CacheControl::fromString('Cache-Control: a, b=foo, c="bar, baz"');
     $this->assertTrue($cacheControlHeader->hasDirective('a'));
     $this->assertTrue($cacheControlHeader->getDirective('a'));
     $this->assertTrue($cacheControlHeader->hasDirective('b'));
     $this->assertEquals('foo', $cacheControlHeader->getDirective('b'));
     $this->assertTrue($cacheControlHeader->hasDirective('c'));
     $this->assertEquals('bar, baz', $cacheControlHeader->getDirective('c'));
 }
 /**
  * @dataProvider processNotSaveCacheProvider
  * @param string $cacheControlHeader
  * @param int $httpCode
  * @param bool $isGet
  * @param bool $overrideHeaders
  */
 public function testProcessNotSaveCache($cacheControlHeader, $httpCode, $isGet, $overrideHeaders)
 {
     $header = \Zend\Http\Header\CacheControl::fromString("Cache-Control: {$cacheControlHeader}");
     $this->responseMock->expects($this->once())->method('getHeader')->with('Cache-Control')->will($this->returnValue($header));
     $this->responseMock->expects($this->any())->method('getHttpResponseCode')->will($this->returnValue($httpCode));
     $this->requestMock->expects($this->any())->method('isGet')->will($this->returnValue($isGet));
     if ($overrideHeaders) {
         $this->responseMock->expects($this->once())->method('setNoCacheHeaders');
     }
     $this->fullPageCacheMock->expects($this->never())->method('save');
     $this->kernel->process($this->responseMock);
 }
Exemplo n.º 4
0
 /**
  * @param  Headers $headers
  * @return self
  */
 public function setCacheControl(Headers $headers)
 {
     if (!empty($this->cacheConfig['cache-control']['value']) && (!empty($this->cacheConfig['cache-control']['override']) || !$headers->has('cache-control'))) {
         $cacheControl = Header\CacheControl::fromString("Cache-Control: {$this->cacheConfig['cache-control']['value']}");
         $headers->addHeader($cacheControl);
     }
     return $this;
 }
 protected function completeGet(array $document, ClassMetadata $metadata)
 {
     $serializer = $this->options->getSerializer();
     if (isset($metadata->rest['cache'])) {
         $cacheControl = new CacheControl();
         foreach ($metadata->rest['cache'] as $key => $value) {
             $cacheControl->addDirective($key, $value);
         }
         $this->response->getHeaders()->addHeader($cacheControl);
     }
     if (isset($metadata->stamp['updatedOn'])) {
         $lastModified = new LastModified();
         $sec = $document[$metadata->stamp['updatedOn']]->sec;
         $lastModified->setDate(new \DateTime("@{$sec}"));
         $this->response->getHeaders()->addHeader($lastModified);
     }
     return $serializer->applySerializeMetadataToArray($document, $metadata->name);
 }
 /**
  *
  * @param MvcEvent $e
  */
 public function injectCacheHeader(MvcEvent $e)
 {
     $cacheEvent = clone $e;
     $cacheEvent->setTarget($this);
     $cacheEvent->setName(self::EVENT_DETERMINE_TTL);
     if ($this->varnishOptions->isCacheEnabled()) {
         $result = $this->getEventManager()->trigger($cacheEvent, function ($result) {
             return $result instanceof CachingStrategyInterface;
         });
         /** @var CachingStrategyInterface $strategy */
         $strategy = $result->last();
         $ttl = $strategy->getTtl();
     } else {
         $ttl = 0;
     }
     $headers = $e->getResponse()->getHeaders();
     if ($this->varnishOptions->getDebug()) {
         $debugValue = isset($strategy) ? get_class($strategy) : 'caching disabled';
         $debug = new GenericHeader(self::HEADER_CACHE_DEBUG, $debugValue);
         $headers->addHeader($debug);
     }
     $cacheControl = new CacheControl();
     $directives = ['no-store' => true, 'no-cache' => true, 'must-revalidate' => true, 'post-check' => 0, 'pre-check' => 0, 's-maxage' => $ttl];
     foreach ($directives as $directive => $value) {
         $cacheControl->addDirective($directive, $value);
     }
     $headers->addHeader($cacheControl);
     $this->setTtl($ttl);
 }
 public function getCacheControl()
 {
     if (!$this->cacheControl instanceof CacheControl) {
         $cacheControl = new CacheControl();
         if (isset($this->cacheControl['public'])) {
             $cacheControl->addDirective('public', true);
         }
         if (isset($this->cacheControl['private'])) {
             $cacheControl->addDirective('private', true);
         }
         if (isset($this->cacheControl['no_cache'])) {
             $cacheControl->addDirective('no-cache', true);
         }
         if (isset($this->cacheControl['max_age'])) {
             $cacheControl->addDirective('max-age', $this->cacheControl['max_age']);
         }
         $this->cacheControl = $cacheControl;
     }
     return $this->cacheControl;
 }
 /**
  * Cache Response for future requests
  *
  * @param MvcEvent $e
  * @return \Zend\Stdlib\ResponseInterface
  */
 public function onFinish(MvcEvent $e)
 {
     $request = $e->getRequest();
     if (!$request instanceof HttpRequest) {
         return;
     }
     if (!$request->isGet()) {
         return;
     }
     $response = $e->getResponse();
     if ($response instanceof HttpResponse && !$response->isOk()) {
         return;
     }
     // Do not continue if weren't able to compose a key
     if (empty($this->cache_key)) {
         return;
     }
     if (!$this->cacheAdapter->hasItem($this->cache_key)) {
         $resourceIdentifier = $e->getRouteMatch()->getParam('resource');
         $resource = call_user_func($this->getResourceLocatorService(), $resourceIdentifier);
         if (!$resource instanceof Resource || !$resource->isCacheable()) {
             return;
         }
         // Generate Response cache headers based on Resource CacheOptions
         $cacheOptions = $resource->getCacheOptions();
         $cacheControl = new CacheControl();
         $cacheControl->addDirective($cacheOptions->getAccess());
         $cacheControl->addDirective('max-age', $cacheOptions->getMaxAge());
         $cacheControl->addDirective('expires', $cacheOptions->getExpires());
         $cacheControl->addDirective('must-revalidate');
         $dateTime = new \DateTime();
         $dateTime->modify('+ ' . $cacheOptions->getExpires() . 'seconds');
         $expires = new Expires();
         $expires->setDate($dateTime);
         $lastModified = new LastModified();
         $lastModified->setDate(new \DateTime());
         // Add Headers to Response Header
         $response->getHeaders()->addHeader($cacheControl);
         $response->getHeaders()->addHeader($expires);
         $response->getHeaders()->addHeaderLine('Pragma: ' . $cacheOptions->getAccess());
         $response->getHeaders()->addHeader(Etag::fromString('Etag: ' . md5($response->getBody())));
         $response->getHeaders()->addHeader($lastModified);
         // Set cache adapter's TTL using Resource cache expires value
         $this->cacheAdapter->getOptions()->setTtl($cacheOptions->getExpires());
         $this->cacheAdapter->setItem($this->cache_key, $response);
         //return $response;
     }
 }