/**
  * Advice for a disabled content cache (e.g. because an exception was handled)
  *
  * @Flow\AfterReturning("method(TYPO3\TypoScript\Core\Cache\RuntimeContentCache->setEnableContentCache())")
  * @param JoinPointInterface $joinPoint
  */
 public function registerDisableContentCache(JoinPointInterface $joinPoint)
 {
     $enableContentCache = $joinPoint->getMethodArgument('enableContentCache');
     if ($enableContentCache !== TRUE) {
         $this->logger->log('Varnish cache disabled due content cache being disabled (e.g. because an exception was handled)', LOG_DEBUG);
         $this->evaluatedUncached = TRUE;
     }
 }
 /**
  * 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);
         }
     }
 }
 /**
  * @return void
  */
 protected function execute()
 {
     try {
         $this->cacheInvalidator->flush();
     } catch (ExceptionCollection $exceptions) {
         foreach ($exceptions as $exception) {
             if ($exception instanceof ProxyResponseException) {
                 $this->logger->log(sprintf('Error calling Varnish with BAN request (cannot connect to the caching proxy). Error %s', $exception->getMessage()), LOG_ERR);
             } elseif ($exception instanceof ProxyUnreachableException) {
                 $this->logger->log(sprintf('Error calling Varnish with BAN request (caching proxy returned an error response). Error %s', $exception->getMessage()), LOG_ERR);
             } else {
                 $this->logger->log(sprintf('Error calling Varnish with BAN request. Error %s', $exception->getMessage()), LOG_ERR);
             }
         }
     }
 }
 /**
  * Extract metadata from the content and store it
  *
  * @param string $entryIdentifier The entry identifier
  * @param string $content The raw content including serialized metadata
  * @return string The content without metadata
  * @throws InvalidDataException
  */
 protected function extractMetadata($entryIdentifier, $content)
 {
     $separatorIndex = strpos($content, self::SEPARATOR);
     if ($separatorIndex === FALSE) {
         $exception = new InvalidDataException('Could not find cache metadata in entry with identifier ' . $entryIdentifier, 1433155925);
         if ($this->environment->getContext()->isProduction()) {
             $this->logger->logException($exception);
         } else {
             throw $exception;
         }
     }
     $metadataJson = substr($content, 0, $separatorIndex);
     $metadata = json_decode($metadataJson, TRUE);
     if ($metadata === NULL) {
         $exception = new InvalidDataException('Invalid cache metadata in entry with identifier ' . $entryIdentifier, 1433155926);
         if ($this->environment->getContext()->isProduction()) {
             $this->logger->logException($exception);
         } else {
             throw $exception;
         }
     }
     $this->metadata[$entryIdentifier] = $metadata;
     return substr($content, $separatorIndex + 1);
 }