class HTTPCacheControl extends SS_Object (View source)

Class HTTPCacheControl

Constants

LEVEL_FORCED

Forcing level forced, optionally combined with one of the below.

LEVEL_DISABLED

Forcing level caching disabled. Overrides public/private.

LEVEL_PRIVATE

Forcing level private-cached. Overrides public.

LEVEL_PUBLIC

Forcing level public cached. Lowest priority.

LEVEL_ENABLED

Forcing level caching enabled.

Properties

static private array $extensions

An array of extension names and parameters to be applied to this object upon construction.

from  SS_Object
string $class from  SS_Object
static private array $allowed_directives

A list of allowed cache directives for HTTPResponses

Methods

static Config_ForClass|null
config()

Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).

static SS_Object
create()

An implementation of the factory method, allows you to create an instance of a class

static SS_Object
singleton()

Creates a class instance by the "singleton" design pattern.

static 
create_from_string($classSpec, $firstArg = null)

Create an object from a string representation. It treats it as a PHP constructor without the 'new' keyword. It also manages to construct the object without the use of eval().

static 
parse_class_spec($classSpec)

Parses a class-spec, such as "Versioned('Stage','Live')", as passed to create_from_string().

static SS_Object
strong_create()

Similar to {@link Object::create()}, except that classes are only overloaded if you set the $strong parameter to TRUE when using {@link Object::useCustomClass()}

static 
useCustomClass(string $oldClass, string $newClass, bool $strong = false)

This class allows you to overload classes with other classes when they are constructed using the factory method {@link Object::create()}

static string
getCustomClass(string $class)

If a class has been overloaded, get the class name it has been overloaded with - otherwise return the class name

static any
static_lookup($class, $name, null $default = null)

Get the value of a static property of a class, even in that property is declared protected (but not private), without any inheritance, merging or parent lookup if it doesn't exist on the given class.

static 
get_static($class, $name, $uncached = false) deprecated

No description

static 
set_static($class, $name, $value) deprecated

No description

static 
uninherited_static($class, $name, $uncached = false) deprecated

No description

static 
combined_static($class, $name, $ceiling = false) deprecated

No description

static 
addStaticVars($class, $properties, $replace = false) deprecated

No description

static 
add_static_var($class, $name, $value, $replace = false) deprecated

No description

static 
has_extension(string $classOrExtension, string $requiredExtension = null, bool $strict = false)

Return TRUE if a class has a specified extension.

static 
add_extension(string $classOrExtension, string $extension = null)

Add an extension to a specific class.

static 
remove_extension(string $extension)

Remove an extension from a class.

static array
get_extensions(string $class, bool $includeArgumentString = false)

No description

static 
get_extra_config_sources($class = null)

No description

__construct()

No description

mixed
__call(string $method, array $arguments)

Attemps to locate and call a method dynamically added to a class at runtime if a default cannot be located

bool
hasMethod(string $method)

Return TRUE if a method exists on this object

array
allMethodNames(bool $custom = false)

Return the names of all the methods available on this object

stat($name, $uncached = false)

No description

set_stat($name, $value)

No description

uninherited($name)

No description

bool
exists()

Return true if this object "exists" i.e. has a sensible value

string
parentClass()

No description

bool
is_a(string $class)

Check if this class is an instance of a specific class, or has that class as one of its parents

string
__toString()

No description

mixed
invokeWithExtensions(string $method, mixed $argument = null)

Calls a method if available on both this object and all applied {@link Extensions}, and then attempts to merge all results into an array

array
extend(string $method, mixed $a1 = null, mixed $a2 = null, mixed $a3 = null, mixed $a4 = null, mixed $a5 = null, mixed $a6 = null, mixed $a7 = null)

Run the given function on all of this object's extensions. Note that this method originally returned void, so if you wanted to return results, you're hosed

getExtensionInstance(string $extension)

Get an extension instance attached to this object by name.

bool
hasExtension(string $extension)

