Requirements_Backend
class Requirements_Backend (View source)
Traits
A class that can be instantiated or replaced via DI
Properties
static private bool | $combine_in_dev | Determine if files should be combined automatically on dev mode. |
|
bool | $writeJavascriptToBody | Put all JavaScript includes at the bottom of the template before the closing |
|
static private string | $default_combined_files_folder | Configures the default prefix for combined files. |
|
static private bool | $combine_hash_querystring | Flag to include the hash in the querystring instead of the filename for combined files. |
Methods
An implementation of the factory method, allows you to create an instance of a class
Creates a class instance by the "singleton" design pattern.
Gets the backend storage for generated files
Enable or disable the combination of CSS and JavaScript files
Check if header comments are written
Flag whether header comments should be written for each combined file
Set the folder to save combined files in. By default they're placed in _combinedfiles, however this may be an issue depending on your setup, especially for CSS files which often contain relative paths.
Retrieve the combined files folder prefix
Set whether to add caching query params to the requests for file-based requirements.
Check whether we want to suffix requirements
Set whether you want to write the JS to the body of the page rather than at the end of the head tag.
Check whether you want to write the JS to the body of the page rather than at the end of the head tag.
Forces the JavaScript requirements to the end of the body, right before the closing tag
Check if the JavaScript requirements are written to the end of the body, right before the closing tag
Check if minify files should be combined
Set if combined files should be minified
Register the given JavaScript file as required.
Gets all scripts that are already provided by prior scripts.
Returns an array of required JavaScript, excluding blocked and duplicates of provided files.
Register the given JavaScript code into the list of requirements
Return all registered custom scripts
Register the given CSS styles into the list of requirements
Return all registered custom CSS
Add the following custom HTML code to the <head>
section of the page
Return all custom head tags
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.
Register the given stylesheet into the list of requirements.
Get the list of registered CSS file requirements, excluding blocked files
Gets the list of all blocked files
Clear either a single or all requirements
Restore requirements cleared by call to Requirements::clear
Block inclusion of a specific file
Remove an item from the block list
Removes all items from the block list
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.
Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given HTTP Response
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.
Concatenate several css or javascript files into a single dynamically generated file. This increases performance by fewer HTTP requests.
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.
Clears all combined files
Clear all registered CSS and JavaScript file combinations
Do the heavy lifting involved in combining the combined files.
Check if combined files are enabled
Registers the given themeable stylesheet as required.
Registers the given themeable javascript as required.
Output debugging information.
Details
static Injectable
create(array ...$args)
An implementation of the factory method, allows you to create an instance of a class
This method will defer class substitution to the Injector API, which can be customised via the Config API to declare substitution classes.
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();
static Injectable
singleton(string $class = null)
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).
GeneratedAssetHandler
getAssetHandler()
Gets the backend storage for generated files
setAssetHandler(GeneratedAssetHandler $handler)
Set a new asset handler for this backend
Requirements_Minifier
getMinifier()
deprecated
deprecated
Gets the minification service for this backend
setMinifier(Requirements_Minifier $minifier = null)
Set a new minification service for this backend
setCombinedFilesEnabled(bool $enable)
Enable or disable the combination of CSS and JavaScript files
bool
getWriteHeaderComment()
Check if header comments are written
$this
setWriteHeaderComment(bool $write)
Flag whether header comments should be written for each combined file
setCombinedFilesFolder(string $folder)
Set the folder to save combined files in. By default they're placed in _combinedfiles, however this may be an issue depending on your setup, especially for CSS files which often contain relative paths.
This must not include any 'assets' prefix
string
getCombinedFilesFolder()
Retrieve the combined files folder prefix
setSuffixRequirements($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.
bool
getSuffixRequirements()
Check whether we want to suffix requirements
$this
setWriteJavascriptToBody($var)
Set whether you want to write the JS to the body of the page rather than at the end of the head tag.
bool
getWriteJavascriptToBody()
Check whether you want to write the JS to the body of the page rather than at the end of the head tag.
$this
setForceJSToBottom($var)
Forces the JavaScript requirements to the end of the body, right before the closing tag
bool
getForceJSToBottom()
Check if the JavaScript requirements are written to the end of the body, right before the closing tag
bool
getMinifyCombinedFiles()
Check if minify files should be combined
$this
setMinifyCombinedFiles(bool $minify)
Set if combined files should be minified
javascript(string $file, array $options = array())
Register the given JavaScript file as required.
array
getProvidedScripts()
Gets all scripts that are already provided by prior scripts.
This follows these rules:
- Files will not be considered provided if they are separately included prior to the providing file.
- Providing files can be blocked, and don't provide anything
- Provided files can't be blocked (you need to block the provider)
- If a combined file includes files that are provided by prior scripts, then these should be excluded from the combined file.
- If a combined file includes files that are provided by later scripts, then these files should be included in the combined file, but we can't block the later script either (possible double up of file).
array
getJavascript()
Returns an array of required JavaScript, excluding blocked and duplicates of provided files.
customScript(string $script, string $uniquenessID = null)
Register the given JavaScript code into the list of requirements
array
getCustomScripts()
Return all registered custom scripts
customCSS(string $script, string $uniquenessID = null)
Register the given CSS styles into the list of requirements
array
getCustomCSS()
Return all registered custom CSS
insertHeadTags(string $html, string $uniquenessID = null)
Add the following custom HTML code to the <head>
section of the page
array
getCustomHeadTags()
Return all custom head tags
javascriptTemplate(string $file, string[] $vars, string $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.
css(string $file, string $media = null, array $options = [])
Register the given stylesheet into the list of requirements.
array
getCSS()
Get the list of registered CSS file requirements, excluding blocked files
array
getBlocked()
Gets the list of all blocked files
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.
restore()
Restore requirements cleared by call to Requirements::clear
block(string|int $fileOrID)
Block inclusion of a specific file
The difference between this and {@link clear} is that the calling order does not matter; {@link 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.
unblock(string|int $fileOrID)
Remove an item from the block list
unblockAll()
Removes all items from the block list
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.
includeInResponse(HTTPResponse $response)
Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given HTTP Response
array|null
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.
combineFiles(string $combinedFileName, array $files, array $options = array())
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 combinations 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',
),
array(
'async' => true,
'defer' => true,
)
);
Example for combined CSS:
Requirements::combine_files(
'foobar.css',
array(
'mysite/javascript/foo.css',
'mysite/javascript/bar.css',
),
array(
'media' => 'print',
)
);
array
getCombinedFiles()
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.
deleteAllCombinedFiles()
Clears all combined files
clearCombinedFiles()
Clear all registered CSS and JavaScript file combinations
processCombinedFiles()
Do the heavy lifting involved in combining the combined files.
bool
getCombinedFilesEnabled()
Check if combined files are enabled
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.
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.
debug()
Output debugging information.