class Requirements implements Flushable (View source)

Requirements tracker for JavaScript and CSS.

Properties

Methods

public static 
flush()

Triggered early in the request when a flush is requested

public static 
set_combined_files_enabled(bool $enable)

Enable combining of css/javascript files.

public static 
bool
get_combined_files_enabled()

Checks whether combining of css/javascript files is enabled.

public static 
set_combined_files_folder(string $folder)

Set the relative folder e.g. 'assets' for where to store combined files

public static 
set_suffix_requirements(bool $var)

Set whether to add caching query params to the requests for file-based requirements.

public static 
bool
get_suffix_requirements()

Check whether we want to suffix requirements

public static 
backend()

No description

public static 
set_backend(Requirements_Backend $backend)

Setter method for changing the Requirements backend

public static 
javascript(string $file, array $options = [])

Register the given JavaScript file as required.

public static 
customScript(string $script, string|int $uniquenessID = null)

Register the given JavaScript code into the list of requirements

public static 
customScriptWithAttributes(string $script, array $options = [], string|int|null $uniquenessID = null)

Register the given Javascript code into the list of requirements with optional tag attributes.

public static 
array
get_custom_scripts()

Return all registered custom scripts

public static 
customCSS(string $script, string|int $uniquenessID = null)

Register the given CSS styles into the list of requirements

public static 
insertHeadTags(string $html, string|int $uniquenessID = null)

Add the following custom HTML code to the <head> section of the page

public static 
javascriptTemplate(string $file, string[]|int[] $vars, string|int $uniquenessID = null)

Include the content of the given JavaScript file in the list of requirements. Dollar-sign variables will be interpolated with values from $vars similar to a .ss template.

public static 
css(string $file, string $media = null, array $options = [])

Register the given stylesheet into the list of requirements.

public static 
themedCSS(string $name, string $media = null)

Registers the given themeable stylesheet as required.

public static 
themedJavascript(string $name, string $type = null)

Registers the given themeable javascript as required.

public static 
clear(string|int $fileOrID = null)

Clear either a single or all requirements

public static 
restore()

Restore requirements cleared by call to Requirements::clear

public static 
block(string|int $fileOrID)

Block inclusion of a specific file

public static 
unblock(string|int $fileOrID)

Remove an item from the block list

public static 
unblock_all()

Removes all items from the block list

public static 
string
includeInHTML(string $content)

Update the given HTML content with the appropriate include tags for the registered requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, including a head and body tag.

public static 
include_in_response(HTTPResponse $response)

Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given HTTP Response

public static 
array
add_i18n_javascript(string $langDir, bool $return = false)

Add i18n files from the given javascript directory. SilverStripe expects that the given directory will contain a number of JavaScript files named by language: en_US.js, de_DE.js, etc.

public static 
combine_files(string $combinedFileName, array $files, array $options = [])

Concatenate several css or javascript files into a single dynamically generated file. This increases performance by fewer HTTP requests.

public static 
array
get_combine_files()

Return all combined files; keys are the combined file names, values are lists of associative arrays with 'files', 'type', and 'media' keys for details about this combined file.

public static 
delete_all_combined_files()

Deletes all generated combined files in the configured combined files directory, but doesn't delete the directory itself

public static 
clear_combined_files()

Re-sets the combined files definition. See Requirements_Backend::clear_combined_files()

public static 
process_combined_files()

Do the heavy lifting involved in combining the combined files.

public static 
bool
get_write_js_to_body()

Set whether you want to write the JS to the body of the page rather than at the end of the head tag.

public static 
set_write_js_to_body(bool $var)

Set whether you want to write the JS to the body of the page rather than at the end of the head tag.

public static 
bool
get_force_js_to_bottom()

Get whether to force the JavaScript to end of the body. Useful if you use inline script tags that don't rely on scripts included via {@link Requirements::javascript()).

public static 
set_force_js_to_bottom(bool $var)

Set whether to force the JavaScript to end of the body. Useful if you use inline script tags that don't rely on scripts included via {@link Requirements::javascript()).

public static 
bool
get_minify_combined_js_files()

Check if JS minification is enabled

public static 
set_minify_combined_js_files(bool $minify)

Enable or disable js minification

public static 
bool
get_write_header_comments()

Check if header comments are written

public
set_write_header_comments(bool $write)

Flag whether header comments should be written for each combined file

public static 
debug()

Output debugging information

Details

static flush()

