2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-11-07 03:06:28 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Requirements tracker, for javascript and css.
|
|
|
|
* @todo Document the requirements tracker, and discuss it with the others.
|
2010-04-23 02:11:41 +02: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
|
|
|
*/
|
|
|
|
class Requirements {
|
2009-04-29 01:52:15 +02:00
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
/**
|
|
|
|
* Enable combining of css/javascript files.
|
2010-05-25 06:13:45 +02:00
|
|
|
* @param boolean $enable
|
2009-03-04 04:44:11 +01:00
|
|
|
*/
|
|
|
|
public static function set_combined_files_enabled($enable) {
|
2010-05-25 06:13:45 +02:00
|
|
|
self::backend()->set_combined_files_enabled($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.
|
|
|
|
* @return boolean
|
2009-04-29 01:52:15 +02:00
|
|
|
*/
|
2010-05-25 06:13:45 +02:00
|
|
|
public static function get_combined_files_enabled() {
|
2012-12-08 12:20:20 +01:00
|
|
|
return self::backend()->get_combined_files_enabled();
|
2010-05-25 06:13:45 +02:00
|
|
|
}
|
2010-05-25 06:15:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the relative folder e.g. "assets" for where to store combined files
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* Set whether we want to suffix requirements with the time /
|
|
|
|
* location on to the requirements
|
|
|
|
*
|
|
|
|
* @param bool
|
|
|
|
*/
|
|
|
|
public static function set_suffix_requirements($var) {
|
2010-05-25 06:13:45 +02:00
|
|
|
self::backend()->set_suffix_requirements($var);
|
2009-04-29 01:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether we want to suffix requirements
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function get_suffix_requirements() {
|
2010-05-25 06:13:45 +02:00
|
|
|
return self::backend()->get_suffix_requirements();
|
2009-04-29 01:52:15 +02:00
|
|
|
}
|
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
|
|
|
* Instance of requirements for storage
|
|
|
|
*
|
|
|
|
* @var Requirements
|
|
|
|
*/
|
|
|
|
private static $backend = null;
|
2010-05-25 06:13:45 +02:00
|
|
|
|
2008-11-10 02:13:42 +01:00
|
|
|
public static function backend() {
|
2008-11-07 03:06:28 +01:00
|
|
|
if(!self::$backend) {
|
|
|
|
self::$backend = new Requirements_Backend();
|
|
|
|
}
|
|
|
|
return self::$backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setter method for changing the Requirements backend
|
|
|
|
*
|
|
|
|
* @param Requirements $backend
|
|
|
|
*/
|
|
|
|
public static function set_backend(Requirements_Backend $backend) {
|
|
|
|
self::$backend = $backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the given javascript file as required.
|
|
|
|
*
|
|
|
|
* See {@link Requirements_Backend::javascript()} for more info
|
|
|
|
*
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function javascript($file) {
|
2008-11-07 03:06:28 +01:00
|
|
|
self::backend()->javascript($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the javascript code to the header of the page
|
|
|
|
*
|
|
|
|
* See {@link Requirements_Backend::customScript()} for more info
|
|
|
|
* @param script The script content
|
|
|
|
* @param uniquenessID Use this to ensure that pieces of code only get added once.
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-02 00:49:53 +01:00
|
|
|
* Include custom CSS styling to the header of the page.
|
2008-11-07 03:06:28 +01:00
|
|
|
*
|
|
|
|
* See {@link Requirements_Backend::customCSS()}
|
2009-02-02 00:49:53 +01:00
|
|
|
*
|
|
|
|
* @param string $script CSS selectors as a string (without <style> tag enclosing selectors).
|
|
|
|
* @param int $uniquenessID Group CSS by a unique ID as to avoid duplicate custom CSS in header
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the following custom code to the <head> section of the page.
|
|
|
|
* See {@link Requirements_Backend::insertHeadTags()}
|
|
|
|
*
|
|
|
|
* @param string $html
|
|
|
|
* @param string $uniquenessID
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the given javascript template with the page.
|
|
|
|
* See {@link Requirements_Backend::javascriptTemplate()}
|
|
|
|
*
|
|
|
|
* @param file The template file to load.
|
|
|
|
* @param vars The array of variables to load. These variables are loaded via string search & replace.
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the given stylesheet file as required.
|
|
|
|
* See {@link Requirements_Backend::css()}
|
|
|
|
*
|
2012-03-24 04:38:57 +01:00
|
|
|
* @param $file String Filenames should be relative to the base, eg, 'framework/javascript/tree/tree.css'
|
2008-11-07 03:06:28 +01:00
|
|
|
* @param $media String Comma-separated list of media-types (e.g. "screen,projector")
|
|
|
|
* @see http://www.w3.org/TR/REC-CSS2/media.html
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-22 15:02:11 +01: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.
|
|
|
|
*
|
|
|
|
* NOTE: This API is experimental and may change in the future.
|
|
|
|
*
|
|
|
|
* @param string $name The name of the file - e.g. "/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 The CSS media attribute.
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Clear either a single or all requirements.
|
|
|
|
* Caution: Clearing single rules works only with customCSS and customScript if you specified a {@uniquenessID}.
|
|
|
|
*
|
|
|
|
* See {@link Requirements_Backend::clear()}
|
|
|
|
*
|
|
|
|
* @param $file String
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Blocks inclusion of a specific file
|
|
|
|
* See {@link Requirements_Backend::block()}
|
|
|
|
*
|
|
|
|
* @param unknown_type $fileOrID
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes an item from the blocking-list.
|
|
|
|
* See {@link Requirements_Backend::unblock()}
|
|
|
|
*
|
|
|
|
* @param string $fileOrID
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all items from the blocking-list.
|
|
|
|
* See {@link Requirements_Backend::unblock_all()}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function unblock_all() {
|
2008-11-07 03:06:28 +01:00
|
|
|
self::backend()->unblock_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore requirements cleared by call to Requirements::clear
|
|
|
|
* See {@link Requirements_Backend::restore()}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function restore() {
|
2008-11-07 03:06:28 +01:00
|
|
|
self::backend()->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the given HTML content with the appropriate include tags for the registered
|
|
|
|
* requirements.
|
|
|
|
* See {@link Requirements_Backend::includeInHTML()} for more information.
|
|
|
|
*
|
|
|
|
* @param string $templateFilePath Absolute path for the *.ss template file
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param string $content HTML content that has already been parsed from the $templateFilePath
|
|
|
|
* through {@link SSViewer}.
|
2008-11-07 03:06:28 +01:00
|
|
|
* @return string HTML content thats augumented with the requirements before the closing <head> tag.
|
|
|
|
*/
|
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-09-19 12:07:39 +02:00
|
|
|
public static function include_in_response(SS_HTTPResponse $response) {
|
2008-11-12 05:31:33 +01:00
|
|
|
return self::backend()->include_in_response($response);
|
|
|
|
}
|
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2009-03-10 23:08:52 +01:00
|
|
|
* Add i18n files from the given javascript directory.
|
2012-01-06 12:00:04 +01:00
|
|
|
*
|
|
|
|
* @param String
|
|
|
|
* @param Boolean
|
|
|
|
* @param Boolean
|
2008-11-07 03:06:28 +01:00
|
|
|
*
|
2009-03-10 23:08:52 +01:00
|
|
|
* See {@link Requirements_Backend::add_i18n_javascript()} for more information.
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Concatenate several css or javascript files into a single dynamically generated file.
|
|
|
|
* See {@link Requirements_Backend::combine_files()} for more info.
|
|
|
|
*
|
|
|
|
* @param string $combinedFileName
|
|
|
|
* @param array $files
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function combine_files($combinedFileName, $files) {
|
2008-11-07 03:06:28 +01:00
|
|
|
self::backend()->combine_files($combinedFileName, $files);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all combined files.
|
|
|
|
* See {@link Requirements_Backend::get_combine_files()}
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_combine_files() {
|
2008-11-07 03:06:28 +01:00
|
|
|
return self::backend()->get_combine_files();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes all dynamically generated combined files from the filesystem.
|
|
|
|
* See {@link Requirements_Backend::delete_combine_files()}
|
|
|
|
*
|
|
|
|
* @param string $combinedFileName If left blank, all combined files are deleted.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function delete_combined_files($combinedFileName = null) {
|
2008-11-07 03:06:28 +01:00
|
|
|
return self::backend()->delete_combined_files($combinedFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
2008-11-07 03:06:28 +01:00
|
|
|
self::backend()->clear_combined_files();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See {@link combine_files()}.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function process_combined_files() {
|
2008-11-07 03:06:28 +01:00
|
|
|
return self::backend()->process_combined_files();
|
|
|
|
}
|
2008-07-18 01:32:31 +02:00
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
/**
|
2008-11-07 03:06:28 +01:00
|
|
|
* Returns all custom scripts
|
|
|
|
* See {@link Requirements_Backend::get_custom_scripts()}
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_custom_scripts() {
|
2008-11-07 03:06:28 +01:00
|
|
|
return self::backend()->get_custom_scripts();
|
|
|
|
}
|
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* Set whether you want to write the JS to the body of the page or
|
|
|
|
* in the head section
|
|
|
|
*
|
2009-02-02 00:49:53 +01:00
|
|
|
* @see Requirements_Backend::set_write_js_to_body()
|
2008-12-04 23:38:32 +01:00
|
|
|
* @param boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_write_js_to_body($var) {
|
2008-12-04 23:38:32 +01:00
|
|
|
self::backend()->set_write_js_to_body($var);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2008-11-07 03:06:28 +01:00
|
|
|
class Requirements_Backend {
|
2010-05-25 06:13:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do we want requirements to suffix onto the requirement link
|
|
|
|
* tags for caching or is it disabled. Getter / Setter available
|
|
|
|
* through {@link Requirements::set_suffix_requirements()}
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $suffix_requirements = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable combining of css/javascript files.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $combined_files_enabled = true;
|
|
|
|
|
|
|
|
/**
|
2008-07-18 05:47:17 +02:00
|
|
|
* Paths to all required .js files relative to the webroot.
|
2010-05-25 06:13:45 +02:00
|
|
|
*
|
2008-07-18 05:47:17 +02:00
|
|
|
* @var array $javascript
|
|
|
|
*/
|
2008-11-07 03:06:28 +01:00
|
|
|
protected $javascript = array();
|
2008-07-18 01:32:31 +02:00
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
/**
|
|
|
|
* Paths to all required .css files relative to the webroot.
|
2010-05-25 06:13:45 +02:00
|
|
|
*
|
2008-09-22 18:02:33 +02:00
|
|
|
* @var array $css
|
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
|
|
|
/**
|
|
|
|
* All custom javascript code that is inserted
|
|
|
|
* directly at the bottom of the HTML <head> tag.
|
|
|
|
*
|
|
|
|
* @var array $customScript
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* All custom CSS rules which are inserted
|
2010-05-25 06:13:45 +02:00
|
|
|
* directly at the bottom of the HTML <head> tag.
|
2008-07-18 05:47:17 +02:00
|
|
|
*
|
|
|
|
* @var array $customCSS
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* All custom HTML markup which is added before
|
|
|
|
* the closing <head> tag, e.g. additional metatags.
|
2010-05-25 06:13:45 +02:00
|
|
|
* This is preferred to entering tags directly into
|
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
|
|
|
/**
|
|
|
|
* Remembers the filepaths of all cleared Requirements
|
|
|
|
* through {@link clear()}.
|
|
|
|
*
|
|
|
|
* @var array $disabled
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* The filepaths (relative to webroot) or
|
|
|
|
* uniquenessIDs of any included requirements
|
|
|
|
* which should be blocked when executing {@link inlcudeInHTML()}.
|
|
|
|
* This is useful to e.g. prevent core classes to modifying
|
|
|
|
* Requirements without subclassing the entire functionality.
|
|
|
|
* Use {@link unblock()} or {@link unblock_all()} to revert changes.
|
2010-05-25 06:13:45 +02:00
|
|
|
*
|
2008-07-18 05:47:17 +02:00
|
|
|
* @var array $blocked
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* See {@link combine_files()}.
|
2010-05-25 06:13:45 +02:00
|
|
|
*
|
2008-07-18 05:47:17 +02:00
|
|
|
* @var array $combine_files
|
2008-07-18 01:32:31 +02:00
|
|
|
*/
|
2008-11-07 03:06:28 +01:00
|
|
|
public $combine_files = array();
|
2010-05-25 06:13:45 +02:00
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
/**
|
|
|
|
* Using the JSMin library to minify any
|
|
|
|
* javascript file passed to {@link combine_files()}.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2008-11-07 03:06:28 +01:00
|
|
|
public $combine_js_with_jsmin = true;
|
2010-05-25 06:13:45 +02:00
|
|
|
|
2010-05-25 06:17:05 +02:00
|
|
|
/**
|
2010-10-15 04:37:41 +02:00
|
|
|
* @var string By default, combined files are stored in assets/_combinedfiles.
|
2010-05-25 06:17:05 +02:00
|
|
|
* Set this by calling Requirements::set_combined_files_folder()
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Put all javascript includes at the bottom of the template
|
|
|
|
* before the closing <body> tag instead of the <head> tag.
|
|
|
|
* This means script downloads won't block other HTTP-requests,
|
|
|
|
* which can be a performance improvement.
|
|
|
|
* Caution: Doesn't work when modifying the DOM from those external
|
|
|
|
* scripts without listening to window.onload/document.ready
|
|
|
|
* (e.g. toplevel document.write() calls).
|
|
|
|
*
|
|
|
|
* @see http://developer.yahoo.com/performance/rules.html#js_bottom
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2008-11-12 05:31:33 +01:00
|
|
|
public $write_js_to_body = true;
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function set_combined_files_enabled($enable) {
|
2010-05-25 06:15:43 +02:00
|
|
|
$this->combined_files_enabled = (bool) $enable;
|
2010-05-25 06:13:45 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function get_combined_files_enabled() {
|
2010-05-25 06:15:43 +02:00
|
|
|
return $this->combined_files_enabled;
|
|
|
|
}
|
|
|
|
|
2010-10-15 04:37:41 +02:00
|
|
|
/**
|
|
|
|
* @param String $folder
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2010-10-15 04:37:41 +02:00
|
|
|
/**
|
|
|
|
* @return String Folder relative to the webroot
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCombinedFilesFolder() {
|
2010-10-15 04:37:41 +02:00
|
|
|
return ($this->combinedFilesFolder) ? $this->combinedFilesFolder : ASSETS_DIR . '/_combinedfiles';
|
|
|
|
}
|
|
|
|
|
2010-05-25 06:13:45 +02:00
|
|
|
/**
|
|
|
|
* Set whether we want to suffix requirements with the time /
|
|
|
|
* location on to the requirements
|
|
|
|
*
|
|
|
|
* @param bool
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function set_suffix_requirements($var) {
|
2010-05-25 06:13:45 +02:00
|
|
|
$this->suffix_requirements = $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether we want to suffix requirements
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function get_suffix_requirements() {
|
2010-05-25 06:13:45 +02:00
|
|
|
return $this->suffix_requirements;
|
|
|
|
}
|
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* Set whether you want the files written to the head or the body. It
|
|
|
|
* writes to the body by default which can break some scripts
|
|
|
|
*
|
|
|
|
* @param boolean
|
|
|
|
*/
|
|
|
|
public function set_write_js_to_body($var) {
|
|
|
|
$this->write_js_to_body = $var;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Register the given javascript file as required.
|
2012-03-24 04:38:57 +01:00
|
|
|
* Filenames should be relative to the base, eg, 'framework/javascript/loader.js'
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-11-07 03:06:28 +01:00
|
|
|
|
|
|
|
public function javascript($file) {
|
|
|
|
$this->javascript[$file] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of all included javascript
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_javascript() {
|
|
|
|
return array_keys(array_diff_key($this->javascript,$this->blocked));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the javascript code to the header of the page
|
|
|
|
* @todo Make Requirements automatically put this into a separate file :-)
|
|
|
|
* @param script The script content
|
|
|
|
* @param uniquenessID Use this to ensure that pieces of code only get added once.
|
|
|
|
*/
|
2008-11-07 03:06:28 +01:00
|
|
|
public function customScript($script, $uniquenessID = null) {
|
2009-02-16 22:10:03 +01:00
|
|
|
if($uniquenessID) $this->customScript[$uniquenessID] = $script;
|
|
|
|
else $this->customScript[] = $script;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$script .= "\n";
|
|
|
|
}
|
2008-11-07 03:06:28 +01:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
/**
|
|
|
|
* Include custom CSS styling to the header of the page.
|
|
|
|
*
|
|
|
|
* @param string $script CSS selectors as a string (without <style> tag enclosing selectors).
|
|
|
|
* @param int $uniquenessID Group CSS by a unique ID as to avoid duplicate custom CSS in header
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function customCSS($script, $uniquenessID = null) {
|
2009-02-16 22:10:03 +01:00
|
|
|
if($uniquenessID) $this->customCSS[$uniquenessID] = $script;
|
|
|
|
else $this->customCSS[] = $script;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-07-18 05:47:17 +02:00
|
|
|
* Add the following custom code to the <head> section of the page.
|
|
|
|
*
|
|
|
|
* @param string $html
|
|
|
|
* @param string $uniquenessID
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function insertHeadTags($html, $uniquenessID = null) {
|
2009-02-16 22:10:03 +01:00
|
|
|
if($uniquenessID) $this->customHeadTags[$uniquenessID] = $html;
|
|
|
|
else $this->customHeadTags[] = $html;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the given javascript template with the page.
|
|
|
|
* @param file The template file to load.
|
|
|
|
* @param vars The array of variables to load. These variables are loaded via string search & replace.
|
|
|
|
*/
|
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));
|
|
|
|
}
|
2009-02-16 22:08:54 +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
|
|
|
}
|
2008-11-07 03:06:28 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Register the given stylesheet file as required.
|
|
|
|
*
|
2012-03-24 04:38:57 +01:00
|
|
|
* @param $file String Filenames should be relative to the base, eg, 'framework/javascript/tree/tree.css'
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param $media String Comma-separated list of media-types (e.g. "screen,projector")
|
|
|
|
* @see http://www.w3.org/TR/REC-CSS2/media.html
|
|
|
|
*/
|
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-09-19 12:07:39 +02:00
|
|
|
public function get_css() {
|
2009-02-02 00:49:53 +01:00
|
|
|
return array_diff_key($this->css, $this->blocked);
|
2008-11-07 03:06:28 +01:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-11-07 03:06:28 +01:00
|
|
|
* Needed to actively prevent the inclusion of a file,
|
|
|
|
* e.g. when using your own prototype.js.
|
|
|
|
* Blocking should only be used as an exception, because
|
|
|
|
* it is hard to trace back. You can just block items with an
|
|
|
|
* ID, so make sure you add an unique identifier to customCSS() and customScript().
|
2007-07-19 12:40:28 +02:00
|
|
|
*
|
2008-11-07 03:06:28 +01:00
|
|
|
* @param string $fileOrID
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function block($fileOrID) {
|
2008-11-07 03:06:28 +01:00
|
|
|
$this->blocked[$fileOrID] = $fileOrID;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear either a single or all requirements.
|
|
|
|
* Caution: Clearing single rules works only with customCSS and customScript if you specified a {@uniquenessID}.
|
|
|
|
*
|
|
|
|
* @param $file String
|
|
|
|
*/
|
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;
|
2007-07-19 12:40:28 +02: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Removes an item from the blocking-list.
|
|
|
|
* CAUTION: Does not "re-add" any previously blocked elements.
|
|
|
|
* @param string $fileOrID
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function unblock($fileOrID) {
|
2008-11-07 03:06:28 +01:00
|
|
|
if(isset($this->blocked[$fileOrID])) unset($this->blocked[$fileOrID]);
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Removes all items from the blocking-list.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function unblock_all() {
|
2009-03-29 03:31:44 +02:00
|
|
|
$this->blocked = array();
|
2008-02-25 03:10:37 +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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
* including a <head> tag. The requirements will insert before the closing <head> tag automatically.
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @todo Calculate $prefix properly
|
2008-07-18 01:32:31 +02:00
|
|
|
*
|
|
|
|
* @param string $templateFilePath Absolute path for the *.ss template file
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param string $content HTML content that has already been parsed from the $templateFilePath
|
|
|
|
* through {@link SSViewer}.
|
2008-07-18 01:32:31 +02:00
|
|
|
* @return string HTML content thats augumented with the requirements before the closing <head> tag.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function includeInHTML($templateFile, $content) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($_GET['debug_profile'])) Profiler::mark("Requirements::includeInHTML");
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if((strpos($content, '</head>') !== false || strpos($content, '</head ') !== false)
|
|
|
|
&& ($this->css||$this->javascript||$this->customCSS||$this->customScript||$this->customHeadTags)) {
|
2008-08-11 04:25:44 +02:00
|
|
|
$requirements = '';
|
|
|
|
$jsRequirements = '';
|
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
// Combine files - updates $this->javascript and $this->css
|
2012-12-08 12:20:20 +01:00
|
|
|
$this->process_combined_files();
|
2008-08-11 04:25:44 +02:00
|
|
|
|
2010-05-25 06:15:43 +02:00
|
|
|
foreach(array_diff_key($this->javascript,$this->blocked) as $file => $dummy) {
|
2010-05-25 06:13:45 +02:00
|
|
|
$path = $this->path_for_file($file);
|
2008-08-11 09:22:50 +02:00
|
|
|
if($path) {
|
|
|
|
$jsRequirements .= "<script type=\"text/javascript\" src=\"$path\"></script>\n";
|
2008-08-11 04:25:44 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-11 07:29:41 +02:00
|
|
|
|
|
|
|
// add all inline javascript *after* including external files which
|
|
|
|
// they might rely on
|
2008-11-07 03:06:28 +01:00
|
|
|
if($this->customScript) {
|
|
|
|
foreach(array_diff_key($this->customScript,$this->blocked) as $script) {
|
2008-08-11 04:25:44 +02:00
|
|
|
$jsRequirements .= "<script type=\"text/javascript\">\n//<![CDATA[\n";
|
|
|
|
$jsRequirements .= "$script\n";
|
|
|
|
$jsRequirements .= "\n//]]>\n</script>\n";
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-11 04:25:44 +02:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
foreach(array_diff_key($this->css,$this->blocked) as $file => $params) {
|
2010-05-25 06:13:45 +02:00
|
|
|
$path = $this->path_for_file($file);
|
2008-08-11 09:22:50 +02:00
|
|
|
if($path) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$media = (isset($params['media']) && !empty($params['media']))
|
|
|
|
? " media=\"{$params['media']}\"" : "";
|
2008-08-11 09:22:50 +02:00
|
|
|
$requirements .= "<link rel=\"stylesheet\" type=\"text/css\"{$media} href=\"$path\" />\n";
|
2008-08-11 04:25:44 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-02-08 20:06:15 +01:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
foreach(array_diff_key($this->customCSS, $this->blocked) as $css) {
|
2008-08-11 04:25:44 +02:00
|
|
|
$requirements .= "<style type=\"text/css\">\n$css\n</style>\n";
|
|
|
|
}
|
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
foreach(array_diff_key($this->customHeadTags,$this->blocked) as $customHeadTag) {
|
2008-08-11 04:25:44 +02:00
|
|
|
$requirements .= "$customHeadTag\n";
|
|
|
|
}
|
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
if($this->write_js_to_body) {
|
2008-08-11 07:29:41 +02:00
|
|
|
// Remove all newlines from code to preserve layout
|
|
|
|
$jsRequirements = preg_replace('/>\n*/', '>', $jsRequirements);
|
|
|
|
|
|
|
|
// We put script tags into the body, for performance.
|
2009-05-06 08:36:16 +02:00
|
|
|
// If your template already has script tags in the body, then we put our script
|
|
|
|
// tags just before those. Otherwise, we put it at the bottom.
|
2008-08-11 07:29:41 +02:00
|
|
|
$p1 = strripos($content, '<script');
|
|
|
|
$p2 = stripos($content, '<body');
|
|
|
|
if($p1 !== false && $p1 > $p2) {
|
2009-05-06 08:36:16 +02:00
|
|
|
$content = substr($content,0,$p1) . $jsRequirements . substr($content,$p1);
|
2008-08-11 07:29:41 +02:00
|
|
|
} else {
|
2009-08-11 10:54:57 +02:00
|
|
|
$content = preg_replace("/(<\/body[^>]*>)/i", $jsRequirements . "\\1", $content);
|
2008-08-11 07:29:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put CSS at the bottom of the head
|
2010-10-15 04:49:52 +02:00
|
|
|
$content = preg_replace("/(<\/head>)/i", $requirements . "\\1", $content);
|
2008-08-11 04:25:44 +02:00
|
|
|
} else {
|
2010-10-15 04:49:52 +02:00
|
|
|
$content = preg_replace("/(<\/head>)/i", $requirements . "\\1", $content);
|
|
|
|
$content = preg_replace("/(<\/head>)/i", $jsRequirements . "\\1", $content);
|
2008-08-11 04:25:44 +02:00
|
|
|
}
|
2008-08-11 07:29:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($_GET['debug_profile'])) Profiler::unmark("Requirements::includeInHTML");
|
|
|
|
|
|
|
|
return $content;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-11-12 05:31:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the HTTP response
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function include_in_response(SS_HTTPResponse $response) {
|
2008-11-12 05:31:33 +01:00
|
|
|
$this->process_combined_files();
|
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
|
|
|
|
2008-11-13 02:31:31 +01:00
|
|
|
foreach(array_diff_key($this->javascript, $this->blocked) as $file => $dummy) {
|
2008-11-12 05:31:33 +01:00
|
|
|
$path = $this->path_for_file($file);
|
2012-01-06 14:09:43 +01:00
|
|
|
if($path) {
|
|
|
|
$jsRequirements[] = str_replace(',', '%2C', $path);
|
|
|
|
}
|
2008-11-12 05:31:33 +01:00
|
|
|
}
|
2010-05-25 06:18:14 +02:00
|
|
|
|
2008-11-12 05:31:33 +01:00
|
|
|
$response->addHeader('X-Include-JS', implode(',', $jsRequirements));
|
|
|
|
|
|
|
|
foreach(array_diff_key($this->css,$this->blocked) as $file => $params) {
|
|
|
|
$path = $this->path_for_file($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
|
|
|
}
|
|
|
|
|
|
|
|
$response->addHeader('X-Include-CSS', implode(',', $cssRequirements));
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-03-10 23:08:52 +01:00
|
|
|
/**
|
2012-03-24 04:38:57 +01:00
|
|
|
* Add i18n files from the given javascript directory. SilverStripe expects that the given directory
|
2009-03-10 23:08:52 +01:00
|
|
|
* will contain a number of java script files named by language: en_US.js, de_DE.js, etc.
|
2012-01-06 12:00:04 +01:00
|
|
|
*
|
2012-03-24 04:38:57 +01:00
|
|
|
* @param String The javascript lang directory, relative to the site root, e.g., 'framework/javascript/lang'
|
2012-01-06 12:00:04 +01:00
|
|
|
* @param Boolean Return all relative file paths rather than including them in requirements
|
|
|
|
* @param Boolean Only include language files, not the base libraries
|
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();
|
2009-03-10 23:08:52 +01:00
|
|
|
if(i18n::get_js_i18n()) {
|
|
|
|
// 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 .= '/';
|
2009-04-29 01:40:35 +02:00
|
|
|
|
2012-01-06 12:00:04 +01:00
|
|
|
$files[] = $langDir . i18n::default_locale() . '.js';
|
|
|
|
$files[] = $langDir . i18n::get_locale() . '.js';
|
2009-03-10 23:08:52 +01:00
|
|
|
|
|
|
|
// Stub i18n implementation for when i18n is disabled.
|
|
|
|
} else {
|
2012-03-24 04:38:57 +01:00
|
|
|
if(!$langOnly) $files[] = FRAMEWORK_DIR . '/javascript/i18nx.js';
|
2012-01-06 12:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($return) {
|
|
|
|
return $files;
|
|
|
|
} else {
|
|
|
|
foreach($files as $file) $this->javascript($file);
|
2008-10-03 02:46:07 +02:00
|
|
|
}
|
2009-03-10 23:08:52 +01:00
|
|
|
}
|
2008-10-03 02:46:07 +02:00
|
|
|
|
2008-08-11 09:22:50 +02:00
|
|
|
/**
|
2008-11-07 03:06:28 +01:00
|
|
|
* Finds the path for specified file.
|
2008-08-11 09:22:50 +02:00
|
|
|
*
|
|
|
|
* @param string $fileOrUrl
|
|
|
|
* @return string|boolean
|
|
|
|
*/
|
2010-05-25 06:13:45 +02:00
|
|
|
protected function path_for_file($fileOrUrl) {
|
2012-08-23 01:13:29 +02:00
|
|
|
if(preg_match('{^//|http[s]?}', $fileOrUrl)) {
|
2008-08-11 09:22:50 +02:00
|
|
|
return $fileOrUrl;
|
|
|
|
} elseif(Director::fileExists($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 = '';
|
|
|
|
if(strpos($fileOrUrl, '?') !== false) {
|
|
|
|
$suffix = '&' . substr($fileOrUrl, strpos($fileOrUrl, '?')+1);
|
|
|
|
$fileOrUrl = substr($fileOrUrl, 0, strpos($fileOrUrl, '?'));
|
|
|
|
}
|
2010-05-25 06:13:45 +02:00
|
|
|
if($this->suffix_requirements) {
|
2009-04-29 01:52:15 +02:00
|
|
|
$mtimesuffix = "?m=" . filemtime(Director::baseFolder() . '/' . $fileOrUrl);
|
|
|
|
}
|
2009-04-29 02:07:39 +02:00
|
|
|
return "{$prefix}{$fileOrUrl}{$mtimesuffix}{$suffix}";
|
2008-08-11 09:22:50 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
/**
|
|
|
|
* Concatenate several css or javascript files into a single dynamically generated
|
|
|
|
* file (stored in {@link Director::baseFolder()}). 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.
|
|
|
|
* If all files to be combined are javascript, we use the external JSMin library
|
|
|
|
* to minify the javascript. This can be controlled by {@link $combine_js_with_jsmin}.
|
|
|
|
*
|
|
|
|
* All combined files will have a comment on the start of each concatenated file
|
|
|
|
* denoting their original position. For easier debugging, we recommend to only
|
|
|
|
* minify javascript if not in development mode ({@link Director::isDev()}).
|
|
|
|
*
|
|
|
|
* 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 wrong styling inheritance.
|
|
|
|
* Depending on the javascript logic, you also have to ensure that files are not included
|
|
|
|
* in more than one combine_files() call.
|
2008-07-18 05:47:17 +02:00
|
|
|
* Best practice is to include every javascript file in exactly *one* combine_files()
|
|
|
|
* directive to avoid the issues mentioned above - this is enforced by this function.
|
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(
|
|
|
|
* '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>
|
|
|
|
*
|
|
|
|
* @see http://code.google.com/p/jsmin-php/
|
2008-07-18 05:47:17 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* @todo Should we enforce unique inclusion of files, or leave it to the developer? Can auto-detection cause
|
|
|
|
* breaks?
|
2008-07-18 05:47:17 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param string $combinedFileName Filename of the combined file (will be stored in {@link Director::baseFolder()}
|
|
|
|
* by default)
|
2008-07-18 01:32:31 +02:00
|
|
|
* @param array $files Array of filenames relative to the webroot
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function combine_files($combinedFileName, $files) {
|
2008-07-18 05:47:17 +02:00
|
|
|
// duplicate check
|
2008-11-07 03:06:28 +01:00
|
|
|
foreach($this->combine_files as $_combinedFileName => $_files) {
|
2008-07-18 05:47:17 +02:00
|
|
|
$duplicates = array_intersect($_files, $files);
|
2010-10-15 05:01:03 +02:00
|
|
|
if($duplicates && $combinedFileName != $_combinedFileName) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Requirements_Backend::combine_files(): Already included files " . implode(',', $duplicates)
|
|
|
|
. " in combined file '{$_combinedFileName}'", E_USER_NOTICE);
|
2008-07-18 05:47:17 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-10-15 05:09:15 +02:00
|
|
|
foreach($files as $index=>$file) {
|
|
|
|
if(is_array($file)) {
|
|
|
|
// Either associative array path=>path type=>type or numeric 0=>path 1=>type
|
|
|
|
// Otherwise, assume path is the first item
|
2012-09-26 23:34:00 +02:00
|
|
|
if (isset($file['type']) && in_array($file['type'], array('css', 'javascript', 'js'))) {
|
2010-10-15 05:09:15 +02:00
|
|
|
switch ($file['type']) {
|
|
|
|
case 'css':
|
|
|
|
$this->css($file['path']);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->javascript($file['path']);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$files[$index] = $file['path'];
|
2012-09-26 23:34:00 +02:00
|
|
|
} elseif (isset($file[1]) && in_array($file[1], array('css', 'javascript', 'js'))) {
|
2010-10-15 05:09:15 +02:00
|
|
|
switch ($file[1]) {
|
|
|
|
case 'css':
|
|
|
|
$this->css($file[0]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->javascript($file[0]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$files[$index] = $file[0];
|
|
|
|
} else {
|
|
|
|
$file = array_shift($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!is_array($file)) {
|
|
|
|
if(substr($file, -2) == 'js') {
|
|
|
|
$this->javascript($file);
|
|
|
|
} elseif(substr($file, -3) == 'css') {
|
|
|
|
$this->css($file);
|
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Requirements_Backend::combine_files(): Couldn't guess file type for file '$file', "
|
|
|
|
. "please specify by passing using an array instead.", E_USER_NOTICE);
|
2010-10-15 05:09:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-07 03:06:28 +01:00
|
|
|
$this->combine_files[$combinedFileName] = $files;
|
2008-07-18 05:47:17 +02:00
|
|
|
}
|
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
|
|
|
* Returns all combined files.
|
2008-07-18 05:47:17 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function get_combine_files() {
|
2008-11-07 03:06:28 +01:00
|
|
|
return $this->combine_files;
|
2008-07-18 05:47:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-08-12 01:18:56 +02:00
|
|
|
* Deletes all dynamically generated combined files from the filesystem.
|
2008-07-18 05:47:17 +02:00
|
|
|
*
|
|
|
|
* @param string $combinedFileName If left blank, all combined files are deleted.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function delete_combined_files($combinedFileName = null) {
|
2008-11-07 03:06:28 +01:00
|
|
|
$combinedFiles = ($combinedFileName) ? array($combinedFileName => null) : $this->combine_files;
|
2012-09-26 23:34:00 +02:00
|
|
|
$combinedFolder = ($this->getCombinedFilesFolder()) ?
|
|
|
|
(Director::baseFolder() . '/' . $this->combinedFilesFolder) : Director::baseFolder();
|
2008-07-18 05:47:17 +02:00
|
|
|
foreach($combinedFiles as $combinedFile => $sourceItems) {
|
2010-05-25 06:15:43 +02:00
|
|
|
$filePath = $combinedFolder . '/' . $combinedFile;
|
2008-07-18 05:47:17 +02:00
|
|
|
if(file_exists($filePath)) {
|
|
|
|
unlink($filePath);
|
|
|
|
}
|
|
|
|
}
|
2008-07-18 01:32:31 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function clear_combined_files() {
|
2008-11-07 03:06:28 +01:00
|
|
|
$this->combine_files = 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
|
|
|
/**
|
2008-11-07 03:06:28 +01:00
|
|
|
* See {@link combine_files()}
|
|
|
|
*
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function process_combined_files() {
|
2009-04-27 02:19:16 +02:00
|
|
|
// The class_exists call prevents us from loading SapphireTest.php (slow) just to know that
|
|
|
|
// SapphireTest isn't running :-)
|
2010-05-25 06:16:33 +02:00
|
|
|
if(class_exists('SapphireTest', false)) $runningTest = SapphireTest::is_running_test();
|
2009-04-27 02:19:16 +02:00
|
|
|
else $runningTest = false;
|
|
|
|
|
2012-06-29 08:35:55 +02:00
|
|
|
if((Director::isDev() && !$runningTest && !isset($_REQUEST['combine'])) || !$this->combined_files_enabled) {
|
2008-11-12 22:15:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-09-20 04:00:11 +02:00
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
// Make a map of files that could be potentially combined
|
|
|
|
$combinerCheck = array();
|
2008-11-07 03:06:28 +01:00
|
|
|
foreach($this->combine_files as $combinedFile => $sourceItems) {
|
2008-07-18 01:32:31 +02:00
|
|
|
foreach($sourceItems as $sourceItem) {
|
|
|
|
if(isset($combinerCheck[$sourceItem]) && $combinerCheck[$sourceItem] != $combinedFile){
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Requirements_Backend::process_combined_files - file '$sourceItem' appears in two " .
|
|
|
|
"combined files:" . " '{$combinerCheck[$sourceItem]}' and '$combinedFile'", E_USER_WARNING);
|
2008-07-18 01:32:31 +02:00
|
|
|
}
|
|
|
|
$combinerCheck[$sourceItem] = $combinedFile;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2010-05-25 06:15:43 +02:00
|
|
|
|
|
|
|
// Work out the relative URL for the combined files from the base folder
|
2010-10-15 04:37:41 +02:00
|
|
|
$combinedFilesFolder = ($this->getCombinedFilesFolder()) ? ($this->getCombinedFilesFolder() . '/') : '';
|
2010-05-25 06:15:43 +02:00
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
// Figure out which ones apply to this pageview
|
|
|
|
$combinedFiles = array();
|
|
|
|
$newJSRequirements = array();
|
|
|
|
$newCSSRequirements = array();
|
2008-11-07 03:06:28 +01:00
|
|
|
foreach($this->javascript as $file => $dummy) {
|
2008-07-18 01:32:31 +02:00
|
|
|
if(isset($combinerCheck[$file])) {
|
2010-05-25 06:15:43 +02:00
|
|
|
$newJSRequirements[$combinedFilesFolder . $combinerCheck[$file]] = true;
|
2008-07-18 01:32:31 +02:00
|
|
|
$combinedFiles[$combinerCheck[$file]] = true;
|
|
|
|
} else {
|
|
|
|
$newJSRequirements[$file] = true;
|
|
|
|
}
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
foreach($this->css as $file => $params) {
|
2008-07-18 01:32:31 +02:00
|
|
|
if(isset($combinerCheck[$file])) {
|
2010-05-25 06:15:43 +02:00
|
|
|
$newCSSRequirements[$combinedFilesFolder . $combinerCheck[$file]] = true;
|
2008-07-18 01:32:31 +02:00
|
|
|
$combinedFiles[$combinerCheck[$file]] = true;
|
|
|
|
} else {
|
|
|
|
$newCSSRequirements[$file] = $params;
|
|
|
|
}
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
// Process the combined files
|
2008-07-18 05:47:17 +02:00
|
|
|
$base = Director::baseFolder() . '/';
|
2010-05-25 06:15:43 +02:00
|
|
|
foreach(array_diff_key($combinedFiles, $this->blocked) as $combinedFile => $dummy) {
|
2008-11-07 03:06:28 +01:00
|
|
|
$fileList = $this->combine_files[$combinedFile];
|
2010-10-15 04:37:41 +02:00
|
|
|
$combinedFilePath = $base . $combinedFilesFolder . '/' . $combinedFile;
|
2008-07-18 01:32:31 +02:00
|
|
|
|
2010-10-04 06:28:48 +02:00
|
|
|
|
|
|
|
// Make the folder if necessary
|
|
|
|
if(!file_exists(dirname($combinedFilePath))) {
|
|
|
|
Filesystem::makeFolder(dirname($combinedFilePath));
|
|
|
|
}
|
2012-09-20 04:00:11 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// If the file isn't writeable, don't even bother trying to make the combined file and return (falls back
|
|
|
|
// to uncombined). Complex test because is_writable fails if the file doesn't exist yet.
|
2012-09-20 04:00:11 +02:00
|
|
|
if((file_exists($combinedFilePath) && !is_writable($combinedFilePath))
|
|
|
|
|| (!file_exists($combinedFilePath) && !is_writable(dirname($combinedFilePath)))
|
|
|
|
) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Requirements_Backend::process_combined_files(): Couldn't create '$combinedFilePath'",
|
|
|
|
E_USER_WARNING);
|
2012-09-20 04:00:11 +02:00
|
|
|
return false;
|
2010-10-04 06:28:48 +02:00
|
|
|
}
|
|
|
|
|
2012-09-20 04:00:11 +02:00
|
|
|
// Determine if we need to build the combined include
|
2010-05-25 06:18:14 +02:00
|
|
|
if(file_exists($combinedFilePath) && !isset($_GET['flush'])) {
|
2008-07-18 05:47:17 +02:00
|
|
|
// file exists, check modification date of every contained file
|
|
|
|
$srcLastMod = 0;
|
|
|
|
foreach($fileList as $file) {
|
2012-12-08 12:20:20 +01:00
|
|
|
if(file_exists($base . $file)) {
|
|
|
|
$srcLastMod = max(filemtime($base . $file), $srcLastMod);
|
|
|
|
}
|
2008-07-18 01:32:31 +02:00
|
|
|
}
|
2010-05-25 06:18:48 +02:00
|
|
|
$refresh = $srcLastMod > filemtime($combinedFilePath);
|
2008-07-18 05:47:17 +02:00
|
|
|
} else {
|
|
|
|
// file doesn't exist, or refresh was explicitly required
|
|
|
|
$refresh = true;
|
|
|
|
}
|
2008-07-18 01:32:31 +02:00
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
if(!$refresh) continue;
|
|
|
|
|
|
|
|
$combinedData = "";
|
2010-05-25 06:16:33 +02:00
|
|
|
foreach(array_diff($fileList, $this->blocked) as $file) {
|
2008-07-18 05:47:17 +02:00
|
|
|
$fileContent = file_get_contents($base . $file);
|
|
|
|
// if we have a javascript file and jsmin is enabled, minify the content
|
2009-09-18 05:15:25 +02:00
|
|
|
$isJS = stripos($file, '.js');
|
|
|
|
if($isJS && $this->combine_js_with_jsmin) {
|
2009-11-21 06:58:32 +01:00
|
|
|
require_once('thirdparty/jsmin/jsmin.php');
|
2008-11-12 22:15:58 +01:00
|
|
|
|
2009-06-28 04:36:46 +02:00
|
|
|
increase_time_limit_to();
|
2008-07-18 05:47:17 +02:00
|
|
|
$fileContent = JSMin::minify($fileContent);
|
2008-07-18 01:32:31 +02:00
|
|
|
}
|
2008-07-18 05:47:17 +02:00
|
|
|
// write a header comment for each file for easier identification and debugging
|
2009-05-18 01:15:31 +02:00
|
|
|
// also the semicolon between each file is required for jQuery to be combinable properly
|
2009-09-18 05:15:25 +02:00
|
|
|
$combinedData .= "/****** FILE: $file *****/\n" . $fileContent . "\n".($isJS ? ';' : '')."\n";
|
2008-07-18 01:32:31 +02:00
|
|
|
}
|
2010-05-25 06:15:43 +02:00
|
|
|
|
2008-11-18 02:48:37 +01:00
|
|
|
$successfulWrite = false;
|
2010-05-25 06:15:43 +02:00
|
|
|
$fh = fopen($combinedFilePath, 'wb');
|
2008-11-18 02:48:37 +01:00
|
|
|
if($fh) {
|
|
|
|
if(fwrite($fh, $combinedData) == strlen($combinedData)) $successfulWrite = true;
|
|
|
|
fclose($fh);
|
|
|
|
unset($fh);
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
|
2008-11-18 02:48:37 +01:00
|
|
|
// Unsuccessful write - just include the regular JS files, rather than the combined one
|
|
|
|
if(!$successfulWrite) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Requirements_Backend::process_combined_files(): Couldn't create '$combinedFilePath'",
|
|
|
|
E_USER_WARNING);
|
2010-10-04 06:28:48 +02:00
|
|
|
continue;
|
2008-11-18 02:48:37 +01:00
|
|
|
}
|
2008-07-18 05:47:17 +02:00
|
|
|
}
|
2008-11-18 02:48:37 +01:00
|
|
|
|
|
|
|
// @todo Alters the original information, which means you can't call this
|
|
|
|
// method repeatedly - it will behave different on the second call!
|
|
|
|
$this->javascript = $newJSRequirements;
|
|
|
|
$this->css = $newCSSRequirements;
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function get_custom_scripts() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$requirements = "";
|
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
if($this->customScript) {
|
|
|
|
foreach($this->customScript as $script) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$requirements .= "$script\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $requirements;
|
|
|
|
}
|
2011-03-22 15:02:11 +01:00
|
|
|
|
2008-11-07 03:06:28 +01:00
|
|
|
/**
|
2011-03-22 15:02:11 +01:00
|
|
|
* @see Requirements::themedCSS()
|
2008-11-07 03:06:28 +01:00
|
|
|
*/
|
2011-03-22 15:02:11 +01:00
|
|
|
public function themedCSS($name, $module = null, $media = null) {
|
2012-06-22 03:34:03 +02:00
|
|
|
$path = SSViewer::get_theme_folder();
|
|
|
|
$abspath = BASE_PATH . DIRECTORY_SEPARATOR . $path;
|
|
|
|
$css = "/css/$name.css";
|
2008-11-07 03:06:28 +01:00
|
|
|
|
2012-06-22 03:34:03 +02:00
|
|
|
if ($module && file_exists($abspath.'_'.$module.$css)) {
|
|
|
|
$this->css($path.'_'.$module.$css, $media);
|
2011-03-22 15:02:11 +01:00
|
|
|
}
|
2012-06-22 03:34:03 +02:00
|
|
|
else if (file_exists($abspath.$css)) {
|
|
|
|
$this->css($path.$css, $media);
|
|
|
|
}
|
|
|
|
else if ($module) {
|
2013-02-01 10:22:58 +01:00
|
|
|
$this->css($module.$css, $media);
|
2011-03-22 15:02:11 +01:00
|
|
|
}
|
2008-11-07 03:06:28 +01:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
Debug::show($this->combine_files);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-11-07 03:06:28 +01:00
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
}
|