コード例 #1
0
 /**
  * Adds cache headers to the response.
  *
  * Called via a signal triggered by the MVC Dispatcher
  *
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @param ControllerInterface $controller
  * @return void
  */
 public function addHeaders(RequestInterface $request, ResponseInterface $response, ControllerInterface $controller)
 {
     if (isset($this->settings['cacheHeaders']['disabled']) && $this->settings['cacheHeaders']['disabled'] === TRUE) {
         $this->logger->log(sprintf('Varnish cache headers disabled (see configuration setting MOC.Varnish.cacheHeaders.disabled)'), LOG_DEBUG);
         return;
     }
     if (!$response instanceof Response || !$controller instanceof NodeController) {
         return;
     }
     $arguments = $controller->getControllerContext()->getArguments();
     if (!$arguments->hasArgument('node')) {
         return;
     }
     $node = $arguments->getArgument('node')->getValue();
     if (!$node instanceof NodeInterface) {
         return;
     }
     if ($node->getContext()->getWorkspaceName() !== 'live') {
         return;
     }
     if ($node->hasProperty('disableVarnishCache') && $node->getProperty('disableVarnishCache') === TRUE) {
         $this->logger->log(sprintf('Varnish cache headers skipped due to property "disableVarnishCache" for node "%s" (%s)', $node->getLabel(), $node->getPath()), LOG_DEBUG);
         return;
     }
     if ($this->contentCacheAspect->isEvaluatedUncached()) {
         $this->logger->log(sprintf('Varnish cache disabled due to uncachable content for node "%s" (%s)', $node->getLabel(), $node->getPath()), LOG_DEBUG);
         $response->getHeaders()->setCacheControlDirective('no-cache');
     } else {
         list($tags, $cacheLifetime) = $this->getCacheTagsAndLifetime();
         if (count($tags) > 0) {
             $response->setHeader('X-Cache-Tags', implode(',', $tags));
         }
         $response->setHeader('X-Site', $this->tokenStorage->getToken());
         $nodeLifetime = $node->getProperty('cacheTimeToLive');
         if ($nodeLifetime === '' || $nodeLifetime === NULL) {
             $defaultLifetime = isset($this->settings['cacheHeaders']['defaultSharedMaximumAge']) ? $this->settings['cacheHeaders']['defaultSharedMaximumAge'] : NULL;
             $timeToLive = $defaultLifetime;
             if ($defaultLifetime === NULL) {
                 $timeToLive = $cacheLifetime;
             } elseif ($cacheLifetime !== NULL) {
                 $timeToLive = min($defaultLifetime, $cacheLifetime);
             }
         } else {
             $timeToLive = $nodeLifetime;
         }
         if ($timeToLive !== NULL) {
             $response->setSharedMaximumAge(intval($timeToLive));
             $this->logger->log(sprintf('Varnish cache enabled for node "%s" (%s) with max-age "%u"', $node->getLabel(), $node->getPath(), $timeToLive), LOG_DEBUG);
         } else {
             $this->logger->log(sprintf('Varnish cache headers not sent for node "%s" (%s) due to no max-age', $node->getLabel(), $node->getPath()), LOG_DEBUG);
         }
     }
 }
コード例 #2
0
 /**
  * Adds cache headers to the response.
  *
  * Called via a signal triggered by the MVC Dispatcher
  *
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @param ControllerInterface $controller
  * @return void
  */
 public function addHeaders(RequestInterface $request, ResponseInterface $response, ControllerInterface $controller)
 {
     if (!$response instanceof Response || !$controller instanceof NodeController) {
         return;
     }
     $arguments = $controller->getControllerContext()->getArguments();
     if (!$arguments->hasArgument('node')) {
         return;
     }
     $node = $arguments->getArgument('node')->getValue();
     if (!$node instanceof NodeInterface) {
         return;
     }
     if ($node->getContext()->getWorkspaceName() !== 'live') {
         return;
     }
     if ($node->hasProperty('disableVarnishCache') && $node->getProperty('disableVarnishCache') === TRUE) {
         return;
     }
     if ($this->contentCacheAspect->isEvaluatedUncached()) {
         $response->getHeaders()->setCacheControlDirective('no-cache');
     } else {
         list($tags, $cacheLifetime) = $this->getCacheTagsAndLifetime();
         if (count($tags) > 0) {
             $response->setHeader('X-Cache-Tags', implode(',', $tags));
         }
         $response->setHeader('X-Site', $this->tokenStorage->getToken());
         $nodeLifetime = $node->getProperty('cacheTimeToLive');
         if ($nodeLifetime === '' || $nodeLifetime === NULL) {
             $defaultLifetime = $this->settings['cacheHeaders']['defaultSharedMaximumAge'];
             $timeToLive = $defaultLifetime;
             if ($defaultLifetime === NULL) {
                 $timeToLive = $cacheLifetime;
             } elseif ($cacheLifetime !== NULL) {
                 $timeToLive = min($defaultLifetime, $cacheLifetime);
             }
         } else {
             $timeToLive = $nodeLifetime;
         }
         if ($timeToLive !== NULL) {
             $response->setSharedMaximumAge(intval($timeToLive));
         }
     }
 }