ARCHITECTURE #3034 wakeless: Make Requirements mockable by pushing the meat of the functionality to Requirements_Backend

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@65436 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2008-11-07 02:06:28 +00:00
parent bfc7c7717b
commit e6c4d32dbc
3 changed files with 428 additions and 152 deletions

View File

@ -380,7 +380,7 @@ class ManifestBuilder {
$extends = ""; $extends = "";
$class=""; $class="";
if(!$file) die("Couldn't open $filename<br />"); if(!$file) die("ManifestBuilder::parse_file(): Couldn't open $filename", E_USER_ERROR);
// We cache the parse results of each file, since only a few files will have changed between flushings // We cache the parse results of each file, since only a few files will have changed between flushings
// And, although it's accurate, TokenisedRegularExpression isn't particularly fast // And, although it's accurate, TokenisedRegularExpression isn't particularly fast

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* Requirements tracker, for javascript and css. * Requirements tracker, for javascript and css.
* @todo Document the requirements tracker, and discuss it with the others. * @todo Document the requirements tracker, and discuss it with the others.
@ -6,20 +7,258 @@
* @subpackage view * @subpackage view
*/ */
class Requirements { class Requirements {
/**
* Instance of requirements for storage
*
* @var Requirements
*/
private static $backend = null;
protected static function backend() {
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
*
*/
static function javascript($file) {
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.
*/
static function customScript($script, $uniquenessID = null) {
self::backend()->customScript($script, $uniquenessID);
}
/** /**
* Add the CSS styling to the header of the page
*
* See {@link Requirements_Backend::customCSS()}
*/
static function customCSS($script, $uniquenessID = null) {
self::backend()->custom($script, $uniquenessID);
}
/**
* Add the following custom code to the <head> section of the page.
* See {@link Requirements_Backend::insertHeadTags()}
*
* @param string $html
* @param string $uniquenessID
*/
static function insertHeadTags($html, $uniquenessID = null) {
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.
*/
static function javascriptTemplate($file, $vars, $uniquenessID = null) {
self::backend()->javascriptTemplate($file, $vars, $uniquenessID);
}
/**
* Register the given stylesheet file as required.
* See {@link Requirements_Backend::css()}
*
* @param $file String Filenames should be relative to the base, eg, 'jsparty/tree/tree.css'
* @param $media String Comma-separated list of media-types (e.g. "screen,projector")
* @see http://www.w3.org/TR/REC-CSS2/media.html
*/
static function css($file, $media = null) {
self::backend()->css($file, $media);
}
/**
* Register the given "themeable stylesheet" as required. See {@link Requirements_Backend::themedCSS()}
*
* @param $name String The identifier of the file. For example, css/MyFile.css would have the identifier "MyFile"
* @param $media String Comma-separated list of media-types (e.g. "screen,projector")
*/
static function themedCSS($name, $media = null) {
return self::backend()->themedCSS($name, $media);
}
/**
* 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
*/
static function clear($fileOrID = null) {
self::backend()->clear($fileOrID);
}
/**
* Blocks inclusion of a specific file
* See {@link Requirements_Backend::block()}
*
* @param unknown_type $fileOrID
*/
static function block($fileOrID) {
self::backend()->block($fileOrID);
}
/**
* Removes an item from the blocking-list.
* See {@link Requirements_Backend::unblock()}
*
* @param string $fileOrID
*/
static function unblock($fileOrID) {
self::backend()->unblock($fileOrID);
}
/**
* Removes all items from the blocking-list.
* See {@link Requirements_Backend::unblock_all()}
*/
static function unblock_all() {
self::backend()->unblock_all();
}
/**
* Restore requirements cleared by call to Requirements::clear
* See {@link Requirements_Backend::restore()}
*/
static function restore() {
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
* @param string $content HTML content that has already been parsed from the $templateFilePath through {@link SSViewer}.
* @return string HTML content thats augumented with the requirements before the closing <head> tag.
*/
static function includeInHTML($templateFile, $content) {
return self::backend()->includeInHTML($templateFile, $content);
}
/**
* Automatically includes the necessary lang-files from the module.
*
* See {@link Requirements_Backend::process_i18n_javascript()} for more info.
*/
protected static function process_i18n_javascript() {
return self::backend()->process_i18n_javascript();
}
/**
* 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
*/
static function combine_files($combinedFileName, $files) {
self::backend()->combine_files($combinedFileName, $files);
}
/**
* Returns all combined files.
* See {@link Requirements_Backend::get_combine_files()}
*
* @return array
*/
static function get_combine_files() {
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.
*/
static function delete_combined_files($combinedFileName = null) {
return self::backend()->delete_combined_files($combinedFileName);
}
/**
* Re-sets the combined files definition. See {@link Requirements_Backend::clear_combined_files()}
*/
static function clear_combined_files() {
self::backend()->clear_combined_files();
}
/**
* See {@link combine_files()}.
*/
static function process_combined_files() {
return self::backend()->process_combined_files();
}
/**
* Returns all custom scripts
* See {@link Requirements_Backend::get_custom_scripts()}
*
* @return array
*/
static function get_custom_scripts() {
return self::backend()->get_custom_scripts();
}
static function debug() {
return self::backend()->debug();
}
}
class Requirements_Backend {
/**
* Paths to all required .js files relative to the webroot. * Paths to all required .js files relative to the webroot.
* *
* @var array $javascript * @var array $javascript
*/ */
protected static $javascript = array(); protected $javascript = array();
/** /**
* Paths to all required .css files relative to the webroot. * Paths to all required .css files relative to the webroot.
* *
* @var array $css * @var array $css
*/ */
protected static $css = array(); protected $css = array();
/** /**
* All custom javascript code that is inserted * All custom javascript code that is inserted
@ -27,7 +266,7 @@ class Requirements {
* *
* @var array $customScript * @var array $customScript
*/ */
protected static $customScript = array(); protected $customScript = array();
/** /**
* All custom CSS rules which are inserted * All custom CSS rules which are inserted
@ -35,14 +274,14 @@ class Requirements {
* *
* @var array $customCSS * @var array $customCSS
*/ */
protected static $customCSS = array(); protected $customCSS = array();
/** /**
* All custom HTML markup which is added before * All custom HTML markup which is added before
* the closing <head> tag, e.g. additional metatags. * the closing <head> tag, e.g. additional metatags.
* This is preferred to entering tags directly into * This is preferred to entering tags directly into
*/ */
protected static $customHeadTags = array(); protected $customHeadTags = array();
/** /**
* Remembers the filepaths of all cleared Requirements * Remembers the filepaths of all cleared Requirements
@ -52,7 +291,7 @@ class Requirements {
* *
* @var array $disabled * @var array $disabled
*/ */
protected static $disabled = array(); protected $disabled = array();
/** /**
* The filepaths (relative to webroot) or * The filepaths (relative to webroot) or
@ -64,14 +303,14 @@ class Requirements {
* *
* @var array $blocked * @var array $blocked
*/ */
protected static $blocked = array(); protected $blocked = array();
/** /**
* See {@link combine_files()}. * See {@link combine_files()}.
* *
* @var array $combine_files * @var array $combine_files
*/ */
public static $combine_files = array(); public $combine_files = array();
/** /**
* Using the JSMin library to minify any * Using the JSMin library to minify any
@ -79,7 +318,7 @@ class Requirements {
* *
* @var boolean * @var boolean
*/ */
public static $combine_js_with_jsmin = true; public $combine_js_with_jsmin = true;
/** /**
* Put all javascript includes at the bottom of the template * Put all javascript includes at the bottom of the template
@ -94,14 +333,24 @@ class Requirements {
* *
* @var boolean * @var boolean
*/ */
public static $write_js_to_body = false; public $write_js_to_body = false;
/** /**
* Register the given javascript file as required. * Register the given javascript file as required.
* Filenames should be relative to the base, eg, 'sapphire/javascript/loader.js' * Filenames should be relative to the base, eg, 'sapphire/javascript/loader.js'
*/ */
static function javascript($file) {
Requirements::$javascript[$file] = true; 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));
} }
/** /**
@ -110,41 +359,39 @@ class Requirements {
* @param script The script content * @param script The script content
* @param uniquenessID Use this to ensure that pieces of code only get added once. * @param uniquenessID Use this to ensure that pieces of code only get added once.
*/ */
static function customScript($script, $uniquenessID = null) { public function customScript($script, $uniquenessID = null) {
if($uniquenessID) if($uniquenessID)
Requirements::$customScript[$uniquenessID] = $script; $this->customScript[$uniquenessID] = $script;
else { else {
Requirements::$customScript[] = $script; $this->customScript[] = $script;
} }
$script .= "\n"; $script .= "\n";
} }
/**
* Add the CSS styling to the header of the page function customCSS($script, $uniquenessID = null) {
* @todo Make Requirements automatically put this into a separate file :-)
*/
static function customCSS($script, $uniquenessID = null) {
if($uniquenessID) if($uniquenessID)
Requirements::$customCSS[$uniquenessID] = $script; $this->customCSS[$uniquenessID] = $script;
else { else {
Requirements::$customCSS[] = $script; $this->customCSS[] = $script;
} }
} }
/** /**
* Add the following custom code to the <head> section of the page. * Add the following custom code to the <head> section of the page.
* *
* @param string $html * @param string $html
* @param string $uniquenessID * @param string $uniquenessID
*/ */
static function insertHeadTags($html, $uniquenessID = null) { function insertHeadTags($html, $uniquenessID = null) {
if($uniquenessID) if($uniquenessID)
Requirements::$customHeadTags[$uniquenessID] = $html; $this->customHeadTags[$uniquenessID] = $html;
else { else {
Requirements::$customHeadTags[] = $html; $this->customHeadTags[] = $html;
} }
} }
/** /**
* Load the given javascript template with the page. * Load the given javascript template with the page.
@ -158,9 +405,9 @@ class Requirements {
$replace[] = str_replace("\\'","'", Convert::raw2js($v)); $replace[] = str_replace("\\'","'", Convert::raw2js($v));
} }
$script = str_replace($search, $replace, $script); $script = str_replace($search, $replace, $script);
Requirements::customScript($script, $uniquenessID); $this->customScript($script, $uniquenessID);
} }
/** /**
* Register the given stylesheet file as required. * Register the given stylesheet file as required.
* *
@ -168,61 +415,14 @@ class Requirements {
* @param $media String Comma-separated list of media-types (e.g. "screen,projector") * @param $media String Comma-separated list of media-types (e.g. "screen,projector")
* @see http://www.w3.org/TR/REC-CSS2/media.html * @see http://www.w3.org/TR/REC-CSS2/media.html
*/ */
static function css($file, $media = null) { function css($file, $media = null) {
Requirements::$css[$file] = array( $this->css[$file] = array(
"media" => $media "media" => $media
); );
} }
/** function get_css() {
* Register the given "themeable stylesheet" as required. return array_diff_key($this->css,$this->blocked));
* Themeable stylesheets have globally unique names, just like templates and PHP files.
* Because of this, they can be replaced by similarly named CSS files in the theme directory.
*
* @param $name String The identifier of the file. For example, css/MyFile.css would have the identifier "MyFile"
* @param $media String Comma-separated list of media-types (e.g. "screen,projector")
*/
static function themedCSS($name, $media = null) {
global $_CSS_MANIFEST;
$theme = SSViewer::current_theme();
if($theme && isset($_CSS_MANIFEST[$name]) && isset($_CSS_MANIFEST[$name]['themes'])
&& isset($_CSS_MANIFEST[$name]['themes'][$theme]))
Requirements::css($_CSS_MANIFEST[$name]['themes'][$theme], $media);
else if(isset($_CSS_MANIFEST[$name]) && isset($_CSS_MANIFEST[$name]['unthemed'])) Requirements::css($_CSS_MANIFEST[$name]['unthemed'], $media);
// Normal requirements fails quietly when there is no css - we should do the same
// else user_error("themedCSS - No CSS file '$name.css' found.", E_USER_WARNING);
}
/**
* 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
*/
static function clear($fileOrID = null) {
if($fileOrID) {
foreach(array('javascript','css', 'customScript', 'customCSS') as $type) {
if(isset(Requirements::${$type}[$fileOrID])) {
Requirements::$disabled[$type][$fileOrID] = Requirements::${$type}[$fileOrID];
unset(Requirements::${$type}[$fileOrID]);
}
}
} else {
Requirements::$disabled['javascript'] = Requirements::$javascript;
Requirements::$disabled['css'] = Requirements::$css;
Requirements::$disabled['customScript'] = Requirements::$customScript;
Requirements::$disabled['customCSS'] = Requirements::$customCSS;
Requirements::$javascript = array();
Requirements::$css = array();
Requirements::$customScript = array();
Requirements::$customCSS = array();
Requirements::$customHeadTags = array();
}
} }
/** /**
@ -234,34 +434,61 @@ class Requirements {
* *
* @param string $fileOrID * @param string $fileOrID
*/ */
static function block($fileOrID) { function block($fileOrID) {
self::$blocked[$fileOrID] = $fileOrID; $this->blocked[$fileOrID] = $fileOrID;
} }
/**
* 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
*/
function clear($fileOrID = null) {
if($fileOrID) {
foreach(array('javascript','css', 'customScript', 'customCSS') as $type) {
if(isset($this->{$type}[$fileOrID])) {
$this->disabled[$type][$fileOrID] = $this->{$type}[$fileOrID];
unset($this->{$type}[$fileOrID]);
}
}
} else {
$this->disabled['javascript'] = $this->javascript;
$this->disabled['css'] = $this->css;
$this->disabled['customScript'] = $this->customScript;
$this->disabled['customCSS'] = $this->customCSS;
$this->javascript = array();
$this->css = array();
$this->customScript = array();
$this->customCSS = array();
$this->customHeadTags = array();
}
}
/** /**
* Removes an item from the blocking-list. * Removes an item from the blocking-list.
* CAUTION: Does not "re-add" any previously blocked elements. * CAUTION: Does not "re-add" any previously blocked elements.
* @param string $fileOrID * @param string $fileOrID
*/ */
static function unblock($fileOrID) { function unblock($fileOrID) {
if(isset(self::$blocked[$fileOrID])) unset(self::$blocked[$fileOrID]); if(isset($this->blocked[$fileOrID])) unset($this->blocked[$fileOrID]);
} }
/** /**
* Removes all items from the blocking-list. * Removes all items from the blocking-list.
*/ */
static function unblock_all() { static function unblock_all() {
self::$blocked = array(); self::backend()->blocked = array();
} }
/** /**
* Restore requirements cleared by call to Requirements::clear * Restore requirements cleared by call to Requirements::clear
*/ */
static function restore() { function restore() {
Requirements::$javascript = Requirements::$disabled['javascript']; $this->javascript = $this->disabled['javascript'];
Requirements::$css = Requirements::$disabled['css']; $this->css = $this->disabled['css'];
Requirements::$customScript = Requirements::$disabled['customScript']; $this->customScript = $this->disabled['customScript'];
Requirements::$customCSS = Requirements::$disabled['customCSS']; $this->customCSS = $this->disabled['customCSS'];
} }
/** /**
@ -275,20 +502,20 @@ class Requirements {
* @param string $content HTML content that has already been parsed from the $templateFilePath through {@link SSViewer}. * @param string $content HTML content that has already been parsed from the $templateFilePath through {@link SSViewer}.
* @return string HTML content thats augumented with the requirements before the closing <head> tag. * @return string HTML content thats augumented with the requirements before the closing <head> tag.
*/ */
static function includeInHTML($templateFile, $content) { function includeInHTML($templateFile, $content) {
if(isset($_GET['debug_profile'])) Profiler::mark("Requirements::includeInHTML"); if(isset($_GET['debug_profile'])) Profiler::mark("Requirements::includeInHTML");
if(strpos($content, '</head') !== false && (Requirements::$javascript || Requirements::$css || Requirements::$customScript || Requirements::$customHeadTags)) { if(strpos($content, '</head') !== false && ($this->javascript || $this->css || $this->customScript || $this->customHeadTags)) {
$requirements = ''; $requirements = '';
$jsRequirements = ''; $jsRequirements = '';
// Combine files - updates Requirements::$javascript and Requirements::$css // Combine files - updates $this->javascript and $this->css
self::process_combined_files(); $this->process_combined_files();
// //
self::process_i18n_javascript(); $this->process_i18n_javascript();
foreach(array_diff_key(self::$javascript,self::$blocked) as $file => $dummy) { foreach(array_diff_key($this->javascript,$this->blocked) as $file => $dummy) {
$path = self::path_for_file($file); $path = self::path_for_file($file);
if($path) { if($path) {
$jsRequirements .= "<script type=\"text/javascript\" src=\"$path\"></script>\n"; $jsRequirements .= "<script type=\"text/javascript\" src=\"$path\"></script>\n";
@ -297,30 +524,30 @@ class Requirements {
// add all inline javascript *after* including external files which // add all inline javascript *after* including external files which
// they might rely on // they might rely on
if(self::$customScript) { if($this->customScript) {
foreach(array_diff_key(self::$customScript,self::$blocked) as $script) { foreach(array_diff_key($this->customScript,$this->blocked) as $script) {
$jsRequirements .= "<script type=\"text/javascript\">\n//<![CDATA[\n"; $jsRequirements .= "<script type=\"text/javascript\">\n//<![CDATA[\n";
$jsRequirements .= "$script\n"; $jsRequirements .= "$script\n";
$jsRequirements .= "\n//]]>\n</script>\n"; $jsRequirements .= "\n//]]>\n</script>\n";
} }
} }
foreach(array_diff_key(self::$css,self::$blocked) as $file => $params) { foreach(array_diff_key($this->css,$this->blocked) as $file => $params) {
$path = self::path_for_file($file); $path = self::path_for_file($file);
if($path) { if($path) {
$media = (isset($params['media']) && !empty($params['media'])) ? " media=\"{$params['media']}\"" : ""; $media = (isset($params['media']) && !empty($params['media'])) ? " media=\"{$params['media']}\"" : "";
$requirements .= "<link rel=\"stylesheet\" type=\"text/css\"{$media} href=\"$path\" />\n"; $requirements .= "<link rel=\"stylesheet\" type=\"text/css\"{$media} href=\"$path\" />\n";
} }
} }
foreach(array_diff_key(self::$customCSS,self::$blocked) as $css) { foreach(array_diff_key($this->customCSS,$this->blocked) as $css) {
$requirements .= "<style type=\"text/css\">\n$css\n</style>\n"; $requirements .= "<style type=\"text/css\">\n$css\n</style>\n";
} }
foreach(array_diff_key(self::$customHeadTags,self::$blocked) as $customHeadTag) { foreach(array_diff_key($this->customHeadTags,$this->blocked) as $customHeadTag) {
$requirements .= "$customHeadTag\n"; $requirements .= "$customHeadTag\n";
} }
if(self::$write_js_to_body) { if($this->write_js_to_body) {
// Remove all newlines from code to preserve layout // Remove all newlines from code to preserve layout
$jsRequirements = preg_replace('/>\n*/', '>', $jsRequirements); $jsRequirements = preg_replace('/>\n*/', '>', $jsRequirements);
@ -349,25 +576,25 @@ class Requirements {
return $content; return $content;
} }
/** /**
* Automatically includes the necessary lang-files from the module * Automatically includes the necessary lang-files from the module
* according to the locale set in {@link i18n::$current_locale}. * according to the locale set in {@link i18n::$current_locale}.
* Assumes that a subfolder /javascript exists relative to the included * Assumes that a subfolder /javascript exists relative to the included
* javascript file, with a file named after the locale - * javascript file, with a file named after the locale -
* so usually <mymodule>/javascript/lang/en_US.js. * so usually <mymodule>/javascript/lang/en_US.js.
*/ */
protected static function process_i18n_javascript() { protected function process_i18n_javascript() {
// ensure to include the i18n base library // ensure to include the i18n base library
if( if(
count(array_diff_key(self::$javascript,self::$blocked)) count(array_diff_key($this->javascript,$this->blocked))
&& !isset(self::$javascript[SAPPHIRE_DIR . '/javascript/i18n.js']) && !isset($this->javascript[SAPPHIRE_DIR . '/javascript/i18n.js'])
) { ) {
self::$javascript[THIRDPARTY_DIR . '/prototype.js'] = true; $this->javascript[THIRDPARTY_DIR . '/prototype.js'] = true;
self::$javascript[SAPPHIRE_DIR . '/javascript/i18n.js'] = true; $this->javascript[SAPPHIRE_DIR . '/javascript/i18n.js'] = true;
} }
// include the specific locale and the master locale for each module // include the specific locale and the master locale for each module
foreach(array_diff_key(self::$javascript,self::$blocked) as $file => $dummy) { foreach(array_diff_key($this->javascript,$this->blocked) as $file => $dummy) {
if(preg_match('/^http[s]?/', $file)) continue; if(preg_match('/^http[s]?/', $file)) continue;
$absolutePath = Director::baseFolder() . '/' . $file; $absolutePath = Director::baseFolder() . '/' . $file;
@ -378,7 +605,7 @@ class Requirements {
$langFile = Director::makeRelative($path); $langFile = Director::makeRelative($path);
// Remove rogue leading slashes from Director::makeRelative() // Remove rogue leading slashes from Director::makeRelative()
$langFile = preg_replace('/^\//', '', $langFile); $langFile = preg_replace('/^\//', '', $langFile);
self::$javascript[$langFile] = true; $this->javascript[$langFile] = true;
} }
} }
} }
@ -386,7 +613,7 @@ class Requirements {
} }
/** /**
* * Finds the path for specified file.
* *
* @param string $fileOrUrl * @param string $fileOrUrl
* @return string|boolean * @return string|boolean
@ -457,9 +684,9 @@ class Requirements {
* @param string $combinedFileName Filename of the combined file (will be stored in {@link Director::baseFolder()} by default) * @param string $combinedFileName Filename of the combined file (will be stored in {@link Director::baseFolder()} by default)
* @param array $files Array of filenames relative to the webroot * @param array $files Array of filenames relative to the webroot
*/ */
static function combine_files($combinedFileName, $files) { function combine_files($combinedFileName, $files) {
// duplicate check // duplicate check
foreach(self::$combine_files as $_combinedFileName => $_files) { foreach($this->combine_files as $_combinedFileName => $_files) {
$duplicates = array_intersect($_files, $files); $duplicates = array_intersect($_files, $files);
if($duplicates) { if($duplicates) {
user_error("Requirements::combine_files(): Already included files " . implode(',', $duplicates) . " in combined file '{$_combinedFileName}'", E_USER_NOTICE); user_error("Requirements::combine_files(): Already included files " . implode(',', $duplicates) . " in combined file '{$_combinedFileName}'", E_USER_NOTICE);
@ -467,14 +694,15 @@ class Requirements {
} }
} }
self::$combine_files[$combinedFileName] = $files; $this->combine_files[$combinedFileName] = $files;
} }
/** /**
* Returns all combined files.
* @return array * @return array
*/ */
static function get_combine_files() { function get_combine_files() {
return self::$combine_files; return $this->combine_files;
} }
/** /**
@ -482,8 +710,8 @@ class Requirements {
* *
* @param string $combinedFileName If left blank, all combined files are deleted. * @param string $combinedFileName If left blank, all combined files are deleted.
*/ */
static function delete_combined_files($combinedFileName = null) { function delete_combined_files($combinedFileName = null) {
$combinedFiles = ($combinedFileName) ? array($combinedFileName => null) : self::$combine_files; $combinedFiles = ($combinedFileName) ? array($combinedFileName => null) : $this->combine_files;
foreach($combinedFiles as $combinedFile => $sourceItems) { foreach($combinedFiles as $combinedFile => $sourceItems) {
$filePath = Director::baseFolder() . '/' . $combinedFile; $filePath = Director::baseFolder() . '/' . $combinedFile;
if(file_exists($filePath)) { if(file_exists($filePath)) {
@ -492,20 +720,19 @@ class Requirements {
} }
} }
/** function clear_combined_files() {
* Re-sets the combined files definition $this->combine_files = array();
*/
static function clear_combined_files() {
self::$combine_files = array();
} }
/** /**
* See {@link combine_files()}. * See {@link combine_files()}
*/ *
static function process_combined_files() { */
function process_combined_files() {
// Make a map of files that could be potentially combined // Make a map of files that could be potentially combined
$combinerCheck = array(); $combinerCheck = array();
foreach(self::$combine_files as $combinedFile => $sourceItems) { foreach($this->combine_files as $combinedFile => $sourceItems) {
foreach($sourceItems as $sourceItem) { foreach($sourceItems as $sourceItem) {
if(isset($combinerCheck[$sourceItem]) && $combinerCheck[$sourceItem] != $combinedFile){ if(isset($combinerCheck[$sourceItem]) && $combinerCheck[$sourceItem] != $combinedFile){
user_error("Requirements::process_combined_files - file '$sourceItem' appears in two combined files:" . " '{$combinerCheck[$sourceItem]}' and '$combinedFile'", E_USER_WARNING); user_error("Requirements::process_combined_files - file '$sourceItem' appears in two combined files:" . " '{$combinerCheck[$sourceItem]}' and '$combinedFile'", E_USER_WARNING);
@ -519,7 +746,7 @@ class Requirements {
$combinedFiles = array(); $combinedFiles = array();
$newJSRequirements = array(); $newJSRequirements = array();
$newCSSRequirements = array(); $newCSSRequirements = array();
foreach(Requirements::$javascript as $file => $dummy) { foreach($this->javascript as $file => $dummy) {
if(isset($combinerCheck[$file])) { if(isset($combinerCheck[$file])) {
$newJSRequirements[$combinerCheck[$file]] = true; $newJSRequirements[$combinerCheck[$file]] = true;
$combinedFiles[$combinerCheck[$file]] = true; $combinedFiles[$combinerCheck[$file]] = true;
@ -528,7 +755,7 @@ class Requirements {
} }
} }
foreach(Requirements::$css as $file => $params) { foreach($this->css as $file => $params) {
if(isset($combinerCheck[$file])) { if(isset($combinerCheck[$file])) {
$newCSSRequirements[$combinerCheck[$file]] = true; $newCSSRequirements[$combinerCheck[$file]] = true;
$combinedFiles[$combinerCheck[$file]] = true; $combinedFiles[$combinerCheck[$file]] = true;
@ -539,13 +766,13 @@ class Requirements {
// @todo Alters the original information, which means you can't call this // @todo Alters the original information, which means you can't call this
// method repeatedly - it will behave different on the second call! // method repeatedly - it will behave different on the second call!
Requirements::$javascript = $newJSRequirements; $this->javascript = $newJSRequirements;
Requirements::$css = $newCSSRequirements; $this->css = $newCSSRequirements;
// Process the combined files // Process the combined files
$base = Director::baseFolder() . '/'; $base = Director::baseFolder() . '/';
foreach(array_diff_key($combinedFiles,self::$blocked) as $combinedFile => $dummy) { foreach(array_diff_key($combinedFiles,$this->blocked) as $combinedFile => $dummy) {
$fileList = self::$combine_files[$combinedFile]; $fileList = $this->combine_files[$combinedFile];
// Determine if we need to build the combined include // Determine if we need to build the combined include
if(file_exists($base . $combinedFile) && !isset($_GET['flush'])) { if(file_exists($base . $combinedFile) && !isset($_GET['flush'])) {
@ -563,10 +790,10 @@ class Requirements {
if(!$refresh) continue; if(!$refresh) continue;
$combinedData = ""; $combinedData = "";
foreach(array_diff($fileList,self::$blocked) as $file) { foreach(array_diff($fileList,$this->blocked) as $file) {
$fileContent = file_get_contents($base . $file); $fileContent = file_get_contents($base . $file);
// if we have a javascript file and jsmin is enabled, minify the content // if we have a javascript file and jsmin is enabled, minify the content
if(stripos($file, '.js') && self::$combine_js_with_jsmin) { if(stripos($file, '.js') && $this->combine_js_with_jsmin) {
require_once('thirdparty/jsmin/JSMin.php'); require_once('thirdparty/jsmin/JSMin.php');
$fileContent = JSMin::minify($fileContent); $fileContent = JSMin::minify($fileContent);
} }
@ -581,14 +808,13 @@ class Requirements {
fclose($fh); fclose($fh);
unset($fh); unset($fh);
} }
} }
static function get_custom_scripts() {
static function get_custom_scripts() {
$requirements = ""; $requirements = "";
if(Requirements::$customScript) { if($this->customScript) {
foreach(Requirements::$customScript as $script) { foreach($this->customScript as $script) {
$requirements .= "$script\n"; $requirements .= "$script\n";
} }
} }
@ -596,14 +822,37 @@ class Requirements {
return $requirements; return $requirements;
} }
static function debug() { /**
Debug::show(Requirements::$javascript); * Register the given "themeable stylesheet" as required.
Debug::show(Requirements::$css); * Themeable stylesheets have globally unique names, just like templates and PHP files.
Debug::show(Requirements::$customCSS); * Because of this, they can be replaced by similarly named CSS files in the theme directory.
Debug::show(Requirements::$customScript); *
Debug::show(Requirements::$customHeadTags); * @param $name String The identifier of the file. For example, css/MyFile.css would have the identifier "MyFile"
Debug::show(Requirements::$combine_files); * @param $media String Comma-separated list of media-types (e.g. "screen,projector")
*/
function themedCSS($name, $media = null) {
global $_CSS_MANIFEST;
$theme = SSViewer::current_theme();
if($theme && isset($_CSS_MANIFEST[$name]) && isset($_CSS_MANIFEST[$name]['themes'])
&& isset($_CSS_MANIFEST[$name]['themes'][$theme]))
Requirements::css($_CSS_MANIFEST[$name]['themes'][$theme], $media);
else if(isset($_CSS_MANIFEST[$name]) && isset($_CSS_MANIFEST[$name]['unthemed'])) $this->css($_CSS_MANIFEST[$name]['unthemed'], $media);
// Normal requirements fails quietly when there is no css - we should do the same
// else user_error("themedCSS - No CSS file '$name.css' found.", E_USER_WARNING);
} }
function debug() {
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);
}
} }

View File

@ -139,5 +139,32 @@ class RequirementsTest extends SapphireTest {
); );
} }
function testRequirementsBackend() {
$requirements = new Requirements_Backend();
$requirements->javascript(SAPPHIRE_DIR . '/tests/forms/a.js');
$this->assertTrue(count($requirements->get_javascript()) == 1, "There should be only 1 file included in required javascript.");
$this->assertTrue(in_array(SAPPHIRE_DIR . '/tests/forms/a.js', $requirements->get_javascript()), "/test/forms/a.js should be included in required javascript.");
$requirements->javascript(SAPPHIRE_DIR . '/tests/forms/b.js');
$this->assertTrue(count($requirements->get_javascript()) == 2, "There should be 2 files included in required javascript.");
$requirements->block(SAPPHIRE_DIR . '/tests/forms/a.js');
$this->assertTrue(count($requirements->get_javascript()) == 1, "There should be only 1 file included in required javascript.");
$this->assertFalse(in_array(SAPPHIRE_DIR . '/tests/forms/a.js', $requirements->get_javascript()), "/test/forms/a.js should not be included in required javascript after it has been blocked.");
$this->assertTrue(in_array(SAPPHIRE_DIR . '/tests/forms/b.js', $requirements->get_javascript()), "/test/forms/b.js should be included in required javascript.");
$requirements->css(SAPPHIRE_DIR . '/tests/forms/a.css');
$this->assertTrue(count($requirements->get_css()) == 1, "There should be only 1 file included in required css.");
$this->assertArrayHasKey(SAPPHIRE_DIR . '/tests/forms/a.css', $requirements->get_css(), "/tests/forms/a.css should be in required css.");
$requirements->block(SAPPHIRE_DIR . '/tests/forms/a.css');
$this->assertTrue(count($requirements->get_css()) == 0, "There should be nothing in required css after file has been blocked.");
}
} }
?> ?>