2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-11-07 03:06:28 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
use SilverStripe\Filesystem\Storage\GeneratedAssetHandler;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Requirements tracker for JavaScript and CSS.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage view
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2015-11-26 03:19:43 +01:00
|
|
|
class Requirements implements Flushable {
|
|
|
|
|
2015-11-30 03:03:46 +01:00
|
|
|
/**
|
|
|
|
* Flag whether combined files should be deleted on flush.
|
|
|
|
*
|
|
|
|
* By default all combined files are deleted on flush. If combined files are stored in source control,
|
|
|
|
* and thus updated manually, you might want to turn this on to disable this behaviour.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $disable_flush_combined = false;
|
|
|
|
|
2015-11-26 03:19:43 +01:00
|
|
|
/**
|
|
|
|
* Triggered early in the request when a flush is requested
|
|
|
|
*/
|
|
|
|
public static function flush() {
|
2015-11-30 03:03:46 +01:00
|
|
|
$disabled = Config::inst()->get(static::class, 'disable_flush_combined');
|
|
|
|
if(!$disabled) {
|
|
|
|
self::delete_all_combined_files();
|
|
|
|
}
|
2015-11-26 03:19:43 +01:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
/**
|
|
|
|
* Enable combining of css/javascript files.
|
2015-07-27 12:31:18 +02:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param bool $enable
|
2009-03-04 04:44:11 +01:00
|
|
|
*/
|
|
|
|
public static function set_combined_files_enabled($enable) {
|
2015-10-12 06:34:34 +02:00
|
|
|
self::backend()->setCombinedFilesEnabled($enable);
|
2009-03-04 04:44:11 +01:00
|
|
|
}
|
|
|
|
|
2009-04-29 01:52:15 +02:00
|
|
|
/**
|
2010-05-25 06:13:45 +02:00
|
|
|
* Checks whether combining of css/javascript files is enabled.
|
2015-07-27 12:31:18 +02:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @return bool
|
2009-04-29 01:52:15 +02:00
|
|
|
*/
|
2010-05-25 06:13:45 +02:00
|
|
|
public static function get_combined_files_enabled() {
|
2015-10-12 06:34:34 +02:00
|
|
|
return self::backend()->getCombinedFilesEnabled();
|
2010-05-25 06:13:45 +02:00
|
|
|
}
|
2010-05-25 06:15:43 +02:00
|
|
|
|
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Set the relative folder e.g. 'assets' for where to store combined files
|
2015-07-27 12:31:18 +02:00
|
|
|
*
|
2010-05-25 06:15:43 +02:00
|
|
|
* @param string $folder Path to folder
|
|
|
|
*/
|
|
|
|
public static function set_combined_files_folder($folder) {
|
|
|
|
self::backend()->setCombinedFilesFolder($folder);
|
|
|
|
}
|
|
|
|
|
2009-04-29 01:52:15 +02:00
|
|
|
/**
|
2015-07-27 12:31:18 +02:00
|
|
|
* Set whether to add caching query params to the requests for file-based requirements.
|
2015-04-29 10:17:08 +02:00
|
|
|
* 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.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2009-04-29 01:52:15 +02:00
|
|
|
* @param bool
|
|
|
|
*/
|
|
|
|
public static function set_suffix_requirements($var) {
|
2015-10-12 06:34:34 +02:00
|
|
|
self::backend()->setSuffixRequirements($var);
|
2009-04-29 01:52:15 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2009-04-29 01:52:15 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Check whether we want to suffix requirements
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2009-04-29 01:52:15 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function get_suffix_requirements() {
|
2015-10-12 06:34:34 +02:00
|
|
|
return self::backend()->getSuffixRequirements();
|
2009-04-29 01:52:15 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2015-07-27 12:31:18 +02:00
|
|
|
* Instance of the requirements for storage. You can create your own backend to change the
|
|
|
|
* default JS and CSS inclusion behaviour.
|
2008-11-07 03:06:28 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @var Requirements_Backend
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
|
|
|
private static $backend = null;
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2015-11-30 03:03:46 +01:00
|
|
|
/**
|
|
|
|
* @return Requirements_Backend
|
|
|
|
*/
|
2008-11-10 02:13:42 +01:00
|
|
|
public static function backend() {
|
2008-11-07 03:06:28 +01:00
|
|
|
if(!self::$backend) {
|
2015-10-12 06:34:34 +02:00
|
|
|
self::$backend = Injector::inst()->create('Requirements_Backend');
|
2008-11-07 03:06:28 +01:00
|
|
|
}
|
|
|
|
return self::$backend;
|
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
|
|
|
* Setter method for changing the Requirements backend
|
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param Requirements_Backend $backend
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
|
|
|
public static function set_backend(Requirements_Backend $backend) {
|
|
|
|
self::$backend = $backend;
|
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Register the given JavaScript file as required.
|
2015-07-27 12:31:18 +02:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string $file Relative to docroot
|
2016-01-14 05:23:21 +01:00
|
|
|
* @param array $options List of options. Available options include:
|
|
|
|
* - 'provides' : List of scripts files included in this file
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2016-01-14 05:23:21 +01:00
|
|
|
public static function javascript($file, $options = array()) {
|
|
|
|
self::backend()->javascript($file, $options);
|
2008-11-07 03:06:28 +01:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Register the given JavaScript code into the list of requirements
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string $script The script content as a string (without enclosing <script> tag)
|
|
|
|
* @param string|int $uniquenessID A unique ID that ensures a piece of code is only added once
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function customScript($script, $uniquenessID = null) {
|
2008-11-07 03:06:28 +01:00
|
|
|
self::backend()->customScript($script, $uniquenessID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Return all registered custom scripts
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_custom_scripts() {
|
2015-10-12 06:34:34 +02:00
|
|
|
return self::backend()->getCustomScripts();
|
2015-04-29 10:17:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-27 12:31:18 +02:00
|
|
|
* Register the given CSS styles into the list of requirements
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string $script CSS selectors as a string (without enclosing <style> tag)
|
|
|
|
* @param string|int $uniquenessID A unique ID that ensures a piece of code is only added once
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function customCSS($script, $uniquenessID = null) {
|
2009-02-02 00:49:53 +01:00
|
|
|
self::backend()->customCSS($script, $uniquenessID);
|
2008-11-07 03:06:28 +01:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Add the following custom HTML code to the <head> section of the page
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string $html Custom HTML code
|
|
|
|
* @param string|int $uniquenessID A unique ID that ensures a piece of code is only added once
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function insertHeadTags($html, $uniquenessID = null) {
|
2008-11-07 03:06:28 +01:00
|
|
|
self::backend()->insertHeadTags($html, $uniquenessID);
|
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* 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.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string $file The template file to load, relative to docroot
|
|
|
|
* @param string[]|int[] $vars The array of variables to interpolate.
|
|
|
|
* @param string|int $uniquenessID A unique ID that ensures a piece of code is only added once
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function javascriptTemplate($file, $vars, $uniquenessID = null) {
|
2008-11-07 03:06:28 +01:00
|
|
|
self::backend()->javascriptTemplate($file, $vars, $uniquenessID);
|
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Register the given stylesheet into the list of requirements.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string $file The CSS file to load, relative to site root
|
2015-07-27 12:31:18 +02:00
|
|
|
* @param string $media Comma-separated list of media types to use in the link tag
|
2015-04-29 10:17:08 +02:00
|
|
|
* (e.g. 'screen,projector')
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function css($file, $media = null) {
|
2008-11-07 03:06:28 +01:00
|
|
|
self::backend()->css($file, $media);
|
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2011-03-22 15:02:11 +01:00
|
|
|
* Registers the given themeable stylesheet as required.
|
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* A CSS file in the current theme path name 'themename/css/$name.css' is first searched for,
|
2015-07-27 12:31:18 +02:00
|
|
|
* and it that doesn't exist and the module parameter is set then a CSS file with that name in
|
2015-04-29 10:17:08 +02:00
|
|
|
* the module is used.
|
2011-03-22 15:02:11 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string $name The name of the file - eg '/css/File.css' would have the name 'File'
|
2015-07-27 12:31:18 +02:00
|
|
|
* @param string $module The module to fall back to if the css file does not exist in the
|
2015-04-29 10:17:08 +02:00
|
|
|
* current theme.
|
2015-07-27 12:31:18 +02:00
|
|
|
* @param string $media Comma-separated list of media types to use in the link tag
|
2015-04-29 10:17:08 +02:00
|
|
|
* (e.g. 'screen,projector')
|
2011-03-22 15:02:11 +01:00
|
|
|
*/
|
|
|
|
public static function themedCSS($name, $module = null, $media = null) {
|
|
|
|
return self::backend()->themedCSS($name, $module, $media);
|
2008-11-07 03:06:28 +01:00
|
|
|
}
|
2011-03-22 15:02:11 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Clear either a single or all requirements
|
2015-07-27 12:31:18 +02:00
|
|
|
*
|
|
|
|
* Caution: Clearing single rules added via customCSS and customScript only works if you
|
2015-04-29 10:17:08 +02:00
|
|
|
* originally specified a $uniquenessID.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string|int $fileOrID
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function clear($fileOrID = null) {
|
2008-11-07 03:06:28 +01:00
|
|
|
self::backend()->clear($fileOrID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Restore requirements cleared by call to Requirements::clear
|
|
|
|
*/
|
|
|
|
public static function restore() {
|
|
|
|
self::backend()->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-27 12:31:18 +02:00
|
|
|
* Block inclusion of a specific file
|
|
|
|
*
|
|
|
|
* The difference between this and {@link clear} is that the calling order does not matter;
|
2015-04-29 10:17:08 +02:00
|
|
|
* {@link clear} must be called after the initial registration, whereas {@link block} can be
|
2015-07-27 12:31:18 +02:00
|
|
|
* used in advance. This is useful, for example, to block scripts included by a superclass
|
2015-04-29 10:17:08 +02:00
|
|
|
* 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.
|
2008-11-07 03:06:28 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string|int $fileOrID
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function block($fileOrID) {
|
2008-11-07 03:06:28 +01:00
|
|
|
self::backend()->block($fileOrID);
|
|
|
|
}
|
2015-07-27 12:31:18 +02:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Remove an item from the block list
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string|int $fileOrID
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function unblock($fileOrID) {
|
2008-11-07 03:06:28 +01:00
|
|
|
self::backend()->unblock($fileOrID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Removes all items from the block list
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function unblock_all() {
|
2015-10-12 06:34:34 +02:00
|
|
|
self::backend()->unblockAll();
|
2008-11-07 03:06:28 +01:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
|
|
|
* Update the given HTML content with the appropriate include tags for the registered
|
2015-04-29 10:17:08 +02:00
|
|
|
* requirements. Needs to receive a valid HTML/XHTML template in the $content parameter,
|
|
|
|
* including a head and body tag.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string $templateFile No longer used, only retained for compatibility
|
|
|
|
* @param string $content HTML content that has already been parsed from the $templateFile
|
|
|
|
* through {@link SSViewer}
|
|
|
|
* @return string HTML content augmented with the requirements tags
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function includeInHTML($templateFile, $content) {
|
2008-11-07 03:06:28 +01:00
|
|
|
return self::backend()->includeInHTML($templateFile, $content);
|
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2015-04-29 10:17:08 +02:00
|
|
|
/**
|
2015-07-27 12:31:18 +02:00
|
|
|
* Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given
|
2015-04-29 10:17:08 +02:00
|
|
|
* HTTP Response
|
2015-07-27 12:31:18 +02:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param SS_HTTPResponse $response
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function include_in_response(SS_HTTPResponse $response) {
|
2015-10-12 06:34:34 +02:00
|
|
|
return self::backend()->includeInResponse($response);
|
2008-11-12 05:31:33 +01:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2015-07-27 12:31:18 +02:00
|
|
|
* 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,
|
2015-04-29 10:17:08 +02:00
|
|
|
* etc.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-07-27 12:31:18 +02:00
|
|
|
* @param string $langDir The JavaScript lang directory, relative to the site root, e.g.,
|
2015-04-29 10:17:08 +02:00
|
|
|
* 'framework/javascript/lang'
|
2015-07-27 12:31:18 +02:00
|
|
|
* @param bool $return Return all relative file paths rather than including them in
|
2015-04-29 10:17:08 +02:00
|
|
|
* requirements
|
|
|
|
* @param bool $langOnly Only include language files, not the base libraries
|
2015-07-27 12:31:18 +02:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @return array
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2012-01-06 12:00:04 +01:00
|
|
|
public static function add_i18n_javascript($langDir, $return = false, $langOnly = false) {
|
|
|
|
return self::backend()->add_i18n_javascript($langDir, $return, $langOnly);
|
2008-11-07 03:06:28 +01:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* 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
|
2015-10-12 06:34:34 +02:00
|
|
|
* rebuild can be triggered by appending ?flush=1 to the URL.
|
2015-04-29 10:17:08 +02:00
|
|
|
*
|
|
|
|
* All combined files will have a comment on the start of each concatenated file denoting their
|
2015-10-12 06:34:34 +02:00
|
|
|
* original position.
|
2015-04-29 10:17:08 +02:00
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
* <code>
|
|
|
|
* Requirements::combine_files(
|
|
|
|
* 'foobar.js',
|
|
|
|
* array(
|
|
|
|
* 'mysite/javascript/foo.js',
|
|
|
|
* 'mysite/javascript/bar.js',
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Example for combined CSS:
|
|
|
|
* <code>
|
|
|
|
* Requirements::combine_files(
|
|
|
|
* 'foobar.css',
|
|
|
|
* array(
|
|
|
|
* 'mysite/javascript/foo.css',
|
|
|
|
* 'mysite/javascript/bar.css',
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param string $combinedFileName Filename of the combined file relative to docroot
|
|
|
|
* @param array $files Array of filenames relative to docroot
|
2015-05-11 07:30:59 +02:00
|
|
|
* @param string $media
|
2008-11-07 03:06:28 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @return bool|void
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2015-05-11 07:30:59 +02:00
|
|
|
public static function combine_files($combinedFileName, $files, $media = null) {
|
2015-10-12 06:34:34 +02:00
|
|
|
self::backend()->combineFiles($combinedFileName, $files, $media);
|
2008-11-07 03:06:28 +01:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2015-07-27 12:31:18 +02:00
|
|
|
* Return all combined files; keys are the combined file names, values are lists of
|
2015-10-12 06:34:34 +02:00
|
|
|
* associative arrays with 'files', 'type', and 'media' keys for details about this
|
|
|
|
* combined file.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2008-11-07 03:06:28 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_combine_files() {
|
2015-10-12 06:34:34 +02:00
|
|
|
return self::backend()->getCombinedFiles();
|
2014-08-16 02:51:17 +02:00
|
|
|
}
|
2008-11-07 03:06:28 +01:00
|
|
|
|
2015-11-26 03:19:43 +01:00
|
|
|
/**
|
|
|
|
* Deletes all generated combined files in the configured combined files directory,
|
|
|
|
* but doesn't delete the directory itself
|
|
|
|
*/
|
|
|
|
public static function delete_all_combined_files() {
|
|
|
|
return self::backend()->deleteAllCombinedFiles();
|
|
|
|
}
|
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
|
|
|
* Re-sets the combined files definition. See {@link Requirements_Backend::clear_combined_files()}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function clear_combined_files() {
|
2015-10-12 06:34:34 +02:00
|
|
|
self::backend()->clearCombinedFiles();
|
2008-11-07 03:06:28 +01:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2015-10-12 06:34:34 +02:00
|
|
|
* Do the heavy lifting involved in combining the combined files.
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function process_combined_files() {
|
2015-10-12 06:34:34 +02:00
|
|
|
return self::backend()->processCombinedFiles();
|
2008-11-07 03:06:28 +01:00
|
|
|
}
|
2008-07-18 01:32:31 +02:00
|
|
|
|
2015-10-23 02:51:26 +02:00
|
|
|
/**
|
|
|
|
* Set whether you want to write the JS to the body of the page rather than at the end of the
|
|
|
|
* head tag.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function get_write_js_to_body() {
|
|
|
|
return self::backend()->getWriteJavascriptToBody();
|
|
|
|
}
|
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Set whether you want to write the JS to the body of the page rather than at the end of the
|
|
|
|
* head tag.
|
2008-11-07 03:06:28 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param bool
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_write_js_to_body($var) {
|
2015-10-12 06:34:34 +02:00
|
|
|
self::backend()->setWriteJavascriptToBody($var);
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2015-10-23 02:51:26 +02:00
|
|
|
/**
|
|
|
|
* 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 bool
|
|
|
|
*/
|
|
|
|
public static function get_force_js_to_bottom() {
|
|
|
|
return self::backend()->getForceJSToBottom();
|
|
|
|
}
|
|
|
|
|
2013-11-25 20:49:35 +01:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* 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()).
|
2013-11-25 20:49:35 +01:00
|
|
|
*
|
2015-10-23 02:51:26 +02:00
|
|
|
* @param bool $var If true, force the JavaScript to be included at the bottom of the page
|
2013-11-25 20:49:35 +01:00
|
|
|
*/
|
|
|
|
public static function set_force_js_to_bottom($var) {
|
2015-10-12 06:34:34 +02:00
|
|
|
self::backend()->setForceJSToBottom($var);
|
2013-11-25 20:49:35 +01:00
|
|
|
}
|
|
|
|
|
2015-10-23 02:51:26 +02:00
|
|
|
/**
|
|
|
|
* Check if JS minification is enabled
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function get_minify_combined_js_files() {
|
|
|
|
return self::backend()->getMinifyCombinedJSFiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable or disable js minification
|
|
|
|
*
|
|
|
|
* @param bool $minify
|
|
|
|
*/
|
|
|
|
public static function set_minify_combined_js_files($minify) {
|
|
|
|
self::backend()->setMinifyCombinedJSFiles($minify);
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Check if header comments are written
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function get_write_header_comments() {
|
|
|
|
return self::backend()->getWriteHeaderComment();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag whether header comments should be written for each combined file
|
|
|
|
*
|
|
|
|
* @param bool $write
|
|
|
|
*/
|
|
|
|
public function set_write_header_comments($write) {
|
|
|
|
self::backend()->setWriteHeaderComment($write);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-29 10:17:08 +02:00
|
|
|
/**
|
|
|
|
* Output debugging information
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function debug() {
|
2008-11-07 03:06:28 +01:00
|
|
|
return self::backend()->debug();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-04-23 02:11:41 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-04-23 02:11:41 +02:00
|
|
|
* @subpackage view
|
|
|
|
*/
|
2015-11-30 03:03:46 +01:00
|
|
|
class Requirements_Backend
|
|
|
|
{
|
2010-05-25 06:13:45 +02:00
|
|
|
|
|
|
|
/**
|
2015-07-27 12:31:18 +02:00
|
|
|
* Whether to add caching query params to the requests for file-based requirements.
|
2015-04-29 10:17:08 +02:00
|
|
|
* 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.
|
2015-07-27 12:31:18 +02:00
|
|
|
*
|
2010-05-25 06:13:45 +02:00
|
|
|
* @var bool
|
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
protected $suffixRequirements = true;
|
2010-05-25 06:13:45 +02:00
|
|
|
|
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Whether to combine CSS and JavaScript files
|
2010-05-25 06:13:45 +02:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @var bool
|
2010-05-25 06:13:45 +02:00
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
protected $combinedFilesEnabled = true;
|
|
|
|
|
|
|
|
/**
|
2015-11-30 03:03:46 +01:00
|
|
|
* Determine if files should be combined automatically on dev mode.
|
|
|
|
*
|
|
|
|
* By default combined files will not be combined except in test or
|
|
|
|
* live environments. Turning this on will allow for pre-combining of files in development mode.
|
2015-10-12 06:34:34 +02:00
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $combine_in_dev = false;
|
2010-05-25 06:13:45 +02:00
|
|
|
|
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Paths to all required JavaScript files relative to docroot
|
2010-05-25 06:13:45 +02:00
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @var array
|
2008-07-18 05:47:17 +02:00
|
|
|
*/
|
2008-11-07 03:06:28 +01:00
|
|
|
protected $javascript = array();
|
2008-07-18 01:32:31 +02:00
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
/**
|
|
|
|
* Map of included scripts to array of contained files.
|
|
|
|
* To be used alongside front-end combination mechanisms.
|
|
|
|
*
|
|
|
|
* @var array Map of providing filepath => array(provided filepaths)
|
|
|
|
*/
|
|
|
|
protected $providedJavascript = array();
|
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Paths to all required CSS files relative to the docroot.
|
2010-05-25 06:13:45 +02:00
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @var array
|
2008-07-18 05:47:17 +02:00
|
|
|
*/
|
2008-11-07 03:06:28 +01:00
|
|
|
protected $css = array();
|
2008-07-18 01:32:31 +02:00
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* All custom javascript code that is inserted into the page's HTML
|
2008-07-18 05:47:17 +02:00
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @var array
|
2008-07-18 05:47:17 +02:00
|
|
|
*/
|
2008-11-07 03:06:28 +01:00
|
|
|
protected $customScript = array();
|
2008-07-18 01:32:31 +02:00
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* All custom CSS rules which are inserted directly at the bottom of the HTML <head> tag
|
2008-07-18 05:47:17 +02:00
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @var array
|
2008-07-18 05:47:17 +02:00
|
|
|
*/
|
2008-11-07 03:06:28 +01:00
|
|
|
protected $customCSS = array();
|
2008-07-18 01:32:31 +02:00
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
/**
|
2015-07-27 12:31:18 +02:00
|
|
|
* All custom HTML markup which is added before the closing <head> tag, e.g. additional
|
2015-04-29 10:17:08 +02:00
|
|
|
* metatags.
|
2015-10-12 06:34:34 +02:00
|
|
|
*
|
|
|
|
* @var array
|
2008-07-18 05:47:17 +02:00
|
|
|
*/
|
2008-11-07 03:06:28 +01:00
|
|
|
protected $customHeadTags = array();
|
2008-07-18 01:32:31 +02:00
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
/**
|
2015-07-27 12:31:18 +02:00
|
|
|
* Remembers the file paths or uniquenessIDs of all Requirements cleared through
|
2015-04-29 10:17:08 +02:00
|
|
|
* {@link clear()}, so that they can be restored later.
|
2008-07-18 05:47:17 +02:00
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @var array
|
2008-07-18 05:47:17 +02:00
|
|
|
*/
|
2008-11-07 03:06:28 +01:00
|
|
|
protected $disabled = array();
|
2010-05-25 06:13:45 +02:00
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* The file paths (relative to docroot) or uniquenessIDs of any included requirements which
|
2015-07-27 12:31:18 +02:00
|
|
|
* should be blocked when executing {@link inlcudeInHTML()}. This is useful, for example,
|
2015-04-29 10:17:08 +02:00
|
|
|
* to block scripts included by a superclass without having to override entire functions and
|
|
|
|
* duplicate a lot of code.
|
2015-07-27 12:31:18 +02:00
|
|
|
*
|
2008-07-18 05:47:17 +02:00
|
|
|
* Use {@link unblock()} or {@link unblock_all()} to revert changes.
|
2010-05-25 06:13:45 +02:00
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @var array
|
2008-07-18 05:47:17 +02:00
|
|
|
*/
|
2008-11-07 03:06:28 +01:00
|
|
|
protected $blocked = array();
|
2010-05-25 06:13:45 +02:00
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* A list of combined files registered via {@link combine_files()}. Keys are the output file
|
|
|
|
* names, values are lists of input files.
|
2010-05-25 06:13:45 +02:00
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @var array
|
2008-07-18 01:32:31 +02:00
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
protected $combinedFiles = array();
|
2010-05-25 06:13:45 +02:00
|
|
|
|
2015-10-23 02:51:26 +02:00
|
|
|
/**
|
|
|
|
* Use the JSMin library to minify any javascript file passed to {@link combine_files()}.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $minifyCombinedJSFiles = true;
|
|
|
|
|
2013-06-04 14:47:44 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Whether or not file headers should be written when combining files
|
2013-06-04 14:47:44 +02:00
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2015-10-23 02:51:26 +02:00
|
|
|
protected $writeHeaderComment = true;
|
2015-07-27 12:31:18 +02:00
|
|
|
|
2010-05-25 06:17:05 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* 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
|
2015-07-27 12:31:18 +02:00
|
|
|
* relative paths.
|
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @var string
|
2010-05-25 06:17:05 +02:00
|
|
|
*/
|
2010-10-15 04:37:41 +02:00
|
|
|
protected $combinedFilesFolder = null;
|
2010-05-25 06:15:43 +02:00
|
|
|
|
2008-08-11 07:29:41 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Put all JavaScript includes at the bottom of the template before the closing <body> tag,
|
2015-07-27 12:31:18 +02:00
|
|
|
* rather than the default behaviour of placing them at the end of the <head> tag. This means
|
2015-04-29 10:17:08 +02:00
|
|
|
* script downloads won't block other HTTP requests, which can be a performance improvement.
|
2015-07-27 12:31:18 +02:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @var bool
|
2008-08-11 07:29:41 +02:00
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
public $writeJavascriptToBody = true;
|
2015-04-29 10:17:08 +02:00
|
|
|
|
2013-11-25 20:49:35 +01:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Force the JavaScript to the bottom of the page, even if there's a script tag in the body already
|
|
|
|
*
|
2013-11-25 20:49:35 +01:00
|
|
|
* @var boolean
|
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
protected $forceJSToBottom = false;
|
|
|
|
|
|
|
|
/**
|
2015-11-30 03:03:46 +01:00
|
|
|
* Configures the default prefix for combined files.
|
|
|
|
*
|
|
|
|
* This defaults to `_combinedfiles`, and is the folder within the configured asset backend that
|
|
|
|
* combined files will be stored in. If using a backend shared with other systems, it is usually
|
|
|
|
* necessary to distinguish combined files from other assets.
|
2015-10-12 06:34:34 +02:00
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $default_combined_files_folder = '_combinedfiles';
|
|
|
|
|
2015-11-30 03:03:46 +01:00
|
|
|
/**
|
|
|
|
* Flag to include the hash in the querystring instead of the filename for combined files.
|
|
|
|
*
|
|
|
|
* By default the `<hash>` of the source files is appended to the end of the combined file
|
|
|
|
* (prior to the file extension). If combined files are versioned in source control or running
|
|
|
|
* in a distributed environment (such as one where the newest version of a file may not always be
|
|
|
|
* immediately available) then it may sometimes be necessary to disable this. When this is set to true,
|
|
|
|
* the hash will instead be appended via a querystring parameter to enable cache busting, but not in
|
|
|
|
* the filename itself. I.e. `assets/_combinedfiles/name.js?m=<hash>`
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $combine_hash_querystring = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var GeneratedAssetHandler
|
|
|
|
*/
|
|
|
|
protected $assetHandler = null;
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Gets the backend storage for generated files
|
|
|
|
*
|
|
|
|
* @return GeneratedAssetHandler
|
|
|
|
*/
|
2015-11-30 03:03:46 +01:00
|
|
|
public function getAssetHandler() {
|
|
|
|
return $this->assetHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a new asset handler for this backend
|
|
|
|
*
|
|
|
|
* @param GeneratedAssetHandler $handler
|
|
|
|
*/
|
|
|
|
public function setAssetHandler(GeneratedAssetHandler $handler) {
|
|
|
|
$this->assetHandler = $handler;
|
2015-10-12 06:34:34 +02:00
|
|
|
}
|
2014-08-16 02:51:17 +02:00
|
|
|
|
2015-04-29 10:17:08 +02:00
|
|
|
/**
|
|
|
|
* Enable or disable the combination of CSS and JavaScript files
|
2015-07-27 12:31:18 +02:00
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @param bool $enable
|
2015-04-29 10:17:08 +02:00
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
public function setCombinedFilesEnabled($enable) {
|
|
|
|
$this->combinedFilesEnabled = (bool) $enable;
|
2010-05-25 06:13:45 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Check if header comments are written
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getWriteHeaderComment() {
|
|
|
|
return $this->writeHeaderComment;
|
2010-05-25 06:15:43 +02:00
|
|
|
}
|
|
|
|
|
2010-10-15 04:37:41 +02:00
|
|
|
/**
|
2015-10-12 06:34:34 +02:00
|
|
|
* Flag whether header comments should be written for each combined file
|
|
|
|
*
|
|
|
|
* @param bool $write
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setWriteHeaderComment($write) {
|
|
|
|
$this->writeHeaderComment = $write;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the folder to save combined files in. By default they're placed in _combinedfiles,
|
2015-07-27 12:31:18 +02:00
|
|
|
* however this may be an issue depending on your setup, especially for CSS files which often
|
2015-04-29 10:17:08 +02:00
|
|
|
* contain relative paths.
|
2015-07-27 12:31:18 +02:00
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* This must not include any 'assets' prefix
|
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string $folder
|
2010-10-15 04:37:41 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setCombinedFilesFolder($folder) {
|
2010-05-25 06:15:43 +02:00
|
|
|
$this->combinedFilesFolder = $folder;
|
2010-05-25 06:13:45 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2010-10-15 04:37:41 +02:00
|
|
|
/**
|
2015-10-12 06:34:34 +02:00
|
|
|
* Retrieve the combined files folder prefix
|
2015-11-26 03:19:43 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2010-10-15 04:37:41 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCombinedFilesFolder() {
|
2015-10-12 06:34:34 +02:00
|
|
|
if($this->combinedFilesFolder) {
|
|
|
|
return $this->combinedFilesFolder;
|
|
|
|
}
|
|
|
|
return Config::inst()->get(__CLASS__, 'default_combined_files_folder');
|
2010-10-15 04:37:41 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2010-05-25 06:13:45 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* 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.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2010-05-25 06:13:45 +02:00
|
|
|
* @param bool
|
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
public function setSuffixRequirements($var) {
|
|
|
|
$this->suffixRequirements = $var;
|
2010-05-25 06:13:45 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2010-05-25 06:13:45 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Check whether we want to suffix requirements
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2010-05-25 06:13:45 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
public function getSuffixRequirements() {
|
|
|
|
return $this->suffixRequirements;
|
2010-05-25 06:13:45 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Set whether you want to write the JS to the body of the page rather than at the end of the
|
|
|
|
* head tag.
|
2008-12-04 23:38:32 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param bool
|
2015-10-12 06:34:34 +02:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setWriteJavascriptToBody($var) {
|
|
|
|
$this->writeJavascriptToBody = $var;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether you want to write the JS to the body of the page rather than at the end of the
|
|
|
|
* head tag.
|
|
|
|
*
|
|
|
|
* @return bool
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
public function getWriteJavascriptToBody() {
|
|
|
|
return $this->writeJavascriptToBody;
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
2015-04-29 10:17:08 +02:00
|
|
|
|
2013-12-07 15:38:51 +01:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Forces the JavaScript requirements to the end of the body, right before the closing tag
|
2013-12-07 15:38:51 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param bool
|
2015-10-12 06:34:34 +02:00
|
|
|
* @return $this
|
2013-12-07 15:38:51 +01:00
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
public function setForceJSToBottom($var) {
|
|
|
|
$this->forceJSToBottom = $var;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the JavaScript requirements are written to the end of the body, right before the closing tag
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getForceJSToBottom() {
|
|
|
|
return $this->forceJSToBottom;
|
2013-12-07 15:38:51 +01:00
|
|
|
}
|
2015-07-27 12:31:18 +02:00
|
|
|
|
2015-10-23 02:51:26 +02:00
|
|
|
/**
|
|
|
|
* Check if minify js files should be combined
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getMinifyCombinedJSFiles() {
|
|
|
|
return $this->minifyCombinedJSFiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set if combined js files should be minified
|
|
|
|
*
|
|
|
|
* @param bool $minify
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setMinifyCombinedJSFiles($minify) {
|
|
|
|
$this->minifyCombinedJSFiles = $minify;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Register the given JavaScript file as required.
|
|
|
|
*
|
|
|
|
* @param string $file Relative to docroot
|
2016-01-14 05:23:21 +01:00
|
|
|
* @param array $options List of options. Available options include:
|
|
|
|
* - 'provides' : List of scripts files included in this file
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-01-14 05:23:21 +01:00
|
|
|
public function javascript($file, $options = array()) {
|
2008-11-07 03:06:28 +01:00
|
|
|
$this->javascript[$file] = true;
|
2016-01-14 05:23:21 +01:00
|
|
|
|
|
|
|
// Record scripts included in this file
|
|
|
|
if(isset($options['provides'])) {
|
|
|
|
$this->providedJavascript[$file] = array_values($options['provides']);
|
|
|
|
}
|
2008-11-07 03:06:28 +01:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2015-10-12 06:34:34 +02:00
|
|
|
* Remove a javascript requirement
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
*/
|
|
|
|
protected function unsetJavascript($file) {
|
|
|
|
unset($this->javascript[$file]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
/**
|
|
|
|
* 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).
|
|
|
|
*
|
|
|
|
* @return array Array of provided files (map of $path => $path)
|
|
|
|
*/
|
|
|
|
public function getProvidedScripts() {
|
|
|
|
$providedScripts = array();
|
|
|
|
$includedScripts = array();
|
|
|
|
foreach($this->javascript as $script => $flag) {
|
|
|
|
// Ignore scripts that are explicitly blocked
|
|
|
|
if(isset($this->blocked[$script])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// At this point, the file is included.
|
|
|
|
// This might also be combined at this point, potentially.
|
|
|
|
$includedScripts[$script] = true;
|
|
|
|
|
|
|
|
// Record any files this provides, EXCEPT those already included by now
|
|
|
|
if(isset($this->providedJavascript[$script])) {
|
|
|
|
foreach($this->providedJavascript[$script] as $provided) {
|
|
|
|
if(!isset($includedScripts[$provided])) {
|
|
|
|
$providedScripts[$provided] = $provided;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $providedScripts;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Returns an array of required JavaScript, excluding blocked
|
2016-01-14 05:23:21 +01:00
|
|
|
* and duplicates of provided files.
|
2008-11-07 03:06:28 +01:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
public function getJavascript() {
|
2016-01-14 05:23:21 +01:00
|
|
|
return array_keys(array_diff_key(
|
|
|
|
$this->javascript,
|
|
|
|
$this->getBlocked(),
|
|
|
|
$this->getProvidedScripts()
|
|
|
|
));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Gets all javascript, including blocked files. Unwraps the array into a non-associative list
|
|
|
|
*
|
|
|
|
* @return array Indexed array of javascript files
|
|
|
|
*/
|
|
|
|
protected function getAllJavascript() {
|
|
|
|
return array_keys($this->javascript);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Register the given JavaScript code into the list of requirements
|
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @param string $script The script content as a string (without enclosing <script> tag)
|
|
|
|
* @param string $uniquenessID A unique ID that ensures a piece of code is only added once
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-11-07 03:06:28 +01:00
|
|
|
public function customScript($script, $uniquenessID = null) {
|
2015-10-12 06:34:34 +02:00
|
|
|
if($uniquenessID) {
|
|
|
|
$this->customScript[$uniquenessID] = $script;
|
|
|
|
} else {
|
|
|
|
$this->customScript[] = $script;
|
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$script .= "\n";
|
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Return all registered custom scripts
|
2009-02-02 00:49:53 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
public function getCustomScripts() {
|
|
|
|
return array_diff_key($this->customScript, $this->blocked);
|
2015-04-29 10:17:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the given CSS styles into the list of requirements
|
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @param string $script CSS selectors as a string (without enclosing <style> tag)
|
|
|
|
* @param string $uniquenessID A unique ID that ensures a piece of code is only added once
|
2009-02-02 00:49:53 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function customCSS($script, $uniquenessID = null) {
|
2015-10-12 06:34:34 +02:00
|
|
|
if($uniquenessID) {
|
|
|
|
$this->customCSS[$uniquenessID] = $script;
|
|
|
|
} else {
|
|
|
|
$this->customCSS[] = $script;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all registered custom CSS
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getCustomCSS() {
|
|
|
|
return array_diff_key($this->customCSS, $this->blocked);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Add the following custom HTML code to the <head> section of the page
|
2008-07-18 05:47:17 +02:00
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @param string $html Custom HTML code
|
|
|
|
* @param string $uniquenessID A unique ID that ensures a piece of code is only added once
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function insertHeadTags($html, $uniquenessID = null) {
|
2015-10-12 06:34:34 +02:00
|
|
|
if($uniquenessID) {
|
|
|
|
$this->customHeadTags[$uniquenessID] = $html;
|
|
|
|
} else {
|
|
|
|
$this->customHeadTags[] = $html;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all custom head tags
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getCustomHeadTags() {
|
|
|
|
return array_diff_key($this->customHeadTags, $this->blocked);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @param string $file The template file to load, relative to docroot
|
|
|
|
* @param string[] $vars The array of variables to interpolate.
|
|
|
|
* @param string $uniquenessID A unique ID that ensures a piece of code is only added once
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function javascriptTemplate($file, $vars, $uniquenessID = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$script = file_get_contents(Director::getAbsFile($file));
|
2009-02-16 22:08:54 +01:00
|
|
|
$search = array();
|
|
|
|
$replace = array();
|
|
|
|
|
|
|
|
if($vars) foreach($vars as $k => $v) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$search[] = '$' . $k;
|
|
|
|
$replace[] = str_replace("\\'","'", Convert::raw2js($v));
|
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$script = str_replace($search, $replace, $script);
|
2008-11-07 03:06:28 +01:00
|
|
|
$this->customScript($script, $uniquenessID);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Register the given stylesheet into the list of requirements.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @param string $file The CSS file to load, relative to site root
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string $media Comma-separated list of media types to use in the link tag
|
|
|
|
* (e.g. 'screen,projector')
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function css($file, $media = null) {
|
2008-11-07 03:06:28 +01:00
|
|
|
$this->css[$file] = array(
|
2007-07-19 12:40:28 +02:00
|
|
|
"media" => $media
|
|
|
|
);
|
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Remove a css requirement
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
*/
|
|
|
|
protected function unsetCSS($file) {
|
|
|
|
unset($this->css[$file]);
|
|
|
|
}
|
|
|
|
|
2015-04-29 10:17:08 +02:00
|
|
|
/**
|
|
|
|
* Get the list of registered CSS file requirements, excluding blocked files
|
2015-07-27 12:31:18 +02:00
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @return array Associative array of file to spec
|
2015-04-29 10:17:08 +02:00
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
public function getCSS() {
|
2009-02-02 00:49:53 +01:00
|
|
|
return array_diff_key($this->css, $this->blocked);
|
2008-11-07 03:06:28 +01:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Gets all CSS files requirements, including blocked
|
|
|
|
*
|
|
|
|
* @return array Associative array of file to spec
|
|
|
|
*/
|
|
|
|
protected function getAllCSS() {
|
|
|
|
return $this->css;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of all blocked files
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getBlocked() {
|
|
|
|
return $this->blocked;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Clear either a single or all requirements
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* Caution: Clearing single rules added via customCSS and customScript only works if you
|
|
|
|
* originally specified a $uniquenessID.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string|int $fileOrID
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function clear($fileOrID = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($fileOrID) {
|
2009-12-16 06:41:38 +01:00
|
|
|
foreach(array('javascript','css', 'customScript', 'customCSS', 'customHeadTags') as $type) {
|
2008-11-07 03:06:28 +01:00
|
|
|
if(isset($this->{$type}[$fileOrID])) {
|
|
|
|
$this->disabled[$type][$fileOrID] = $this->{$type}[$fileOrID];
|
|
|
|
unset($this->{$type}[$fileOrID]);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2008-11-07 03:06:28 +01:00
|
|
|
$this->disabled['javascript'] = $this->javascript;
|
|
|
|
$this->disabled['css'] = $this->css;
|
|
|
|
$this->disabled['customScript'] = $this->customScript;
|
|
|
|
$this->disabled['customCSS'] = $this->customCSS;
|
2008-12-15 02:45:13 +01:00
|
|
|
$this->disabled['customHeadTags'] = $this->customHeadTags;
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
$this->javascript = array();
|
|
|
|
$this->css = array();
|
|
|
|
$this->customScript = array();
|
|
|
|
$this->customCSS = array();
|
|
|
|
$this->customHeadTags = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Restore requirements cleared by call to Requirements::clear
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function restore() {
|
2008-11-07 03:06:28 +01:00
|
|
|
$this->javascript = $this->disabled['javascript'];
|
|
|
|
$this->css = $this->disabled['css'];
|
|
|
|
$this->customScript = $this->disabled['customScript'];
|
|
|
|
$this->customCSS = $this->disabled['customCSS'];
|
2008-12-15 02:49:06 +01:00
|
|
|
$this->customHeadTags = $this->disabled['customHeadTags'];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2015-11-26 03:19:43 +01:00
|
|
|
|
2015-04-29 10:17:08 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param string|int $fileOrID
|
|
|
|
*/
|
|
|
|
public function block($fileOrID) {
|
|
|
|
$this->blocked[$fileOrID] = $fileOrID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an item from the block list
|
|
|
|
*
|
|
|
|
* @param string|int $fileOrID
|
|
|
|
*/
|
|
|
|
public function unblock($fileOrID) {
|
2015-10-12 06:34:34 +02:00
|
|
|
unset($this->blocked[$fileOrID]);
|
2015-04-29 10:17:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all items from the block list
|
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
public function unblockAll() {
|
2015-04-29 10:17:08 +02:00
|
|
|
$this->blocked = array();
|
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Update the given HTML content with the appropriate include tags for the registered
|
2008-07-18 01:32:31 +02:00
|
|
|
* requirements. Needs to receive a valid HTML/XHTML template in the $content parameter,
|
2015-04-29 10:17:08 +02:00
|
|
|
* including a head and body tag.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string $templateFile No longer used, only retained for compatibility
|
|
|
|
* @param string $content HTML content that has already been parsed from the $templateFile
|
|
|
|
* through {@link SSViewer}
|
|
|
|
* @return string HTML content augmented with the requirements tags
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function includeInHTML($templateFile, $content) {
|
2016-01-14 05:23:21 +01:00
|
|
|
// Skip if content isn't injectable, or there is nothing to inject
|
|
|
|
$tagsAvailable = preg_match('#</head\b#', $content);
|
|
|
|
$hasFiles = $this->css || $this->javascript || $this->customCSS || $this->customScript || $this->customHeadTags;
|
|
|
|
if(!$tagsAvailable || !$hasFiles) {
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
$requirements = '';
|
|
|
|
$jsRequirements = '';
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
// Combine files - updates $this->javascript and $this->css
|
|
|
|
$this->processCombinedFiles();
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
foreach($this->getJavascript() as $file) {
|
|
|
|
$path = Convert::raw2xml($this->pathForFile($file));
|
|
|
|
if($path) {
|
|
|
|
$jsRequirements .= "<script type=\"text/javascript\" src=\"$path\"></script>\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-01-14 05:23:21 +01:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
// Add all inline JavaScript *after* including external files they might rely on
|
|
|
|
foreach($this->getCustomScripts() as $script) {
|
|
|
|
$jsRequirements .= "<script type=\"text/javascript\">\n//<![CDATA[\n";
|
|
|
|
$jsRequirements .= "$script\n";
|
|
|
|
$jsRequirements .= "\n//]]>\n</script>\n";
|
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
foreach($this->getCSS() as $file => $params) {
|
|
|
|
$path = Convert::raw2xml($this->pathForFile($file));
|
|
|
|
if($path) {
|
|
|
|
$media = (isset($params['media']) && !empty($params['media']))
|
|
|
|
? " media=\"{$params['media']}\"" : "";
|
|
|
|
$requirements .= "<link rel=\"stylesheet\" type=\"text/css\"{$media} href=\"$path\" />\n";
|
2008-08-11 04:25:44 +02:00
|
|
|
}
|
2016-01-14 05:23:21 +01:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
foreach($this->getCustomCSS() as $css) {
|
|
|
|
$requirements .= "<style type=\"text/css\">\n$css\n</style>\n";
|
|
|
|
}
|
2015-07-27 12:31:18 +02:00
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
foreach($this->getCustomHeadTags() as $customHeadTag) {
|
|
|
|
$requirements .= "$customHeadTag\n";
|
|
|
|
}
|
2015-07-27 12:31:18 +02:00
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
if ($this->getForceJSToBottom()) {
|
|
|
|
// Remove all newlines from code to preserve layout
|
|
|
|
$jsRequirements = preg_replace('/>\n*/', '>', $jsRequirements);
|
|
|
|
|
|
|
|
// Forcefully put the scripts at the bottom of the body instead of before the first
|
|
|
|
// script tag.
|
|
|
|
$content = preg_replace("/(<\/body[^>]*>)/i", $jsRequirements . "\\1", $content);
|
|
|
|
|
|
|
|
// Put CSS at the bottom of the head
|
|
|
|
$content = preg_replace("/(<\/head>)/i", $requirements . "\\1", $content);
|
|
|
|
} elseif($this->getWriteJavascriptToBody()) {
|
|
|
|
// Remove all newlines from code to preserve layout
|
|
|
|
$jsRequirements = preg_replace('/>\n*/', '>', $jsRequirements);
|
|
|
|
|
|
|
|
// If your template already has script tags in the body, then we try to put our script
|
|
|
|
// tags just before those. Otherwise, we put it at the bottom.
|
|
|
|
$p2 = stripos($content, '<body');
|
|
|
|
$p1 = stripos($content, '<script', $p2);
|
|
|
|
|
|
|
|
$commentTags = array();
|
|
|
|
$canWriteToBody = ($p1 !== false)
|
|
|
|
&&
|
|
|
|
// Check that the script tag is not inside a html comment tag
|
|
|
|
!(
|
|
|
|
preg_match('/.*(?|(<!--)|(-->))/U', $content, $commentTags, 0, $p1)
|
2014-08-30 20:32:28 +02:00
|
|
|
&&
|
2016-01-14 05:23:21 +01:00
|
|
|
$commentTags[1] == '-->'
|
|
|
|
);
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
if($canWriteToBody) {
|
|
|
|
$content = substr($content,0,$p1) . $jsRequirements . substr($content,$p1);
|
2008-08-11 04:25:44 +02:00
|
|
|
} else {
|
2016-01-14 05:23:21 +01:00
|
|
|
$content = preg_replace("/(<\/body[^>]*>)/i", $jsRequirements . "\\1", $content);
|
2008-08-11 04:25:44 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
// Put CSS at the bottom of the head
|
|
|
|
$content = preg_replace("/(<\/head>)/i", $requirements . "\\1", $content);
|
|
|
|
} else {
|
|
|
|
$content = preg_replace("/(<\/head>)/i", $requirements . "\\1", $content);
|
|
|
|
$content = preg_replace("/(<\/head>)/i", $jsRequirements . "\\1", $content);
|
|
|
|
}
|
2008-08-11 07:29:41 +02:00
|
|
|
return $content;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-11-12 05:31:33 +01:00
|
|
|
|
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given
|
|
|
|
* HTTP Response
|
|
|
|
*
|
|
|
|
* @param SS_HTTPResponse $response
|
2008-11-12 05:31:33 +01:00
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
public function includeInResponse(SS_HTTPResponse $response) {
|
|
|
|
$this->processCombinedFiles();
|
2008-11-13 02:31:31 +01:00
|
|
|
$jsRequirements = array();
|
2008-11-13 02:39:30 +01:00
|
|
|
$cssRequirements = array();
|
2008-11-12 05:31:33 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
foreach($this->getJavascript() as $file) {
|
|
|
|
$path = $this->pathForFile($file);
|
2012-01-06 14:09:43 +01:00
|
|
|
if($path) {
|
|
|
|
$jsRequirements[] = str_replace(',', '%2C', $path);
|
|
|
|
}
|
2008-11-12 05:31:33 +01:00
|
|
|
}
|
2015-12-22 01:56:05 +01:00
|
|
|
|
|
|
|
if(count($jsRequirements)) {
|
|
|
|
$response->addHeader('X-Include-JS', implode(',', $jsRequirements));
|
|
|
|
}
|
2008-11-12 05:31:33 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
foreach($this->getCSS() as $file => $params) {
|
|
|
|
$path = $this->pathForFile($file);
|
2012-01-06 14:09:43 +01:00
|
|
|
if($path) {
|
|
|
|
$path = str_replace(',', '%2C', $path);
|
|
|
|
$cssRequirements[] = isset($params['media']) ? "$path:##:$params[media]" : $path;
|
|
|
|
}
|
2008-11-12 05:31:33 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 01:56:05 +01:00
|
|
|
if(count($cssRequirements)) {
|
|
|
|
$response->addHeader('X-Include-CSS', implode(',', $cssRequirements));
|
|
|
|
}
|
2008-11-12 05:31:33 +01:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2009-03-10 23:08:52 +01:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* 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.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string $langDir The JavaScript lang directory, relative to the site root, e.g.,
|
|
|
|
* 'framework/javascript/lang'
|
|
|
|
* @param bool $return Return all relative file paths rather than including them in
|
|
|
|
* requirements
|
|
|
|
* @param bool $langOnly Only include language files, not the base libraries
|
|
|
|
*
|
|
|
|
* @return array
|
2009-03-10 23:08:52 +01:00
|
|
|
*/
|
2012-01-06 12:00:04 +01:00
|
|
|
public function add_i18n_javascript($langDir, $return = false, $langOnly = false) {
|
|
|
|
$files = array();
|
2013-10-23 17:53:08 +02:00
|
|
|
$base = Director::baseFolder() . '/';
|
2013-03-21 19:48:54 +01:00
|
|
|
if(i18n::config()->js_i18n) {
|
2009-03-10 23:08:52 +01:00
|
|
|
// Include i18n.js even if no languages are found. The fact that
|
|
|
|
// add_i18n_javascript() was called indicates that the methods in
|
|
|
|
// here are needed.
|
2012-03-24 04:38:57 +01:00
|
|
|
if(!$langOnly) $files[] = FRAMEWORK_DIR . '/javascript/i18n.js';
|
2009-03-10 23:08:52 +01:00
|
|
|
|
|
|
|
if(substr($langDir,-1) != '/') $langDir .= '/';
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2013-10-23 17:53:08 +02:00
|
|
|
$candidates = array(
|
|
|
|
'en.js',
|
|
|
|
'en_US.js',
|
|
|
|
i18n::get_lang_from_locale(i18n::default_locale()) . '.js',
|
|
|
|
i18n::default_locale() . '.js',
|
|
|
|
i18n::get_lang_from_locale(i18n::get_locale()) . '.js',
|
|
|
|
i18n::get_locale() . '.js',
|
|
|
|
);
|
|
|
|
foreach($candidates as $candidate) {
|
|
|
|
if(file_exists($base . DIRECTORY_SEPARATOR . $langDir . $candidate)) {
|
|
|
|
$files[] = $langDir . $candidate;
|
|
|
|
}
|
2013-01-09 22:31:19 +01:00
|
|
|
}
|
2009-03-10 23:08:52 +01:00
|
|
|
} else {
|
2013-10-23 17:53:08 +02:00
|
|
|
// Stub i18n implementation for when i18n is disabled.
|
2015-10-12 06:34:34 +02:00
|
|
|
if(!$langOnly) {
|
|
|
|
$files[] = FRAMEWORK_DIR . '/javascript/i18nx.js';
|
|
|
|
}
|
2012-01-06 12:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($return) {
|
|
|
|
return $files;
|
|
|
|
} else {
|
2015-10-12 06:34:34 +02:00
|
|
|
foreach($files as $file) {
|
|
|
|
$this->javascript($file);
|
|
|
|
}
|
2008-10-03 02:46:07 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
}
|
|
|
|
|
2008-08-11 09:22:50 +02:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* Finds the path for specified file
|
2008-08-11 09:22:50 +02:00
|
|
|
*
|
|
|
|
* @param string $fileOrUrl
|
2015-04-29 10:17:08 +02:00
|
|
|
* @return string|bool
|
2008-08-11 09:22:50 +02:00
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
protected function pathForFile($fileOrUrl) {
|
|
|
|
// Since combined urls could be root relative, treat them as urls here.
|
|
|
|
if(preg_match('{^(//)|(http[s]?:)}', $fileOrUrl) || Director::is_root_relative_url($fileOrUrl)) {
|
2008-08-11 09:22:50 +02:00
|
|
|
return $fileOrUrl;
|
|
|
|
} elseif(Director::fileExists($fileOrUrl)) {
|
2012-12-18 00:58:09 +01:00
|
|
|
$filePath = preg_replace('/\?.*/', '', Director::baseFolder() . '/' . $fileOrUrl);
|
2011-03-14 06:26:32 +01:00
|
|
|
$prefix = Director::baseURL();
|
2009-04-29 01:52:15 +02:00
|
|
|
$mtimesuffix = "";
|
2009-04-29 02:07:39 +02:00
|
|
|
$suffix = '';
|
2015-10-12 06:34:34 +02:00
|
|
|
if($this->getSuffixRequirements()) {
|
2012-12-18 00:58:09 +01:00
|
|
|
$mtimesuffix = "?m=" . filemtime($filePath);
|
|
|
|
$suffix = '&';
|
|
|
|
}
|
2009-04-29 02:07:39 +02:00
|
|
|
if(strpos($fileOrUrl, '?') !== false) {
|
2012-12-18 00:58:09 +01:00
|
|
|
if (strlen($suffix) == 0) {
|
|
|
|
$suffix = '?';
|
|
|
|
}
|
|
|
|
$suffix .= substr($fileOrUrl, strpos($fileOrUrl, '?')+1);
|
2009-04-29 02:07:39 +02:00
|
|
|
$fileOrUrl = substr($fileOrUrl, 0, strpos($fileOrUrl, '?'));
|
2013-04-30 00:33:21 +02:00
|
|
|
} else {
|
|
|
|
$suffix = '';
|
2009-04-29 02:07:39 +02:00
|
|
|
}
|
|
|
|
return "{$prefix}{$fileOrUrl}{$mtimesuffix}{$suffix}";
|
2008-08-11 09:22:50 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
/**
|
2015-07-27 12:31:18 +02:00
|
|
|
* Concatenate several css or javascript files into a single dynamically generated file. This
|
2015-04-29 10:17:08 +02:00
|
|
|
* increases performance by fewer HTTP requests.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-07-27 12:31:18 +02:00
|
|
|
* The combined file is regenerated based on every file modification time. Optionally a
|
2015-10-12 06:34:34 +02:00
|
|
|
* rebuild can be triggered by appending ?flush=1 to the URL.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-07-27 12:31:18 +02:00
|
|
|
* All combined files will have a comment on the start of each concatenated file denoting their
|
2015-10-12 06:34:34 +02:00
|
|
|
* original position.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-07-27 12:31:18 +02:00
|
|
|
* 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
|
2015-04-29 10:17:08 +02:00
|
|
|
* 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.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2008-09-22 18:02:33 +02:00
|
|
|
* CAUTION: Combining CSS Files discards any "media" information.
|
2008-07-18 01:32:31 +02:00
|
|
|
*
|
|
|
|
* Example for combined JavaScript:
|
|
|
|
* <code>
|
|
|
|
* Requirements::combine_files(
|
|
|
|
* 'foobar.js',
|
|
|
|
* array(
|
2015-04-29 10:17:08 +02:00
|
|
|
* 'mysite/javascript/foo.js',
|
|
|
|
* 'mysite/javascript/bar.js',
|
|
|
|
* )
|
2008-07-18 01:32:31 +02:00
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Example for combined CSS:
|
|
|
|
* <code>
|
|
|
|
* Requirements::combine_files(
|
|
|
|
* 'foobar.css',
|
2015-04-29 10:17:08 +02:00
|
|
|
* array(
|
|
|
|
* 'mysite/javascript/foo.css',
|
|
|
|
* 'mysite/javascript/bar.css',
|
|
|
|
* )
|
2008-07-18 01:32:31 +02:00
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*
|
2015-04-29 10:17:08 +02:00
|
|
|
* @param string $combinedFileName Filename of the combined file relative to docroot
|
2015-10-12 06:34:34 +02:00
|
|
|
* @param array $files Array of filenames relative to docroot
|
|
|
|
* @param string $media If including CSS Files, you can specify a media type
|
2008-07-18 01:32:31 +02:00
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
public function combineFiles($combinedFileName, $files, $media = null) {
|
|
|
|
// Skip this combined files if already included
|
|
|
|
if(isset($this->combinedFiles[$combinedFileName])) {
|
|
|
|
return;
|
2008-07-18 05:47:17 +02:00
|
|
|
}
|
2015-11-26 03:19:43 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
// Add all files to necessary type list
|
|
|
|
$paths = array();
|
|
|
|
$combinedType = null;
|
|
|
|
foreach($files as $file) {
|
|
|
|
// Get file details
|
|
|
|
list($path, $type) = $this->parseCombinedFile($file);
|
|
|
|
if($type === 'javascript') {
|
|
|
|
$type = 'js';
|
2010-10-15 05:09:15 +02:00
|
|
|
}
|
2015-10-12 06:34:34 +02:00
|
|
|
if($combinedType && $type && $combinedType !== $type) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
"Cannot mix js and css files in same combined file {$combinedFileName}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
switch($type) {
|
|
|
|
case 'css':
|
|
|
|
$this->css($path, $media);
|
|
|
|
break;
|
|
|
|
case 'js':
|
|
|
|
$this->javascript($path);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new InvalidArgumentException("Invalid combined file type: {$type}");
|
2010-10-15 05:09:15 +02:00
|
|
|
}
|
2015-10-12 06:34:34 +02:00
|
|
|
$combinedType = $type;
|
|
|
|
$paths[] = $path;
|
2010-10-15 05:09:15 +02:00
|
|
|
}
|
2015-10-12 06:34:34 +02:00
|
|
|
|
|
|
|
// Duplicate check
|
|
|
|
foreach($this->combinedFiles as $existingCombinedFilename => $combinedItem) {
|
|
|
|
$existingFiles = $combinedItem['files'];
|
|
|
|
$duplicates = array_intersect($existingFiles, $paths);
|
|
|
|
if($duplicates) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
"Requirements_Backend::combine_files(): Already included file(s) %s in combined file '%s'",
|
|
|
|
implode(',', $duplicates),
|
|
|
|
$existingCombinedFilename
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-26 03:19:43 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->combinedFiles[$combinedFileName] = array(
|
|
|
|
'files' => $paths,
|
|
|
|
'type' => $combinedType,
|
|
|
|
'media' => $media
|
|
|
|
);
|
2008-07-18 05:47:17 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2015-04-29 10:17:08 +02:00
|
|
|
/**
|
2015-10-12 06:34:34 +02:00
|
|
|
* Return path and type of given combined file
|
2015-04-29 10:17:08 +02:00
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @param string|array $file Either a file path, or an array spec
|
|
|
|
* @return array array with two elements, path and type of file
|
2008-07-18 05:47:17 +02:00
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
protected function parseCombinedFile($file) {
|
|
|
|
// Array with path and type keys
|
|
|
|
if(is_array($file) && isset($file['path']) && isset($file['type'])) {
|
|
|
|
return array($file['path'], $file['type']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract value from indexed array
|
|
|
|
if(is_array($file)) {
|
|
|
|
$path = array_shift($file);
|
|
|
|
|
|
|
|
// See if there's a type specifier
|
|
|
|
if($file) {
|
|
|
|
$type = array_shift($file);
|
|
|
|
return array($path, $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise convent to string
|
|
|
|
$file = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = File::get_file_extension($file);
|
|
|
|
return array($file, $type);
|
2008-07-18 05:47:17 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
/**
|
2015-10-12 06:34:34 +02:00
|
|
|
* 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.
|
2012-12-12 12:00:52 +01:00
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @return array
|
2008-07-18 05:47:17 +02:00
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
public function getCombinedFiles() {
|
|
|
|
return array_diff_key($this->combinedFiles, $this->blocked);
|
2008-07-18 01:32:31 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2014-08-16 02:51:17 +02:00
|
|
|
/**
|
2015-10-12 06:34:34 +02:00
|
|
|
* Includes all combined files, including blocked ones
|
|
|
|
*
|
|
|
|
* @return type
|
2014-08-16 02:51:17 +02:00
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
protected function getAllCombinedFiles() {
|
|
|
|
return $this->combinedFiles;
|
2014-08-16 02:51:17 +02:00
|
|
|
}
|
|
|
|
|
2015-11-26 03:19:43 +01:00
|
|
|
/**
|
|
|
|
* Clears all combined files
|
|
|
|
*/
|
|
|
|
public function deleteAllCombinedFiles() {
|
|
|
|
$combinedFolder = $this->getCombinedFilesFolder();
|
|
|
|
if($combinedFolder) {
|
|
|
|
$this->getAssetHandler()->removeContent($combinedFolder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 10:17:08 +02:00
|
|
|
/**
|
|
|
|
* Clear all registered CSS and JavaScript file combinations
|
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
public function clearCombinedFiles() {
|
|
|
|
$this->combinedFiles = array();
|
2008-08-12 01:18:56 +02:00
|
|
|
}
|
2008-11-07 03:06:28 +01:00
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
/**
|
2015-10-12 06:34:34 +02:00
|
|
|
* Do the heavy lifting involved in combining the combined files.
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
public function processCombinedFiles() {
|
|
|
|
// Check if combining is enabled
|
2015-10-23 02:51:26 +02:00
|
|
|
if(!$this->getCombinedFilesEnabled()) {
|
2008-11-12 22:15:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-09-20 04:00:11 +02:00
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
// Before scripts are modified, detect files that are provided by preceding ones
|
|
|
|
$providedScripts = $this->getProvidedScripts();
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
// Process each combined files
|
|
|
|
foreach($this->getAllCombinedFiles() as $combinedFile => $combinedItem) {
|
|
|
|
$fileList = $combinedItem['files'];
|
|
|
|
$type = $combinedItem['type'];
|
|
|
|
$media = $combinedItem['media'];
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
// Generate this file, unless blocked
|
|
|
|
$combinedURL = null;
|
|
|
|
if(!isset($this->blocked[$combinedFile])) {
|
2016-01-14 05:23:21 +01:00
|
|
|
// Filter files for blocked / provided
|
|
|
|
$filteredFileList = array_diff(
|
|
|
|
$fileList,
|
|
|
|
$this->getBlocked(),
|
|
|
|
$providedScripts
|
|
|
|
);
|
|
|
|
$combinedURL = $this->getCombinedFileURL($combinedFile, $filteredFileList, $type);
|
2008-07-18 01:32:31 +02:00
|
|
|
}
|
2010-05-25 06:15:43 +02:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
// Replace all existing files, injecting the combined file at the position of the first item
|
|
|
|
// in order to preserve inclusion order.
|
|
|
|
// Note that we iterate across blocked files in order to get the correct order, and validate
|
|
|
|
// that the file is included in the correct location (regardless of which files are blocked).
|
|
|
|
$included = false;
|
|
|
|
switch($type) {
|
|
|
|
case 'css': {
|
|
|
|
$newCSS = array(); // Assoc array of css file => spec
|
|
|
|
foreach($this->getAllCSS() as $css => $spec) {
|
|
|
|
if(!in_array($css, $fileList)) {
|
|
|
|
$newCSS[$css] = $spec;
|
|
|
|
} elseif(!$included && $combinedURL) {
|
|
|
|
$newCSS[$combinedURL] = array('media' => $media);
|
|
|
|
$included = true;
|
|
|
|
}
|
|
|
|
// If already included, or otherwise blocked, then don't add into CSS
|
|
|
|
}
|
|
|
|
$this->css = $newCSS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'js': {
|
|
|
|
// Assoc array of file => true
|
|
|
|
$newJS = array();
|
|
|
|
foreach($this->getAllJavascript() as $script) {
|
|
|
|
if(!in_array($script, $fileList)) {
|
|
|
|
$newJS[$script] = true;
|
|
|
|
} elseif(!$included && $combinedURL) {
|
|
|
|
$newJS[$combinedURL] = true;
|
|
|
|
$included = true;
|
|
|
|
}
|
|
|
|
// If already included, or otherwise blocked, then don't add into scripts
|
|
|
|
}
|
|
|
|
$this->javascript = $newJS;
|
|
|
|
break;
|
|
|
|
}
|
2008-07-18 01:32:31 +02:00
|
|
|
}
|
|
|
|
}
|
2015-10-12 06:34:34 +02:00
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Given a set of files, combine them (as necessary) and return the url
|
|
|
|
*
|
|
|
|
* @param string $combinedFile Filename for this combined file
|
|
|
|
* @param array $fileList List of files to combine
|
2015-10-23 02:51:26 +02:00
|
|
|
* @param string $type Either 'js' or 'css'
|
2016-01-14 05:23:21 +01:00
|
|
|
* @return string|null URL to this resource, if there are files to combine
|
2015-10-12 06:34:34 +02:00
|
|
|
*/
|
2015-10-23 02:51:26 +02:00
|
|
|
protected function getCombinedFileURL($combinedFile, $fileList, $type) {
|
2016-01-14 05:23:21 +01:00
|
|
|
// Skip empty lists
|
|
|
|
if(empty($fileList)) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-10-12 06:34:34 +02:00
|
|
|
|
2015-11-26 03:19:43 +01:00
|
|
|
// Generate path (Filename)
|
2015-11-30 03:03:46 +01:00
|
|
|
$hashQuerystring = Config::inst()->get(static::class, 'combine_hash_querystring');
|
|
|
|
if(!$hashQuerystring) {
|
|
|
|
$combinedFile = $this->hashedCombinedFilename($combinedFile, $fileList);
|
|
|
|
}
|
|
|
|
$combinedFileID = File::join_paths($this->getCombinedFilesFolder(), $combinedFile);
|
2015-10-12 06:34:34 +02:00
|
|
|
|
|
|
|
// Send file combination request to the backend, with an optional callback to perform regeneration
|
2015-10-23 02:51:26 +02:00
|
|
|
$minify = $this->getMinifyCombinedJSFiles();
|
2015-10-12 06:34:34 +02:00
|
|
|
$combinedURL = $this
|
|
|
|
->getAssetHandler()
|
2015-11-26 03:19:43 +01:00
|
|
|
->getContentURL(
|
2015-10-12 06:34:34 +02:00
|
|
|
$combinedFileID,
|
2015-10-23 02:51:26 +02:00
|
|
|
function() use ($fileList, $minify, $type) {
|
|
|
|
// Physically combine all file content
|
2015-10-12 06:34:34 +02:00
|
|
|
$combinedData = '';
|
|
|
|
$base = Director::baseFolder() . '/';
|
2015-10-23 02:51:26 +02:00
|
|
|
$minifier = Injector::inst()->get('Requirements_Minifier');
|
2015-11-26 03:19:43 +01:00
|
|
|
foreach($fileList as $file) {
|
2015-10-12 06:34:34 +02:00
|
|
|
$fileContent = file_get_contents($base . $file);
|
2015-10-23 02:51:26 +02:00
|
|
|
// Use configured minifier
|
|
|
|
if($minify) {
|
|
|
|
$fileContent = $minifier->minify($fileContent, $type, $file);
|
|
|
|
}
|
2015-11-26 03:19:43 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
if ($this->writeHeaderComment) {
|
|
|
|
// Write a header comment for each file for easier identification and debugging.
|
|
|
|
$combinedData .= "/****** FILE: $file *****/\n";
|
|
|
|
}
|
|
|
|
$combinedData .= $fileContent . "\n";
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|
2015-10-12 06:34:34 +02:00
|
|
|
return $combinedData;
|
2008-07-18 01:32:31 +02:00
|
|
|
}
|
2015-10-12 06:34:34 +02:00
|
|
|
);
|
2015-07-27 12:31:18 +02:00
|
|
|
|
2015-11-30 03:03:46 +01:00
|
|
|
// If the name isn't hashed, we will need to append the querystring m= parameter instead
|
|
|
|
// Since url won't be automatically suffixed, add it in here
|
|
|
|
if($hashQuerystring && $this->getSuffixRequirements()) {
|
|
|
|
$hash = $this->hashOfFiles($fileList);
|
|
|
|
$q = stripos($combinedURL, '?') === false ? '?' : '&';
|
|
|
|
$combinedURL .= "{$q}m={$hash}";
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
return $combinedURL;
|
|
|
|
}
|
2013-06-04 14:47:44 +02:00
|
|
|
|
2015-11-26 03:19:43 +01:00
|
|
|
/**
|
|
|
|
* Given a filename and list of files, generate a new filename unique to these files
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param array $files
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function hashedCombinedFilename($combinedFile, $fileList) {
|
|
|
|
$name = pathinfo($combinedFile, PATHINFO_FILENAME);
|
|
|
|
$hash = $this->hashOfFiles($fileList);
|
|
|
|
$extension = File::get_file_extension($combinedFile);
|
|
|
|
return $name . '-' . substr($hash, 0, 7) . '.' . $extension;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Check if combined files are enabled
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-10-23 02:51:26 +02:00
|
|
|
public function getCombinedFilesEnabled() {
|
2015-10-12 06:34:34 +02:00
|
|
|
if(!$this->combinedFilesEnabled) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-25 06:15:43 +02:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
// Tests should be combined
|
|
|
|
if(class_exists('SapphireTest', false) && SapphireTest::is_running_test()) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-27 12:31:18 +02:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
// Check if specified via querystring
|
|
|
|
if(isset($_REQUEST['combine'])) {
|
|
|
|
return true;
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
// Non-dev sites are always combined
|
|
|
|
if(!Director::isDev()) {
|
|
|
|
return true;
|
2008-07-18 05:47:17 +02:00
|
|
|
}
|
2008-11-18 02:48:37 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
// Fallback to default
|
|
|
|
return Config::inst()->get(__CLASS__, 'combine_in_dev');
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|
|
|
|
|
2015-04-29 10:17:08 +02:00
|
|
|
/**
|
2015-10-12 06:34:34 +02:00
|
|
|
* For a given filelist, determine some discriminating value to determine if
|
|
|
|
* any of these files have changed.
|
2015-07-27 12:31:18 +02:00
|
|
|
*
|
2015-10-12 06:34:34 +02:00
|
|
|
* @param array $fileList List of files
|
2015-11-26 03:19:43 +01:00
|
|
|
* @return string SHA1 bashed file hash
|
2015-04-29 10:17:08 +02:00
|
|
|
*/
|
2015-11-26 03:19:43 +01:00
|
|
|
protected function hashOfFiles($fileList) {
|
|
|
|
// Get hash based on hash of each file
|
2015-10-12 06:34:34 +02:00
|
|
|
$base = Director::baseFolder() . '/';
|
2015-11-26 03:19:43 +01:00
|
|
|
$hash = '';
|
2015-10-12 06:34:34 +02:00
|
|
|
foreach($fileList as $file) {
|
|
|
|
if(file_exists($base . $file)) {
|
2015-11-26 03:19:43 +01:00
|
|
|
$hash .= sha1_file($base . $file);
|
2015-10-12 06:34:34 +02:00
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException("Combined file {$file} does not exist");
|
|
|
|
}
|
2013-06-04 14:47:44 +02:00
|
|
|
}
|
2015-11-26 03:19:43 +01:00
|
|
|
return sha1($hash);
|
2013-06-04 14:47:44 +02:00
|
|
|
}
|
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2015-04-29 10:17:08 +02:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param string $name The name of the file - eg '/css/File.css' would have the name 'File'
|
|
|
|
* @param string $module The module to fall back to if the css file does not exist in the
|
|
|
|
* current theme.
|
|
|
|
* @param string $media Comma-separated list of media types to use in the link tag
|
|
|
|
* (e.g. 'screen,projector')
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2011-03-22 15:02:11 +01:00
|
|
|
public function themedCSS($name, $module = null, $media = null) {
|
2013-10-25 23:42:01 +02:00
|
|
|
$theme = SSViewer::get_theme_folder();
|
|
|
|
$project = project();
|
|
|
|
$absbase = BASE_PATH . DIRECTORY_SEPARATOR;
|
|
|
|
$abstheme = $absbase . $theme;
|
|
|
|
$absproject = $absbase . $project;
|
2012-06-22 03:34:03 +02:00
|
|
|
$css = "/css/$name.css";
|
2015-07-27 12:31:18 +02:00
|
|
|
|
2013-10-25 23:42:01 +02:00
|
|
|
if(file_exists($absproject . $css)) {
|
|
|
|
$this->css($project . $css, $media);
|
|
|
|
} elseif($module && file_exists($abstheme . '_' . $module.$css)) {
|
|
|
|
$this->css($theme . '_' . $module . $css, $media);
|
|
|
|
} elseif(file_exists($abstheme . $css)) {
|
|
|
|
$this->css($theme . $css, $media);
|
|
|
|
} elseif($module) {
|
|
|
|
$this->css($module . $css, $media);
|
2011-03-22 15:02:11 +01:00
|
|
|
}
|
2008-11-07 03:06:28 +01:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2015-04-29 10:17:08 +02:00
|
|
|
/**
|
|
|
|
* Output debugging information.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function debug() {
|
2008-11-07 03:06:28 +01:00
|
|
|
Debug::show($this->javascript);
|
|
|
|
Debug::show($this->css);
|
|
|
|
Debug::show($this->customCSS);
|
|
|
|
Debug::show($this->customScript);
|
|
|
|
Debug::show($this->customHeadTags);
|
2015-10-12 06:34:34 +02:00
|
|
|
Debug::show($this->combinedFiles);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-12-12 12:00:52 +01:00
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
}
|
2015-10-23 02:51:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides an abstract interface for minifying content
|
|
|
|
*/
|
|
|
|
interface Requirements_Minifier {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Minify the given content
|
|
|
|
*
|
|
|
|
* @param string $content
|
|
|
|
* @param string $type Either js or css
|
|
|
|
* @param string $filename Name of file to display in case of error
|
|
|
|
* @return string minified content
|
|
|
|
*/
|
|
|
|
public function minify($content, $type, $filename);
|
2015-12-22 01:56:05 +01:00
|
|
|
}
|