Permalink
Browse files

API CHANGE Refactored Requirements to use Requirements_Backend at all…

… times - this makes testing far easier. Thanks tobych!

MINOR Updated RequirementsTest to test Requirements_Backend instead of global statics (from r100513)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@105590 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information...
1 parent 5a38c65 commit 9c03cc7fe141a506d00ec9adcbf0eb82b2625d19 @chillu chillu committed May 25, 2010
Showing with 147 additions and 133 deletions.
  1. +73 −42 core/Requirements.php
  2. +74 −91 tests/forms/RequirementsTest.php
View
@@ -11,27 +11,19 @@ class Requirements {
/**
* Enable combining of css/javascript files.
- *
- * @var boolean
+ * @param boolean $enable
*/
- private static $combined_files_enabled = true;
-
public static function set_combined_files_enabled($enable) {
- self::$combined_files_enabled = (bool) $enable;
- }
-
- public static function get_combined_files_enabled() {
- return self::$combined_files_enabled;
+ self::backend()->set_combined_files_enabled($enable);
}
/**
- * 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
+ * Checks whether combining of css/javascript files is enabled.
+ * @return boolean
*/
- private static $suffix_requirements = true;
+ public static function get_combined_files_enabled() {
+ return self::backend()->get_combined_files_enabled();
+ }
/**
* Set whether we want to suffix requirements with the time /
@@ -40,7 +32,7 @@ public static function get_combined_files_enabled() {
* @param bool
*/
public static function set_suffix_requirements($var) {
- self::$suffix_requirements = $var;
+ self::backend()->set_suffix_requirements($var);
}
/**
@@ -49,7 +41,7 @@ public static function set_suffix_requirements($var) {
* @return bool
*/
public static function get_suffix_requirements() {
- return self::$suffix_requirements;
+ return self::backend()->get_suffix_requirements();
}
/**
@@ -58,10 +50,10 @@ public static function get_suffix_requirements() {
* @var Requirements
*/
private static $backend = null;
+
public static function backend() {
if(!self::$backend) {
self::$backend = new Requirements_Backend();
-
}
return self::$backend;
}
@@ -74,7 +66,6 @@ public static function backend() {
public static function set_backend(Requirements_Backend $backend) {
self::$backend = $backend;
}
-
/**
* Register the given javascript file as required.
@@ -86,7 +77,6 @@ static function javascript($file) {
self::backend()->javascript($file);
}
-
/**
* Add the javascript code to the header of the page
*
@@ -120,8 +110,6 @@ static function customCSS($script, $uniquenessID = null) {
static function insertHeadTags($html, $uniquenessID = null) {
self::backend()->insertHeadTags($html, $uniquenessID);
}
-
-
/**
* Load the given javascript template with the page.
@@ -196,7 +184,6 @@ static function unblock_all() {
self::backend()->unblock_all();
}
-
/**
* Restore requirements cleared by call to Requirements::clear
* See {@link Requirements_Backend::restore()}
@@ -310,16 +297,33 @@ static function debug() {
* @subpackage view
*/
class Requirements_Backend {
- /**
+
+ /**
+ * 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;
+
+ /**
* Paths to all required .js files relative to the webroot.
- *
+ *
* @var array $javascript
*/
protected $javascript = array();
/**
* Paths to all required .css files relative to the webroot.
- *
+ *
* @var array $css
*/
protected $css = array();
@@ -334,7 +338,7 @@ class Requirements_Backend {
/**
* All custom CSS rules which are inserted
- * directly at the bottom of the HTML <head> tag.
+ * directly at the bottom of the HTML <head> tag.
*
* @var array $customCSS
*/
@@ -343,7 +347,7 @@ class Requirements_Backend {
/**
* All custom HTML markup which is added before
* 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 $customHeadTags = array();
@@ -354,34 +358,34 @@ class Requirements_Backend {
* @var array $disabled
*/
protected $disabled = array();
-
+
/**
* 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.
- *
+ *
* @var array $blocked
*/
protected $blocked = array();
-
+
/**
* See {@link combine_files()}.
- *
+ *
* @var array $combine_files
*/
public $combine_files = array();
-
+
/**
* Using the JSMin library to minify any
* javascript file passed to {@link combine_files()}.
*
* @var boolean
*/
public $combine_js_with_jsmin = true;
-
+
/**
* Put all javascript includes at the bottom of the template
* before the closing <body> tag instead of the <head> tag.
@@ -397,6 +401,33 @@ class Requirements_Backend {
*/
public $write_js_to_body = true;
+ function set_combined_files_enabled($enable) {
+ $this->combined_files_enabled = (bool) $enable;
+ }
+
+ function get_combined_files_enabled() {
+ return $this->combined_files_enabled;
+ }
+
+ /**
+ * Set whether we want to suffix requirements with the time /
+ * location on to the requirements
+ *
+ * @param bool
+ */
+ function set_suffix_requirements($var) {
+ $this->suffix_requirements = $var;
+ }
+
+ /**
+ * Return whether we want to suffix requirements
+ *
+ * @return bool
+ */
+ function get_suffix_requirements() {
+ return $this->suffix_requirements;
+ }
+
/**
* 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
@@ -585,7 +616,7 @@ function includeInHTML($templateFile, $content) {
$this->process_combined_files();
foreach(array_diff_key($this->javascript,$this->blocked) as $file => $dummy) {
- $path = self::path_for_file($file);
+ $path = $this->path_for_file($file);
if($path) {
$jsRequirements .= "<script type=\"text/javascript\" src=\"$path\"></script>\n";
}
@@ -602,7 +633,7 @@ function includeInHTML($templateFile, $content) {
}
foreach(array_diff_key($this->css,$this->blocked) as $file => $params) {
- $path = self::path_for_file($file);
+ $path = $this->path_for_file($file);
if($path) {
$media = (isset($params['media']) && !empty($params['media'])) ? " media=\"{$params['media']}\"" : "";
$requirements .= "<link rel=\"stylesheet\" type=\"text/css\"{$media} href=\"$path\" />\n";
@@ -697,7 +728,7 @@ public function add_i18n_javascript($langDir) {
* @param string $fileOrUrl
* @return string|boolean
*/
- protected static function path_for_file($fileOrUrl) {
+ protected function path_for_file($fileOrUrl) {
if(preg_match('/^http[s]?/', $fileOrUrl)) {
return $fileOrUrl;
} elseif(Director::fileExists($fileOrUrl)) {
@@ -708,7 +739,7 @@ protected static function path_for_file($fileOrUrl) {
$suffix = '&' . substr($fileOrUrl, strpos($fileOrUrl, '?')+1);
$fileOrUrl = substr($fileOrUrl, 0, strpos($fileOrUrl, '?'));
}
- if(Requirements::get_suffix_requirements()) {
+ if($this->suffix_requirements) {
$mtimesuffix = "?m=" . filemtime(Director::baseFolder() . '/' . $fileOrUrl);
}
return "{$prefix}{$fileOrUrl}{$mtimesuffix}{$suffix}";
@@ -776,7 +807,7 @@ function combine_files($combinedFileName, $files) {
foreach($this->combine_files as $_combinedFileName => $_files) {
$duplicates = array_intersect($_files, $files);
if($duplicates) {
- user_error("Requirements::combine_files(): Already included files " . implode(',', $duplicates) . " in combined file '{$_combinedFileName}'", E_USER_NOTICE);
+ user_error("Requirements_Backend::combine_files(): Already included files " . implode(',', $duplicates) . " in combined file '{$_combinedFileName}'", E_USER_NOTICE);
return false;
}
}
@@ -822,7 +853,7 @@ function process_combined_files() {
if(class_exists('SapphireTest',false)) $runningTest = SapphireTest::is_running_test();
else $runningTest = false;
- if((Director::isDev() && !$runningTest) || !Requirements::get_combined_files_enabled()) {
+ if((Director::isDev() && !$runningTest) || !$this->combined_files_enabled) {
return;
}
@@ -831,7 +862,7 @@ function process_combined_files() {
foreach($this->combine_files as $combinedFile => $sourceItems) {
foreach($sourceItems as $sourceItem) {
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_Backend::process_combined_files - file '$sourceItem' appears in two combined files:" . " '{$combinerCheck[$sourceItem]}' and '$combinedFile'", E_USER_WARNING);
}
$combinerCheck[$sourceItem] = $combinedFile;
@@ -947,7 +978,7 @@ function themedCSS($name, $media = null) {
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);
+ $this->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
Oops, something went wrong.

0 comments on commit 9c03cc7

Please sign in to comment.