Returns TRUE if this object instance has a specific extension applied in {@link $extension_instances}. Extension instances are initialized at constructor time, meaning if you use {@link add_extension()} afterwards, the added extension will just be added to new instances of the extended class. Use the static method {@link has_extension()} to check if a class (not an instance) has a specific extension.

array
getExtensionInstances()

Get all extension instances for this specific object instance.

mixed
cacheToFile(string $method, int $lifetime = 3600, string $ID = false, array $arguments = array())

Cache the results of an instance method in this object to a file, or if it is already cache return the cached results

clearCache($method, $ID = false, $arguments = array())

Clears the cache for the given cacheToFile call

$this
setDirective(string $directive, string|bool $value = null)

Low level method for setting directives include any experimental or custom ones added via config

$this
setDirectivesFromArray(array $directives)

Low level method to set directives from an associative array

$this
removeDirective(string $directive)

Low level method for removing directives

bool
hasDirective(string $directive)

Low level method to check if a directive is currently set

string|false|null
getDirective(string $directive)

Low level method to get the value of a directive

$this
setNoStore(bool $noStore = true)

The cache should not store anything about the client request or server response.

$this
setNoCache(bool $noCache = true)

Forces caches to submit the request to the origin server for validation before releasing a cached copy.

$this
setMaxAge(int $age)

Specifies the maximum amount of time (seconds) a resource will be considered fresh.

$this
setSharedMaxAge(int $age)

Overrides max-age or the Expires header, but it only applies to shared caches (e.g., proxies) and is ignored by a private cache.

$this
setMustRevalidate(bool $mustRevalidate = true)

The cache must verify the status of the stale resources before using it and expired ones should not be used.

$this
enableCache(bool $force = false)

Simple way to set cache control header to a cacheable state.

$this
disableCache(bool $force = false)

Simple way to set cache control header to a non-cacheable state.

$this
privateCache(bool $force = false)

Advanced way to set cache control header to a non-cacheable state.

$this
publicCache(bool $force = false)

Advanced way to set cache control header to a cacheable state.

$this
applyToResponse(SS_HTTPResponse $response)

Generate and add the Cache-Control header to a response object

array
generateHeaders()

Generate all headers to output

static 
reset()

Reset registered http cache control and force a fresh instance to be built

Details

static Config_ForClass|null config()

Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).

Return Value

Config_ForClass|null

static SS_Object create()

An implementation of the factory method, allows you to create an instance of a class

This method first for strong class overloads (singletons & DB interaction), then custom class overloads. If an overload is found, an instance of this is returned rather than the original class. To overload a class, use {@link Object::useCustomClass()}

This can be called in one of two ways - either calling via the class directly, or calling on Object and passing the class name as the first parameter. The following are equivalent: $list = DataList::create('SiteTree'); $list = SiteTree::get();

Return Value

SS_Object

static SS_Object singleton()

Creates a class instance by the "singleton" design pattern.

It will always return the same instance for this class, which can be used for performance reasons and as a simple way to access instance methods which don't rely on instance data (e.g. the custom SilverStripe static handling).

Return Value

SS_Object

The singleton instance

static create_from_string($classSpec, $firstArg = null)

Create an object from a string representation. It treats it as a PHP constructor without the 'new' keyword. It also manages to construct the object without the use of eval().

Construction itself is done with Object::create(), so that Object::useCustomClass() calls are respected.