Triggered early in the request when a flush is requested

static set_combined_files_enabled(bool $enable)

Enable combining of css/javascript files.

Parameters

bool $enable

static bool get_combined_files_enabled()

Checks whether combining of css/javascript files is enabled.

Return Value

bool

static set_combined_files_folder(string $folder)

Set the relative folder e.g. 'assets' for where to store combined files

Parameters

string $folder

Path to folder

static set_suffix_requirements(bool $var)

Set whether to add caching query params to the requests for file-based requirements.

Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by filemtime. This has the benefit of allowing the browser to cache the URL infinitely, while automatically busting this cache every time the file is changed.

Parameters

bool $var

static bool get_suffix_requirements()

Check whether we want to suffix requirements

Return Value

bool

static Requirements_Backend backend()

No description

Return Value

Requirements_Backend

static set_backend(Requirements_Backend $backend)

Setter method for changing the Requirements backend

Parameters

Requirements_Backend $backend

static javascript(string $file, array $options = [])

Register the given JavaScript file as required.

Parameters

string $file

Relative to docroot

array $options

List of options. Available options include:

  • 'provides' : List of scripts files included in this file
  • 'async' : Boolean value to set async attribute to script tag
  • 'defer' : Boolean value to set defer attribute to script tag

static customScript(string $script, string|int $uniquenessID = null)

Register the given JavaScript code into the list of requirements

Parameters

string $script

The script content as a string (without enclosing <script> tag)

string|int $uniquenessID

A unique ID that ensures a piece of code is only added once

static customScriptWithAttributes(string $script, array $options = [], string|int|null $uniquenessID = null)

Register the given Javascript code into the list of requirements with optional tag attributes.

Parameters

string $script

The script content as a string (without enclosing <script> tag)

array $options

List of options. Available options include:

  • 'type' : Specifies the type of script
  • 'crossorigin' : Cross-origin policy for the resource
string|int|null $uniquenessID

A unique ID that ensures a piece of code is only added once

static array get_custom_scripts()

Return all registered custom scripts

Return Value

array

static customCSS(string $script, string|int $uniquenessID = null)

Register the given CSS styles into the list of requirements

Parameters

string $script

CSS selectors as a string (without enclosing <style> tag)

string|int $uniquenessID

A unique ID that ensures a piece of code is only added once

static insertHeadTags(string $html, string|int $uniquenessID = null)

Add the following custom HTML code to the <head> section of the page

Parameters

string $html

Custom HTML code

string|int $uniquenessID

A unique ID that ensures a piece of code is only added once

static javascriptTemplate(string $file, string[]|int[] $vars, string|int $uniquenessID = null)

Include the content of the given JavaScript file in the list of requirements. Dollar-sign variables will be interpolated with values from $vars similar to a .ss template.

Parameters

string $file

The template file to load, relative to docroot

string[]|int[] $vars

The array of variables to interpolate.

string|int $uniquenessID

A unique ID that ensures a piece of code is only added once

static css(string $file, string $media = null, array $options = [])

Register the given stylesheet into the list of requirements.

Parameters

string $file

The CSS file to load, relative to site root

string $media

Comma-separated list of media types to use in the link tag (e.g. 'screen,projector')

array $options

List of options. Available options include:

  • 'integrity' : SubResource Integrity hash
  • 'crossorigin' : Cross-origin policy for the resource

static themedCSS(string $name, string $media = null)

Registers the given themeable stylesheet as required.

A CSS file in the current theme path name 'themename/css/$name.css' is first searched for, and it that doesn't exist and the module parameter is set then a CSS file with that name in the module is used.

Parameters

string $name

The name of the file - eg '/css/File.css' would have the name 'File'

string $media

Comma-separated list of media types to use in the link tag (e.g. 'screen,projector')

static themedJavascript(string $name, string $type = null)

Registers the given themeable javascript as required.

A javascript file in the current theme path name 'themename/javascript/$name.js' is first searched for, and it that doesn't exist and the module parameter is set then a javascript file with that name in the module is used.

Parameters

string $name

The name of the file - eg '/javascript/File.js' would have the name 'File'

string $type

Comma-separated list of types to use in the script tag (e.g. 'text/javascript,text/ecmascript')

static clear(string|int $fileOrID = null)

Clear either a single or all requirements

Caution: Clearing single rules added via customCSS and customScript only works if you originally specified a $uniquenessID.

Parameters

string|int $fileOrID

