public function onKernelResponse(FilterResponseEvent $event)
    {
        $request = $event->getRequest();

        if (!$configuration = $request->attributes->get('_cache')) {
            return;
        }

        $response = $event->getResponse();

        // http://tools.ietf.org/html/draft-ietf-httpbis-p4-conditional-12#section-3.1
        if (!\in_array($response->getStatusCode(), [200, 203, 300, 301, 302, 304, 404, 410])) {
            return;
        }

        if (!$response->headers->hasCacheControlDirective('s-maxage') && null !== $age = $configuration->getSMaxAge()) {
            $age = $this->convertToSecondsIfNeeded($age);

            $response->setSharedMaxAge($age);
        }

        if ($configuration->mustRevalidate()) {
            $response->headers->addCacheControlDirective('must-revalidate');
        }

        if (!$response->headers->hasCacheControlDirective('max-age') && null !== $age = $configuration->getMaxAge()) {
            $age = $this->convertToSecondsIfNeeded($age);

            $response->setMaxAge($age);
        }

        if (!$response->headers->hasCacheControlDirective('max-stale') && null !== $stale = $configuration->getMaxStale()) {
            $stale = $this->convertToSecondsIfNeeded($stale);

            $response->headers->addCacheControlDirective('max-stale', $stale);
        }

        if (!$response->headers->has('Expires') && null !== $configuration->getExpires()) {
            $date = \DateTime::createFromFormat('U', strtotime($configuration->getExpires()), new \DateTimeZone('UTC'));
            $response->setExpires($date);
        }

        if (!$response->headers->has('Vary') && null !== $configuration->getVary()) {
            $response->setVary($configuration->getVary());
        }

        if ($configuration->isPublic()) {
            $response->setPublic();
        }

        if ($configuration->isPrivate()) {
            $response->setPrivate();
        }

        if (!$response->headers->has('Last-Modified') && isset($this->lastModifiedDates[$request])) {
            $response->setLastModified($this->lastModifiedDates[$request]);

            unset($this->lastModifiedDates[$request]);
        }

        if (!$response->headers->has('Etag') && isset($this->etags[$request])) {
            $response->setEtag($this->etags[$request]);

            unset($this->etags[$request]);
        }
    }
public function onKernelResponse(FilterResponseEvent $event)
    {
        $request = $event->getRequest();

        if (!$configuration = $request->attributes->get('_cache')) {
            return;
        }

        $response = $event->getResponse();

        // http://tools.ietf.org/html/draft-ietf-httpbis-p4-conditional-12#section-3.1
        if (!\in_array($response->getStatusCode(), [200, 203, 300, 301, 302, 304, 404, 410])) {
            return;
        }

        if (!$response->headers->hasCacheControlDirective('s-maxage') && null !== $age = $configuration->getSMaxAge()) {
            $age = $this->convertToSecondsIfNeeded($age);

            $response->setSharedMaxAge($age);
        }

        if ($configuration->mustRevalidate()) {
            $response->headers->addCacheControlDirective('must-revalidate');
        }

        if (!$response->headers->hasCacheControlDirective('max-age') && null !== $age = $configuration->getMaxAge()) {
            $age = $this->convertToSecondsIfNeeded($age);

            $response->setMaxAge($age);
        }

        if (!$response->headers->hasCacheControlDirective('max-stale') && null !== $stale = $configuration->getMaxStale()) {
            $stale = $this->convertToSecondsIfNeeded($stale);

            $response->headers->addCacheControlDirective('max-stale', $stale);
        }

        if (!$response->headers->has('Expires') && null !== $configuration->getExpires()) {
            $date = \DateTime::createFromFormat('U', strtotime($configuration->getExpires()), new \DateTimeZone('UTC'));
            $response->setExpires($date);
        }

        if (!$response->headers->has('Vary') && null !== $configuration->getVary()) {
            $response->setVary($configuration->getVary());
        }

        if ($configuration->isPublic()) {
            $response->setPublic();
        }

        if ($configuration->isPrivate()) {
            $response->setPrivate();
        }

        if (!$response->headers->has('Last-Modified') && isset($this->lastModifiedDates[$request])) {
            $response->setLastModified($this->lastModifiedDates[$request]);

            unset($this->lastModifiedDates[$request]);
        }

        if (!$response->headers->has('Etag') && isset($this->etags[$request])) {
            $response->setEtag($this->etags[$request]);

            unset($this->etags[$request]);
        }
    }
public function onKernelResponse ( FilterResponseEvent $ event ) { $ request = $ event -> getRequest ( ) ; if ( ! $ configuration = $ request -> attributes -> get ( '_cache' ) ) { return ; } $ response = $ event -> getResponse ( ) ; // http://tools.ietf.org/html/draft-ietf-httpbis-p4-conditional-12#section-3.1 if ( ! \ in_array ( $ response -> getStatusCode ( ) , [ 200 , 203 , 300 , 301 , 302 , 304 , 404 , 410 ] ) ) { return ; } if ( ! $ response -> headers -> hasCacheControlDirective ( 's-maxage' ) && null !== $ age = $ configuration -> getSMaxAge ( ) ) { $ age = $ this -> convertToSecondsIfNeeded ( $ age ) ; $ response -> setSharedMaxAge ( $ age ) ; } if ( $ configuration -> mustRevalidate ( ) ) { $ response -> headers -> addCacheControlDirective ( 'must-revalidate' ) ; } if ( ! $ response -> headers -> hasCacheControlDirective ( 'max-age' ) && null !== $ age = $ configuration -> getMaxAge ( ) ) { $ age = $ this -> convertToSecondsIfNeeded ( $ age ) ; $ response -> setMaxAge ( $ age ) ; } if ( ! $ response -> headers -> hasCacheControlDirective ( 'max-stale' ) && null !== $ stale = $ configuration -> getMaxStale ( ) ) { $ stale = $ this -> convertToSecondsIfNeeded ( $ stale ) ; $ response -> headers -> addCacheControlDirective ( 'max-stale' , $ stale ) ; } if ( ! $ response -> headers -> has ( 'Expires' ) && null !== $ configuration -> getExpires ( ) ) { $ date = \ DateTime :: createFromFormat ( 'U' , strtotime ( $ configuration -> getExpires ( ) ) , new \ DateTimeZone ( 'UTC' ) ) ; $ response -> setExpires ( $ date ) ; } if ( ! $ response -> headers -> has ( 'Vary' ) && null !== $ configuration -> getVary ( ) ) { $ response -> setVary ( $ configuration -> getVary ( ) ) ; } if ( $ configuration -> isPublic ( ) ) { $ response -> setPublic ( ) ; } if ( $ configuration -> isPrivate ( ) ) { $ response -> setPrivate ( ) ; } if ( ! $ response -> headers -> has ( 'Last-Modified' ) && isset ( $ this -> lastModifiedDates [ $ request ] ) ) { $ response -> setLastModified ( $ this -> lastModifiedDates [ $ request ] ) ; unset ( $ this -> lastModifiedDates [ $ request ] ) ; } if ( ! $ response -> headers -> has ( 'Etag' ) && isset ( $ this -> etags [ $ request ] ) ) { $ response -> setEtag ( $ this -> etags [ $ request ] ) ; unset ( $ this -> etags [ $ request ] ) ; } }
Modifies the response to apply HTTP cache headers when needed.
Modifies the response to apply HTTP cache headers when needed .