Object::create_from_string("Versioned('Stage','Live')") will return the result of Versioned::create('Stage', 'Live);

It is designed for simple, clonable objects. The first time this method is called for a given string it is cached, and clones of that object are returned.

If you pass the $firstArg argument, this will be prepended to the constructor arguments. It's impossible to pass null as the firstArg argument.

Object::create_from_string("Varchar(50)", "MyField") will return the result of Vachar::create('MyField', '50');

Arguments are always strings, although this is a quirk of the current implementation rather than something that can be relied upon.

Parameters

$classSpec
$firstArg

static parse_class_spec($classSpec)

Parses a class-spec, such as "Versioned('Stage','Live')", as passed to create_from_string().

Returns a 2-elemnent array, with classname and arguments

Parameters

$classSpec

static SS_Object strong_create()

Similar to {@link Object::create()}, except that classes are only overloaded if you set the $strong parameter to TRUE when using {@link Object::useCustomClass()}

Return Value

SS_Object

static useCustomClass(string $oldClass, string $newClass, bool $strong = false)

This class allows you to overload classes with other classes when they are constructed using the factory method {@link Object::create()}

Parameters

string $oldClass

the class to replace

string $newClass

the class to replace it with

bool $strong

allows you to enforce a certain class replacement under all circumstances. This is used in singletons and DB interaction classes

static string getCustomClass(string $class)

If a class has been overloaded, get the class name it has been overloaded with - otherwise return the class name

Parameters

string $class

the class to check

Return Value

string

the class that would be created if you called {@link Object::create()} with the class

static any static_lookup($class, $name, null $default = null)

Get the value of a static property of a class, even in that property is declared protected (but not private), without any inheritance, merging or parent lookup if it doesn't exist on the given class.

Parameters

$class
  • The class to get the static from
$name
  • The property to get from the class
null $default
  • The value to return if property doesn't exist on class

Return Value

any
  • The value of the static property $name on class $class, or $default if that property is not defined

static get_static($class, $name, $uncached = false) deprecated

deprecated

Parameters

$class
$name
$uncached

static set_static($class, $name, $value) deprecated

deprecated

Parameters

$class
$name
$value

static uninherited_static($class, $name, $uncached = false) deprecated

deprecated

Parameters

$class
$name
$uncached

static combined_static($class, $name, $ceiling = false) deprecated

deprecated

Parameters

$class
$name
$ceiling

static addStaticVars($class, $properties, $replace = false) deprecated

deprecated

Parameters

$class
$properties
$replace

static add_static_var($class, $name, $value, $replace = false) deprecated

deprecated

Parameters

$class
$name
$value
$replace

static has_extension(string $classOrExtension, string $requiredExtension = null, bool $strict = false)

Return TRUE if a class has a specified extension.

This supports backwards-compatible format (static Object::has_extension($requiredExtension)) and new format ($object->has_extension($class, $requiredExtension))

Parameters

string $classOrExtension

if 1 argument supplied, the class name of the extension to check for; if 2 supplied, the class name to test

string $requiredExtension

used only if 2 arguments supplied

bool $strict

if the extension has to match the required extension and not be a subclass

static add_extension(string $classOrExtension, string $extension = null)

Add an extension to a specific class.

The preferred method for adding extensions is through YAML config, since it avoids autoloading the class, and is easier to override in more specific configurations.

As an alternative, extensions can be added to a specific class directly in the {@link Object::$extensions} array. See {@link SiteTree::$extensions} for examples. Keep in mind that the extension will only be applied to new instances, not existing ones (including all instances created through {@link singleton()}).

Parameters

string $classOrExtension

Class that should be extended - has to be a subclass of {@link Object}

string $extension

Subclass of {@link Extension} with optional parameters as a string, e.g. "Versioned" or "Translatable('Param')"

See also

http://doc.silverstripe.org/framework/en/trunk/reference/dataextension

static remove_extension(string $extension)

Remove an extension from a class.

Keep in mind that this won't revert any datamodel additions of the extension at runtime, unless its used before the schema building kicks in (in your _config.php). Doesn't remove the extension from any {@link Object} instances which are already created, but will have an effect on new extensions. Clears any previously created singletons through {@link singleton()} to avoid side-effects from stale extension information.

Parameters

string $extension

Classname of an {@link Extension} subclass, without parameters

static array get_extensions(string $class, bool $includeArgumentString = false)

Parameters

string $class
bool $includeArgumentString

Include the argument string in the return array, FALSE would return array("Versioned"), TRUE returns array("Versioned('Stage','Live')").

Return Value

array

Numeric array of either {@link DataExtension} classnames, or eval'ed classname strings with constructor arguments.

static get_extra_config_sources($class = null)

Parameters

$class

__construct()

mixed __call(string $method, array $arguments)

Attemps to locate and call a method dynamically added to a class at runtime if a default cannot be located

You can add extra methods to a class using {@link Extensions}, {@link Object::createMethod()} or {@link Object::addWrapperMethod()}

Parameters

string $method
array $arguments

Return Value

mixed

bool hasMethod(string $method)

Return TRUE if a method exists on this object

This should be used rather than PHP's inbuild method_exists() as it takes into account methods added via extensions

Parameters

string $method

Return Value

bool

array allMethodNames(bool $custom = false)

Return the names of all the methods available on this object

Parameters

bool $custom

include methods added dynamically at runtime

Return Value

array

stat($name, $uncached = false)

Parameters

$name
$uncached

See also

SS_Object::get_static

set_stat($name, $value)

Parameters

$name
$value

See also

SS_Object::set_static

uninherited($name)

Parameters

$name

See also

SS_Object::uninherited_static

bool exists()

Return true if this object "exists" i.e. has a sensible value

This method should be overriden in subclasses to provide more context about the classes state. For example, a {@link DataObject} class could return false when it is deleted from the database

Return Value

bool

string parentClass()

Return Value

string

this classes parent class

bool is_a(string $class)

Check if this class is an instance of a specific class, or has that class as one of its parents

Parameters

string $class

Return Value

bool

string __toString()

Return Value

string

the class name

mixed invokeWithExtensions(string $method, mixed $argument = null)

Calls a method if available on both this object and all applied {@link Extensions}, and then attempts to merge all results into an array

Parameters

string $method

the method name to call

mixed $argument

a single argument to pass

Return Value

mixed

array extend(string $method, mixed $a1 = null, mixed $a2 = null, mixed $a3 = null, mixed $a4 = null, mixed $a5 = null, mixed $a6 = null, mixed $a7 = null)

Run the given function on all of this object's extensions. Note that this method originally returned void, so if you wanted to return results, you're hosed

Currently returns an array, with an index resulting every time the function is called. Only adds returns if they're not NULL, to avoid bogus results from methods just defined on the parent extension. This is important for permission-checks through extend, as they use min() to determine if any of the returns is FALSE. As min() doesn't do type checking, an included NULL return would fail the permission checks.

The extension methods are defined during {@link __construct()} in {@link defineMethods()}.

Parameters

string $method

the name of the method to call on each extension

mixed $a1
mixed $a2
mixed $a3
mixed $a4
mixed $a5
mixed $a6
mixed $a7

Return Value

array

Extension getExtensionInstance(string $extension)

Get an extension instance attached to this object by name.

Parameters

string $extension

Return Value

Extension

bool hasExtension(string $extension)

Returns TRUE if this object instance has a specific extension applied in {@link $extension_instances}. Extension instances are initialized at constructor time, meaning if you use {@link add_extension()} afterwards, the added extension will just be added to new instances of the extended class. Use the static method {@link has_extension()} to check if a class (not an instance) has a specific extension.

Caution: Don't use singleton()->hasExtension() as it will give you inconsistent results based on when the singleton was first accessed.

Parameters

string $extension

Classname of an {@link Extension} subclass without parameters

Return Value

bool

array getExtensionInstances()

Get all extension instances for this specific object instance.

See {@link get_extensions()} to get all applied extension classes for this class (not the instance).

Return Value

array

Map of {@link DataExtension} instances, keyed by classname.

mixed cacheToFile(string $method, int $lifetime = 3600, string $ID = false, array $arguments = array())

Cache the results of an instance method in this object to a file, or if it is already cache return the cached results

Parameters

string $method

the method name to cache

int $lifetime

the cache lifetime in seconds

string $ID

custom cache ID to use

array $arguments

an optional array of arguments

Return Value

mixed

the cached data

clearCache($method, $ID = false, $arguments = array())

Clears the cache for the given cacheToFile call

Parameters

$method
$ID
$arguments

$this setDirective(string $directive, string|bool $value = null)

Low level method for setting directives include any experimental or custom ones added via config

Parameters

string $directive
string|bool $value

Return Value

$this

$this setDirectivesFromArray(array $directives)

Low level method to set directives from an associative array

Parameters

array $directives

Return Value

$this

$this removeDirective(string $directive)

Low level method for removing directives

Parameters

string $directive

Return Value

$this

bool hasDirective(string $directive)

Low level method to check if a directive is currently set

Parameters

string $directive

Return Value

bool

string|false|null getDirective(string $directive)

Low level method to get the value of a directive

Note that null value is acceptable for a directive

Parameters

string $directive

Return Value

string|false|null

$this setNoStore(bool $noStore = true)

The cache should not store anything about the client request or server response.

Set the no-store directive (also removes max-age and s-maxage for consistency purposes)

Parameters

bool $noStore

Return Value

$this

$this setNoCache(bool $noCache = true)

Forces caches to submit the request to the origin server for validation before releasing a cached copy.

Parameters

bool $noCache

Return Value

$this

$this setMaxAge(int $age)

Specifies the maximum amount of time (seconds) a resource will be considered fresh.

This directive is relative to the time of the request.

Parameters

int $age

Return Value

$this

$this setSharedMaxAge(int $age)

Overrides max-age or the Expires header, but it only applies to shared caches (e.g., proxies) and is ignored by a private cache.

Parameters

int $age

Return Value

$this

$this setMustRevalidate(bool $mustRevalidate = true)

The cache must verify the status of the stale resources before using it and expired ones should not be used.

Parameters

bool $mustRevalidate

Return Value

$this

$this enableCache(bool $force = false)

Simple way to set cache control header to a cacheable state.

Use this method over publicCache() if you are unsure about caching details.

Removes no-store and no-cache directives; other directives will remain in place. Use alongside setMaxAge() to indicate caching.

Does not set public directive. Usually, setMaxAge() is sufficient. Use publicCache() if this is explicitly required. See https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#public_vs_private

Parameters

bool $force

Force the cache to public even if its unforced private or public

Return Value

$this

See also

https://docs.silverstripe.org/en/developer_guides/performance/http_cache_headers/

$this disableCache(bool $force = false)

Simple way to set cache control header to a non-cacheable state.

Use this method over privateCache() if you are unsure about caching details. Takes precendence over unforced enableCache(), privateCache() or publicCache() calls.

Removes all state and replaces it with no-cache, no-store, must-revalidate. Although no-store is sufficient the others are added under recommendation from Mozilla (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#Examples)

Does not set private directive, use privateCache() if this is explicitly required. See https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#public_vs_private

Parameters

bool $force

Force the cache to diabled even if it's forced private or public

Return Value

$this

See also

https://docs.silverstripe.org/en/developer_guides/performance/http_cache_headers/

$this privateCache(bool $force = false)

Advanced way to set cache control header to a non-cacheable state.

Indicates that the response is intended for a single user and must not be stored by a shared cache. A private cache (e.g. Web Browser) may store the response. Also removes public as this is a contradictory directive.

Parameters

bool $force

Force the cache to private even if it's forced public

Return Value

$this

See also

https://docs.silverstripe.org/en/developer_guides/performance/http_cache_headers/

$this publicCache(bool $force = false)

Advanced way to set cache control header to a cacheable state.

Indicates that the response may be cached by any cache. (eg: CDNs, Proxies, Web browsers) Also removes private as this is a contradictory directive

Parameters

bool $force

Force the cache to public even if it's private, unless it's been forced private

Return Value

$this

See also

https://docs.silverstripe.org/en/developer_guides/performance/http_cache_headers/

$this applyToResponse(SS_HTTPResponse $response)

Generate and add the Cache-Control header to a response object

Parameters

SS_HTTPResponse $response

Return Value

$this

array generateHeaders()

Generate all headers to output

Return Value

array

static reset()

Reset registered http cache control and force a fresh instance to be built