static restore()

Restore requirements cleared by call to Requirements::clear

static block(string|int $fileOrID)

Block inclusion of a specific file

The difference between this and clear is that the calling order does not matter; clear} must be called after the initial registration, whereas {@link block can be used in advance. This is useful, for example, to block scripts included by a superclass without having to override entire functions and duplicate a lot of code.

Note that blocking should be used sparingly because it's hard to trace where an file is being blocked from.

Parameters

string|int $fileOrID

static unblock(string|int $fileOrID)

Remove an item from the block list

Parameters

string|int $fileOrID

static unblock_all()

Removes all items from the block list

static string includeInHTML(string $content)

Update the given HTML content with the appropriate include tags for the registered requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, including a head and body tag.

Parameters

string $content

HTML content that has already been parsed from the $templateFile through SSViewer

Return Value

string

HTML content augmented with the requirements tags

static include_in_response(HTTPResponse $response)

Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given HTTP Response

Parameters

HTTPResponse $response

static array add_i18n_javascript(string $langDir, bool $return = false)

Add i18n files from the given javascript directory. SilverStripe expects that the given directory will contain a number of JavaScript files named by language: en_US.js, de_DE.js, etc.

Parameters

string $langDir

The JavaScript lang directory, relative to the site root, e.g., 'framework/javascript/lang'

bool $return

Return all relative file paths rather than including them in requirements

Return Value

array

static combine_files(string $combinedFileName, array $files, array $options = [])

Concatenate several css or javascript files into a single dynamically generated file. This increases performance by fewer HTTP requests.

The combined file is regenerated based on every file modification time. Optionally a rebuild can be triggered by appending ?flush=1 to the URL.

All combined files will have a comment on the start of each concatenated file denoting their original position.

CAUTION: You're responsible for ensuring that the load order for combined files is retained - otherwise combining JavaScript files can lead to functional errors in the JavaScript logic, and combining CSS can lead to incorrect inheritance. You can also only include each file once across all includes and comibinations in a single page load.

CAUTION: Combining CSS Files discards any "media" information.

Example for combined JavaScript:

Requirements::combine_files(
 'foobar.js',
 array(
       'mysite/javascript/foo.js',
       'mysite/javascript/bar.js',
   )
);

Example for combined CSS:

Requirements::combine_files(
 'foobar.css',
   array(
       'mysite/javascript/foo.css',
       'mysite/javascript/bar.css',
   )
);

Parameters

string $combinedFileName

Filename of the combined file relative to docroot

array $files

Array of filenames relative to docroot

array $options

Array of options for combining files. Available options are:

  • 'media' : If including CSS Files, you can specify a media type
  • 'async' : If including JavaScript Files, boolean value to set async attribute to script tag
  • 'defer' : If including JavaScript Files, boolean value to set defer attribute to script tag

static array get_combine_files()

Return all combined files; keys are the combined file names, values are lists of associative arrays with 'files', 'type', and 'media' keys for details about this combined file.

Return Value

array

static delete_all_combined_files()

Deletes all generated combined files in the configured combined files directory, but doesn't delete the directory itself

static clear_combined_files()

Re-sets the combined files definition. See Requirements_Backend::clear_combined_files()

static process_combined_files()

Do the heavy lifting involved in combining the combined files.

static bool get_write_js_to_body()

Set whether you want to write the JS to the body of the page rather than at the end of the head tag.

Return Value

bool

static set_write_js_to_body(bool $var)

Set whether you want to write the JS to the body of the page rather than at the end of the head tag.

Parameters

bool $var

static bool get_force_js_to_bottom()

Get whether to force the JavaScript to end of the body. Useful if you use inline script tags that don't rely on scripts included via {@link Requirements::javascript()).

Return Value

bool

static set_force_js_to_bottom(bool $var)

Set whether to force the JavaScript to end of the body. Useful if you use inline script tags that don't rely on scripts included via {@link Requirements::javascript()).

Parameters

bool $var

If true, force the JavaScript to be included at the bottom of the page

static bool get_minify_combined_js_files()

Check if JS minification is enabled

Return Value

bool

static set_minify_combined_js_files(bool $minify)

Enable or disable js minification

Parameters

bool $minify

static bool get_write_header_comments()

Check if header comments are written

Return Value

bool

set_write_header_comments(bool $write)

Flag whether header comments should be written for each combined file

Parameters

bool $write

static debug()

Output debugging information