Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding Helper::assetTimestamp()

Refactor's repeated code from Html and Javascript Helpers.
Test cases added.
  • Loading branch information...
commit ee514f3fc6fce7bf4a4fef76af6104b85283a208 1 parent 26d2237
@markstory markstory authored
View
19 cake/libs/view/helper.php
@@ -225,6 +225,25 @@ function webroot($file) {
}
/**
+ * Adds a timestamp to a file based resource based on the value of `Asset.timestamp` in
+ * Configure. If Asset.timestamp is true and debug > 0, or Asset.timestamp == 'force'
+ * a timestamp will be added.
+ *
+ * @param string $path The file path to timestamp, the path must be inside WWW_ROOT
+ * @return string Path with a timestamp added, or not.
+ **/
+ function assetTimestamp($path) {
+ $timestampEnabled = (
+ (Configure::read('Asset.timestamp') === true && Configure::read() > 0) ||
+ Configure::read('Asset.timestamp') === 'force'
+ );
+ if (strpos($path, '?') === false && $timestampEnabled) {
+ $path .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $path));
+ }
+ return $path;
+ }
+
+/**
* Used to remove harmful tags from content
*
* @param mixed $output
View
35 cake/tests/cases/libs/view/helper.test.php
@@ -412,6 +412,41 @@ function testUrlConversion() {
}
/**
+ * test assetTimestamp application
+ *
+ * @return void
+ **/
+ function testAssetTimestamp() {
+ $_timestamp = Configure::read('Asset.timestamp');
+ $_debug = Configure::read('debug');
+
+ Configure::write('Asset.timestamp', false);
+ $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
+ $this->assertEqual($result, CSS_URL . 'cake.generic.css');
+
+ Configure::write('Asset.timestamp', true);
+ Configure::write('debug', 0);
+ $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
+ $this->assertEqual($result, CSS_URL . 'cake.generic.css');
+
+ Configure::write('Asset.timestamp', true);
+ Configure::write('debug', 2);
+ $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
+ $this->assertPattern('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
+
+ Configure::write('Asset.timestamp', 'force');
+ Configure::write('debug', 0);
+ $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
+ $this->assertPattern('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
+
+ $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css?someparam');
+ $this->assertEqual($result, CSS_URL . 'cake.generic.css?someparam');
+
+ Configure::write('debug', $_debug);
+ Configure::write('Asset.timestamp', $_timestamp);
+ }
+
+/**
* testFieldsWithSameName method
*
* @access public
Please sign in to comment.
Something went wrong with that request. Please try again.