diff --git a/src/Symfony/Component/HttpKernel/HttpCache/ResponseCacheStrategy.php b/src/Symfony/Component/HttpKernel/HttpCache/ResponseCacheStrategy.php index b031199ed189c..f598f1c500736 100644 --- a/src/Symfony/Component/HttpKernel/HttpCache/ResponseCacheStrategy.php +++ b/src/Symfony/Component/HttpKernel/HttpCache/ResponseCacheStrategy.php @@ -27,17 +27,17 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface /** * Cache-Control headers that are sent to the final response if they appear in ANY of the responses. */ - private static $overrideDirectives = array('private', 'no-cache', 'no-store', 'no-transform', 'must-revalidate', 'proxy-revalidate'); + private static $overrideDirectives = ['private', 'no-cache', 'no-store', 'no-transform', 'must-revalidate', 'proxy-revalidate']; /** * Cache-Control headers that are sent to the final response if they appear in ALL of the responses. */ - private static $inheritDirectives = array('public', 'immutable'); + private static $inheritDirectives = ['public', 'immutable']; private $embeddedResponses = 0; private $isNotCacheableResponseEmbedded = false; private $age = 0; - private $flagDirectives = array( + private $flagDirectives = [ 'no-cache' => null, 'no-store' => null, 'no-transform' => null, @@ -46,12 +46,12 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface 'public' => null, 'private' => null, 'immutable' => null, - ); - private $ageDirectives = array( + ]; + private $ageDirectives = [ 'max-age' => null, 's-maxage' => null, 'expires' => null, - ); + ]; /** * {@inheritdoc} @@ -147,7 +147,7 @@ public function update(Response $response) if (\is_numeric($this->ageDirectives['expires'])) { $date = clone $response->getDate(); - $date->modify('+'.($this->ageDirectives['expires']+$this->age).' seconds'); + $date->modify('+'.($this->ageDirectives['expires'] + $this->age).' seconds'); $response->setExpires($date); } } @@ -171,7 +171,7 @@ private function willMakeFinalResponseUncacheable(Response $response) // Last-Modified and Etag headers cannot be merged, they render the response uncacheable // by default (except if the response also has max-age etc.). - if (\in_array($response->getStatusCode(), array(200, 203, 300, 301, 410)) + if (\in_array($response->getStatusCode(), [200, 203, 300, 301, 410]) && null === $response->getLastModified() && null === $response->getEtag() ) { @@ -181,7 +181,7 @@ private function willMakeFinalResponseUncacheable(Response $response) // RFC2616: A response received with any other status code (e.g. status codes 302 and 307) // MUST NOT be returned in a reply to a subsequent request unless there are // cache-control directives or another header(s) that explicitly allow it. - $cacheControl = array('max-age', 's-maxage', 'must-revalidate', 'proxy-revalidate', 'public', 'private'); + $cacheControl = ['max-age', 's-maxage', 'must-revalidate', 'proxy-revalidate', 'public', 'private']; foreach ($cacheControl as $key) { if ($response->headers->hasCacheControlDirective($key)) { return false; diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/ResponseCacheStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/ResponseCacheStrategyTest.php index 960b5cdcc65f9..f5593a625e563 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/ResponseCacheStrategyTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/ResponseCacheStrategyTest.php @@ -289,9 +289,9 @@ public function testCacheControlMerging(array $expects, array $master, array $su $cacheStrategy->update($response); foreach ($expects as $key => $value) { - if ($key === 'expires') { + if ('expires' === $key) { $this->assertSame($value, $response->getExpires()->format('U') - $response->getDate()->format('U')); - } else if ($key === 'age') { + } elseif ('age' === $key) { $this->assertSame($value, $response->getAge()); } elseif (true === $value) { $this->assertTrue($response->headers->hasCacheControlDirective($key), sprintf('Cache-Control header must have "%s" flag', $key)); @@ -308,160 +308,160 @@ public function testCacheControlMerging(array $expects, array $master, array $su public function cacheControlMergingProvider() { - yield 'result is public if all responses are public' => array( - array('private' => false, 'public' => true), - array('public' => true), - array( - array('public' => true), - ), - ); - - yield 'result is private by default' => array( - array('private' => true, 'public' => false), - array('public' => true), - array( - array(), - ), - ); - - yield 'combines public and private responses' => array( - array('must-revalidate' => false, 'private' => true, 'public' => false), - array('public' => true), - array( - array('private' => true), - ), - ); - - yield 'inherits no-cache from surrogates' => array( - array('no-cache' => true, 'public' => false), - array('public' => true), - array( - array('no-cache' => true), - ), - ); - - yield 'inherits no-store from surrogate' => array( - array('no-store' => true, 'public' => false), - array('public' => true), - array( - array('no-store' => true), - ), - ); - - yield 'resolve to lowest possible max-age' => array( - array('public' => false, 'private' => true, 's-maxage' => false, 'max-age' => '60'), - array('public' => true, 'max-age' => 3600), - array( - array('private' => true, 'max-age' => 60), - ), - ); - - yield 'resolves multiple max-age' => array( - array('public' => false, 'private' => true, 's-maxage' => false, 'max-age' => '60'), - array('private' => true, 'max-age' => 100), - array( - array('private' => true, 'max-age' => 3600), - array('public' => true, 'max-age' => 60, 's-maxage' => 60), - array('private' => true, 'max-age' => 60), - ), - ); - - yield 'merge max-age and s-maxage' => array( - array('public' => true, 's-maxage' => '60', 'max-age' => null), - array('public' => true, 's-maxage' => 3600), - array( - array('public' => true, 'max-age' => 60), - ), - ); - - yield 'result is private when combining private responses' => array( - array('no-cache' => false, 'must-revalidate' => false, 'private' => true), - array('s-maxage' => 60, 'private' => true), - array( - array('s-maxage' => 60, 'private' => true), - ), - ); - - yield 'result can have s-maxage and max-age' => array( - array('public' => true, 'private' => false, 's-maxage' => '60', 'max-age' => '30'), - array('s-maxage' => 100, 'max-age' => 2000), - array( - array('s-maxage' => 1000, 'max-age' => 30), - array('s-maxage' => 500, 'max-age' => 500), - array('s-maxage' => 60, 'max-age' => 1000), - ), - ); - - yield 'does not set headers without value' => array( - array('max-age' => null, 's-maxage' => null, 'public' => null), - array('private' => true), - array( - array('private' => true), - ), - ); - - yield 'max-age 0 is sent to the client' => array( - array('private' => true, 'max-age' => '0'), - array('max-age' => 0, 'private' => true), - array( - array('max-age' => 60, 'private' => true), - ), - ); - - yield 'max-age is relative to age' => array( - array('max-age' => '240', 'age' => 60), - array('max-age' => 180), - array( - array('max-age' => 600, 'age' => 60), - ), - ); - - yield 'retains lowest age of all responses' => array( - array('max-age' => '160', 'age' => 60), - array('max-age' => 600, 'age' => 60), - array( - array('max-age' => 120, 'age' => 20), - ), - ); - - yield 'max-age can be less than age, essentially expiring the response' => array( - array('age' => 120, 'max-age' => '90'), - array('max-age' => 90, 'age' => 120), - array( - array('max-age' => 120, 'age' => 60), - ), - ); - - yield 'max-age is 0 regardless of age' => array( - array('max-age' => '0'), - array('max-age' => 60), - array( - array('max-age' => 0, 'age' => 60), - ), - ); - - yield 'max-age is not negative' => array( - array('max-age' => '0'), - array('max-age' => 0), - array( - array('max-age' => 0, 'age' => 60), - ), - ); - - yield 'calculates lowest Expires header' => array( - array('expires' => 60), - array('expires' => 60), - array( - array('expires' => 120), - ), - ); - - yield 'calculates Expires header relative to age' => array( - array('expires' => 210, 'age' => 120), - array('expires' => 90), - array( - array('expires' => 600, 'age' => '120'), - ), - ); + yield 'result is public if all responses are public' => [ + ['private' => false, 'public' => true], + ['public' => true], + [ + ['public' => true], + ], + ]; + + yield 'result is private by default' => [ + ['private' => true, 'public' => false], + ['public' => true], + [ + [], + ], + ]; + + yield 'combines public and private responses' => [ + ['must-revalidate' => false, 'private' => true, 'public' => false], + ['public' => true], + [ + ['private' => true], + ], + ]; + + yield 'inherits no-cache from surrogates' => [ + ['no-cache' => true, 'public' => false], + ['public' => true], + [ + ['no-cache' => true], + ], + ]; + + yield 'inherits no-store from surrogate' => [ + ['no-store' => true, 'public' => false], + ['public' => true], + [ + ['no-store' => true], + ], + ]; + + yield 'resolve to lowest possible max-age' => [ + ['public' => false, 'private' => true, 's-maxage' => false, 'max-age' => '60'], + ['public' => true, 'max-age' => 3600], + [ + ['private' => true, 'max-age' => 60], + ], + ]; + + yield 'resolves multiple max-age' => [ + ['public' => false, 'private' => true, 's-maxage' => false, 'max-age' => '60'], + ['private' => true, 'max-age' => 100], + [ + ['private' => true, 'max-age' => 3600], + ['public' => true, 'max-age' => 60, 's-maxage' => 60], + ['private' => true, 'max-age' => 60], + ], + ]; + + yield 'merge max-age and s-maxage' => [ + ['public' => true, 's-maxage' => '60', 'max-age' => null], + ['public' => true, 's-maxage' => 3600], + [ + ['public' => true, 'max-age' => 60], + ], + ]; + + yield 'result is private when combining private responses' => [ + ['no-cache' => false, 'must-revalidate' => false, 'private' => true], + ['s-maxage' => 60, 'private' => true], + [ + ['s-maxage' => 60, 'private' => true], + ], + ]; + + yield 'result can have s-maxage and max-age' => [ + ['public' => true, 'private' => false, 's-maxage' => '60', 'max-age' => '30'], + ['s-maxage' => 100, 'max-age' => 2000], + [ + ['s-maxage' => 1000, 'max-age' => 30], + ['s-maxage' => 500, 'max-age' => 500], + ['s-maxage' => 60, 'max-age' => 1000], + ], + ]; + + yield 'does not set headers without value' => [ + ['max-age' => null, 's-maxage' => null, 'public' => null], + ['private' => true], + [ + ['private' => true], + ], + ]; + + yield 'max-age 0 is sent to the client' => [ + ['private' => true, 'max-age' => '0'], + ['max-age' => 0, 'private' => true], + [ + ['max-age' => 60, 'private' => true], + ], + ]; + + yield 'max-age is relative to age' => [ + ['max-age' => '240', 'age' => 60], + ['max-age' => 180], + [ + ['max-age' => 600, 'age' => 60], + ], + ]; + + yield 'retains lowest age of all responses' => [ + ['max-age' => '160', 'age' => 60], + ['max-age' => 600, 'age' => 60], + [ + ['max-age' => 120, 'age' => 20], + ], + ]; + + yield 'max-age can be less than age, essentially expiring the response' => [ + ['age' => 120, 'max-age' => '90'], + ['max-age' => 90, 'age' => 120], + [ + ['max-age' => 120, 'age' => 60], + ], + ]; + + yield 'max-age is 0 regardless of age' => [ + ['max-age' => '0'], + ['max-age' => 60], + [ + ['max-age' => 0, 'age' => 60], + ], + ]; + + yield 'max-age is not negative' => [ + ['max-age' => '0'], + ['max-age' => 0], + [ + ['max-age' => 0, 'age' => 60], + ], + ]; + + yield 'calculates lowest Expires header' => [ + ['expires' => 60], + ['expires' => 60], + [ + ['expires' => 120], + ], + ]; + + yield 'calculates Expires header relative to age' => [ + ['expires' => 210, 'age' => 120], + ['expires' => 90], + [ + ['expires' => 600, 'age' => '120'], + ], + ]; } }