class Requirements_Backend (View source)

Properties

protected bool $suffix_requirements

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

protected bool $combined_files_enabled

Whether to combine CSS and JavaScript files

protected array $javascript

Paths to all required JavaScript files relative to docroot

protected array $css

Paths to all required CSS files relative to the docroot.

protected array $customScript

All custom javascript code that is inserted into the page's HTML

protected array $customCSS

All custom CSS rules which are inserted directly at the bottom of the HTML tag

protected $customHeadTags

All custom HTML markup which is added before the closing tag, e.g. additional metatags.

protected array $disabled

Remembers the file paths or uniquenessIDs of all Requirements cleared through clear(), so that they can be restored later.

protected array $blocked

The file paths (relative to docroot) or uniquenessIDs of any included requirements which should be blocked when executing inlcudeInHTML(). This is useful, for example, to block scripts included by a superclass without having to override entire functions and duplicate a lot of code.

public array $combine_files

A list of combined files registered via combine_files(). Keys are the output file names, values are lists of input files.

public bool $combine_js_with_jsmin

Use the JSMin library to minify any javascript file passed to combine_files().

public bool $write_header_comment

Whether or not file headers should be written when combining files

protected string $combinedFilesFolder

Where to save combined files. By default they're placed in assets/_combinedfiles, however this may be an issue depending on your setup, especially for CSS files which often contain relative paths.

public bool $write_js_to_body

Put all JavaScript includes at the bottom of the template before the closing tag, rather than the default behaviour of placing them at the end of the tag. This means script downloads won't block other HTTP requests, which can be a performance improvement.

protected bool $force_js_to_bottom

Force the JavaScript to the bottom of the page, even if there's a script tag in the body already

Methods

public
set_combined_files_enabled($enable)

Enable or disable the combination of CSS and JavaScript files

public
bool
get_combined_files_enabled()

Check whether file combination is enabled.

public
setCombinedFilesFolder(string $folder)

Set the folder to save combined files in. By default they're placed in assets/_combinedfiles, however this may be an issue depending on your setup, especially for CSS files which often contain relative paths.

public
string
getCombinedFilesFolder()

No description

public
set_suffix_requirements($var)

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

public
bool
get_suffix_requirements()

Check whether we want to suffix requirements

public
set_write_js_to_body($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
set_force_js_to_bottom($var)

Forces the JavaScript requirements to the end of the body, right before the closing tag

public
javascript(string $file)

Register the given JavaScript file as required.

public
array
get_javascript()

Returns an array of all required JavaScript

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

Register the given JavaScript code into the list of requirements

public
array
get_custom_scripts()

Return all registered custom scripts

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

Register the given CSS styles into the list of requirements

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

Add the following custom HTML code to the section of the page

public
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
css(string $file, string $media = null)

Register the given stylesheet into the list of requirements.

public
array
get_css()

Get the list of registered CSS file requirements, excluding blocked files

public
clear(string|int $fileOrID = null)

Clear either a single or all requirements

public
restore()

Restore requirements cleared by call to Requirements::clear

public
block(string|int $fileOrID)

Block inclusion of a specific file

public
unblock(string|int $fileOrID)

Remove an item from the block list

public
unblock_all()

Removes all items from the block list

public
string
includeInHTML(string $templateFile, 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.

protected
string
removeNewlinesFromCode(string $code)

Remove all newlines from code to preserve layout

protected
string
escapeReplacement(string $replacement)

Safely escape a literal string for use in preg_replace replacement

public
include_in_response(SS_HTTPResponse $response)

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

public
array
add_i18n_javascript(string $langDir, bool $return = false, bool $langOnly = 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.

protected
string|bool
path_for_file(string $fileOrUrl)

Finds the path for specified file

public
bool|void
combine_files(string $combinedFileName, array $files, string $media = null)

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

public
array
get_combine_files()

Return all combined files; keys are the combined file names, values are lists of files being combined.

public
delete_combined_files(string $combinedFileName = null)

Delete all dynamically generated combined files from the filesystem

public
delete_all_combined_files()

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

public
clear_combined_files()

Clear all registered CSS and JavaScript file combinations

public
process_combined_files()

Do the heavy lifting involved in combining (and, in the case of JavaScript minifying) the combined files.

protected
string
minifyFile(string $filename, string $content)

Minify the given $content according to the file type indicated in $filename

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

Registers the given themeable stylesheet as required.

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

Registers the given themeable javascript as required.

public
debug()

Output debugging information.

Details

set_combined_files_enabled($enable)

Enable or disable the combination of CSS and JavaScript files

Parameters

$enable

bool get_combined_files_enabled()

Check whether file combination is enabled.

Return Value

bool

setCombinedFilesFolder(string $folder)

Set the folder to save combined files in. By default they're placed in assets/_combinedfiles, however this may be an issue depending on your setup, especially for CSS files which often contain relative paths.

Parameters

string $folder

string getCombinedFilesFolder()

No description

Return Value

string

Folder relative to the webroot

set_suffix_requirements($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

$var

bool get_suffix_requirements()

Check whether we want to suffix requirements

Return Value

bool

set_write_js_to_body($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

$var

set_force_js_to_bottom($var)

Forces the JavaScript requirements to the end of the body, right before the closing tag

Parameters

$var

javascript(string $file)

Register the given JavaScript file as required.

Parameters

string $file

Relative to docroot

array get_javascript()

Returns an array of all required JavaScript

Return Value

array

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