Fix inverted condition

Remove unnecessary yml block
Deprecate HTTP::set_cache_age()
This commit is contained in:
Damian Mooyman 2018-06-13 13:56:47 +12:00
parent befd81d0c2
commit aa1ba0ef90
No known key found for this signature in database
GPG Key ID: 78B823A10DE27D1A
4 changed files with 13 additions and 22 deletions

View File

@ -47,12 +47,3 @@ SilverStripe\Core\Injector\Injector:
properties: properties:
ForceSSL: false ForceSSL: false
ForceWWW: false ForceWWW: false
---
Name: httpcache-dev
Only:
environment: dev
---
SilverStripe\Core\Injector\Injector:
SilverStripe\Control\Middleware\HTTPCacheControlMiddleware:
Properties:
Enabled: false

View File

@ -9,6 +9,7 @@ use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Convert; use SilverStripe\Core\Convert;
use InvalidArgumentException; use InvalidArgumentException;
use finfo; use finfo;
use SilverStripe\Dev\Deprecation;
/** /**
* A class with HTTP-related helpers. Like Debug, this is more a bundle of methods than a class. * A class with HTTP-related helpers. Like Debug, this is more a bundle of methods than a class.
@ -356,12 +357,14 @@ class HTTP
/** /**
* Set the maximum age of this page in web caches, in seconds. * Set the maximum age of this page in web caches, in seconds.
* *
* @deprecated 4.2..5.0 Use HTTPCacheControlMiddleware::singleton()->setMaxAge($age) instead
* @param int $age * @param int $age
*/ */
public static function set_cache_age($age) public static function set_cache_age($age)
{ {
Deprecation::notice('5.0', 'Use HTTPCacheControlMiddleware::singleton()->setMaxAge($age) instead');
self::$cache_age = $age; self::$cache_age = $age;
HTTPCacheControlMiddleware::singleton()->setMaxAge(self::$cache_age); HTTPCacheControlMiddleware::singleton()->setMaxAge($age);
} }
/** /**

View File

@ -499,7 +499,7 @@ class HTTPCacheControlMiddleware implements HTTPMiddleware, Resettable
public function publicCache($force = false) public function publicCache($force = false)
{ {
// Only execute this if its forcing level is high enough // Only execute this if its forcing level is high enough
if (!$this->applyChangeLevel(self::LEVEL_PUBLIC, $force)) { if ($this->applyChangeLevel(self::LEVEL_PUBLIC, $force)) {
$this->setState(self::STATE_PUBLIC); $this->setState(self::STATE_PUBLIC);
} }
return $this; return $this;

View File

@ -2,6 +2,7 @@
namespace SilverStripe\Control\Tests; namespace SilverStripe\Control\Tests;
use PHPUnit_Framework_Error_Warning;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Control\HTTP; use SilverStripe\Control\HTTP;
@ -9,8 +10,6 @@ use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse; use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware; use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel;
use SilverStripe\Dev\FunctionalTest; use SilverStripe\Dev\FunctionalTest;
/** /**
@ -33,18 +32,16 @@ class HTTPTest extends FunctionalTest
$body = "<html><head></head><body><h1>Mysite</h1></body></html>"; $body = "<html><head></head><body><h1>Mysite</h1></body></html>";
$response = new HTTPResponse($body, 200); $response = new HTTPResponse($body, 200);
HTTPCacheControlMiddleware::singleton()->publicCache(); HTTPCacheControlMiddleware::singleton()->publicCache();
HTTP::set_cache_age(30); HTTPCacheControlMiddleware::singleton()->setMaxAge(30);
HTTP::add_cache_headers($response); HTTP::add_cache_headers($response);
$this->assertNotEmpty($response->getHeader('Cache-Control')); $this->assertNotEmpty($response->getHeader('Cache-Control'));
/** @var Kernel $kernel */
$kernel = Injector::inst()->get(Kernel::class);
// Ensure cache headers are set correctly when disabled via config (e.g. when dev) // Ensure cache headers are set correctly when disabled via config (e.g. when dev)
Config::modify()->set(HTTP::class, 'disable_http_cache', true); Config::modify()->set(HTTP::class, 'disable_http_cache', true);
HTTPCacheControlMiddleware::reset(); HTTPCacheControlMiddleware::reset();
HTTPCacheControlMiddleware::singleton()->publicCache(); HTTPCacheControlMiddleware::singleton()->publicCache();
HTTP::set_cache_age(30); HTTPCacheControlMiddleware::singleton()->setMaxAge(30);
$response = new HTTPResponse($body, 200); $response = new HTTPResponse($body, 200);
HTTP::add_cache_headers($response); HTTP::add_cache_headers($response);
$this->assertContains('no-cache', $response->getHeader('Cache-Control')); $this->assertContains('no-cache', $response->getHeader('Cache-Control'));
@ -55,7 +52,7 @@ class HTTPTest extends FunctionalTest
Config::modify()->remove(HTTP::class, 'disable_http_cache'); Config::modify()->remove(HTTP::class, 'disable_http_cache');
HTTPCacheControlMiddleware::reset(); HTTPCacheControlMiddleware::reset();
HTTPCacheControlMiddleware::singleton()->publicCache(); HTTPCacheControlMiddleware::singleton()->publicCache();
HTTP::set_cache_age(30); HTTPCacheControlMiddleware::singleton()->setMaxAge(30);
$response = new HTTPResponse($body, 200); $response = new HTTPResponse($body, 200);
HTTP::add_cache_headers($response); HTTP::add_cache_headers($response);
$this->assertContains('max-age=30', $response->getHeader('Cache-Control')); $this->assertContains('max-age=30', $response->getHeader('Cache-Control'));
@ -69,14 +66,14 @@ class HTTPTest extends FunctionalTest
); );
HTTPCacheControlMiddleware::reset(); HTTPCacheControlMiddleware::reset();
HTTPCacheControlMiddleware::singleton()->publicCache(); HTTPCacheControlMiddleware::singleton()->publicCache();
HTTP::set_cache_age(30); HTTPCacheControlMiddleware::singleton()->setMaxAge(30);
$response = new HTTPResponse($body, 200); $response = new HTTPResponse($body, 200);
foreach ($headers as $name => $value) { foreach ($headers as $name => $value) {
$response->addHeader($name, $value); $response->addHeader($name, $value);
} }
// Expect a warning if the header is already set // Expect a warning if the header is already set
$this->expectException( $this->expectException(PHPUnit_Framework_Error_Warning::class);
\PHPUnit_Framework_Error_Warning::class, $this->expectExceptionMessage(
'Cache-Control header has already been set. ' 'Cache-Control header has already been set. '
. 'Please use HTTPCacheControl API to set caching options instead.' . 'Please use HTTPCacheControl API to set caching options instead.'
); );
@ -88,7 +85,7 @@ class HTTPTest extends FunctionalTest
{ {
$body = "<html><head></head><body><h1>Mysite</h1></body></html>"; $body = "<html><head></head><body><h1>Mysite</h1></body></html>";
$response = new HTTPResponse($body, 200); $response = new HTTPResponse($body, 200);
HTTP::set_cache_age(30); HTTPCacheControlMiddleware::singleton()->setMaxAge(30);
HTTP::add_cache_headers($response); HTTP::add_cache_headers($response);
$v = $response->getHeader('Vary'); $v = $response->getHeader('Vary');