Skip to content
Browse files

use options param instead of Configure::write(...)

  • Loading branch information...
1 parent a626a87 commit 9a12910e2a43d388106c2d00c3317c39cebdbaec Matt Curry committed Oct 22, 2010
Showing with 91 additions and 63 deletions.
  1. +34 −10 README
  2. +7 −6 tests/cases/helpers/asset.test.php
  3. +50 −47 views/helpers/asset.php
View
44 README
@@ -30,14 +30,38 @@
8. Then in your layout file, in the head section, instead of using $scripts_for_layout call the helper:
echo $asset->scripts_for_layout();
+/* Options */
+ Options are set when including the helper.
+ var $helpers = array('Asset.asset' => array('optionName' => 'optionValue'));
+
+ * debug, default: false
+ Cake debug = 0 packed js/css returned. $this->options['md5FileName']options['debug'] doesn't do anything.
+ Cake debug > 0, $this->options['md5FileName']options['debug'] = false essentially turns the helper off. js/css not packed. Good for debugging your js/css files.
+ Cake debug > 0, $this->options['md5FileName']options['debug'] = true packed js/css returned. Good for debugging this helper.
+
+ * checkTs, default: false
+ there is a *minimal* perfomance hit associated with looking up the filemtimes
+ if you clean out your cached dir (as set below) on builds then you don't need this.
+
+ * md5FileName, default: false
+ the packed files are named by stringing together all the individual file names
+ this can generate really long names, so by setting this option to true
+ the long name is md5'd, producing a resonable length file name.
+
+ * searchPaths, default: array()
+ Additional paths for searching for css/js
+
+ * cachePaths, default: array('css' => 'ccss', 'js' => 'cjs')
+ Paths for storing the compressed files, relative to your webroot
+
+ * cssCompression, default: high_compression
+ default, low_compression, high_compression, highest_compression
+
+ * fixCssImg, default: false
+ replace relative img paths in css files with full http://...
+
/* Tips */
- * By default the cached files are written to /app/webroot/cjs and /app/webroot/ccss. You can change that by setting:
- Configure::write('Asset.jsPath', 'some/path');
- Configure::write('Asset.cssPath', 'some/path');
- Don't include slashes at the beginning or end. Path will be relative to /app/webroot. So if set:
- Configure::write('Asset.jsPath', 'js/packed');
- The path will be /app/webroot/js/packed
- * Remember to set the inline option to false for JS and CSS in your layout if you want them to be packed with the view scripts.
- * Setting DEBUG on will cause this helper to output the scripts the same way $scripts_for_layout would, effectifly turning it off while testing.
- * If you get a JavaScript error with a packed version of a file it's most likely missing a semi-colon somewhere.
- * Order is important. If you include script1 then script2 on one view and script2 then script1 on another, they will generate separate packed versions and will be treated by the browser as separate scripts.
+ * Remember to set the inline option to false for JS and CSS in your layout if you want them to be packed with the view scripts.
+ * Setting DEBUG on will cause this helper to output the scripts the same way $scripts_for_layout would, effectifly turning it off while testing.
+ * If you get a JavaScript error with a packed version of a file it's most likely missing a semi-colon somewhere.
+ * Order is important. If you include script1 then script2 on one view and script2 then script1 on another, they will generate separate packed versions and will be treated by the browser as separate scripts.
View
13 tests/cases/helpers/asset.test.php
@@ -22,7 +22,7 @@ function startCase() {
$controller = null;
$this->View = new View($controller);
- $this->Asset = new AssetHelper(array('wwwRoot' => $this->wwwRoot, 'js' => $this->wwwRoot . 'js' . DS, 'css' => $this->wwwRoot . 'css' . DS));
+ $this->Asset = new AssetHelper(array(), array('wwwRoot' => $this->wwwRoot, 'js' => $this->wwwRoot . 'js' . DS, 'css' => $this->wwwRoot . 'css' . DS));
$this->Asset->Javascript = new JavascriptHelper();
$this->Asset->Html = new HtmlHelper();
@@ -82,11 +82,11 @@ function testGenerateFileName() {
}
function testGenerateFileNameMd5() {
- $this->Asset->md5FileName = true;
+ $this->Asset->options['md5FileName'] = true;
$files = array('script1', 'script2', 'script3');
$name = $this->Asset->__generateFileName($files);
$this->assertEqual('4991a54c1356544e1188bf6c8b9e7ae9', $name);
- $this->Asset->md5FileName = false;
+ $this->Asset->options['md5FileName'] = false;
}
function testFindFileDupeName() {
@@ -119,7 +119,7 @@ function testGetFileContentsPlugin() {
}
function testGetFileContentsExtraPath() {
- Configure::write('Asset.searchPaths', array($this->wwwRoot . 'js' . DS));
+ $this->Asset->options['searchPaths'] = array($this->wwwRoot . 'js' . DS);
$asset = array('plugin' => '', 'script' => 'open_source_with_js_and_css/style');
$contents = $this->Asset->__getFileContents($asset, 'css');
$expected = <<<END
@@ -128,7 +128,7 @@ function testGetFileContentsExtraPath() {
}
END;
$this->assertEqual($expected, $contents);
- Configure::delete('Asset.searchPaths');
+ $this->Asset->options['searchPaths'] = array();
}
function testProcessJsNew() {
@@ -185,9 +185,10 @@ function testProcessJsExistingWithChanges() {
array('plugin' => '', 'script' => 'script2'),
array('plugin' => 'asset', 'script' => 'script3'));
- $this->Asset->checkTs = true;
+ $this->Asset->options['checkTs'] = true;
$fileName = $this->Asset->__process('js', $js);
$this->assertNotEqual($origFileName, $fileName);
+ $this->Asset->options['checkTs'] = false;
}
function testProcessCssNew() {
View
97 views/helpers/asset.php
@@ -13,50 +13,58 @@
App::import('Core', array('File', 'Folder', 'Sanitize'));
class AssetHelper extends Helper {
- //Cake debug = 0 packed js/css returned. $this->debug doesn't do anything.
- //Cake debug > 0, $this->debug = false essentially turns the helper off. js/css not packed. Good for debugging your js/css files.
- //Cake debug > 0, $this->debug = true packed js/css returned. Good for debugging this helper.
- var $debug = false;
-
- //there is a *minimal* perfomance hit associated with looking up the filemtimes
- //if you clean out your cached dir (as set below) on builds then you don't need this.
- var $checkTs = false;
+ var $options = array(
+ //Cake debug = 0 packed js/css returned. $this->options['md5FileName']options['debug'] doesn't do anything.
+ //Cake debug > 0, $this->options['md5FileName']options['debug'] = false essentially turns the helper off. js/css not packed. Good for debugging your js/css files.
+ //Cake debug > 0, $this->options['md5FileName']options['debug'] = true packed js/css returned. Good for debugging this helper.
+ 'debug' => false,
+
+ //there is a *minimal* perfomance hit associated with looking up the filemtimes
+ //if you clean out your cached dir (as set below) on builds then you don't need this.
+ 'checkTs' => false,
+
+ //the packed files are named by stringing together all the individual file names
+ //this can generate really long names, so by setting this option to true
+ //the long name is md5'd, producing a resonable length file name.
+ 'md5FileName' => false,
+ //Additional paths for searching for css/js
+ 'searchPaths' => array(),
+
+ //Paths for storing the compressed files, relative to your webroot
+ 'cachePaths' => array('css' => 'ccss', 'js' => 'cjs'),
+
+ //options: default, low_compression, high_compression, highest_compression
+ //I like high_compression because it still leaves the file readable.
+ 'cssCompression' => 'high_compression',
+
+ //replace relative img paths in css files with full http://...
+ 'fixCssImg' => false
+ );
+
//Class for localizing JS files if JS I18N plugin is installed
//http://github.com/mcurry/js/tree/master
var $Lang = false;
- //the packed files are named by stringing together all the individual file names
- //this can generate really long names, so by setting this option to true
- //the long name is md5'd, producing a resonable length file name.
- var $md5FileName = false;
-
- //you can change this if you want to store the files in a different location.
- //this is relative to your webroot
- var $cachePaths = array('css' => 'ccss', 'js' => 'cjs');
var $paths = array('wwwRoot' => WWW_ROOT,
'js' => JS,
'css' => CSS);
var $foundFiles = array();
- //set the css compression level
- //options: default, low_compression, high_compression, highest_compression
- //default is no compression
- //I like high_compression because it still leaves the file readable.
- var $cssCompression = 'high_compression';
-
var $helpers = array('Html', 'Javascript');
var $viewScriptCount = 0;
var $initialized = false;
var $js = array();
+
var $css = array();
var $assets = array();
var $View = null;
- function __construct($paths=array()) {
- $this->paths = am($this->paths, $paths);
+ function __construct($options, $paths=array()) {
+ $this->options = array_merge($this->options, $options);
+ $this->paths = array_merge($this->paths, $paths);
$this->View =& ClassRegistry::getObject('view');
}
@@ -77,7 +85,7 @@ function scripts_for_layout($types=array('js', 'css', 'codeblock')) {
$this->__init();
}
- if (Configure::read('debug') && $this->debug == false) {
+ if (Configure::read('debug') && $this->options['debug'] == false) {
return join("\n\t", $this->View->__scripts);
}
@@ -90,11 +98,11 @@ function scripts_for_layout($types=array('js', 'css', 'codeblock')) {
switch($asset['type']) {
case 'js':
$processed = $this->__process($asset['type'], $asset['assets']);
- $scripts_for_layout[] = $this->Javascript->link('/' . $this->cachePaths['js'] . '/' . $processed);
+ $scripts_for_layout[] = $this->Javascript->link('/' . $this->options['cachePaths']['js'] . '/' . $processed);
break;
case 'css':
$processed = $this->__process($asset['type'], $asset['assets']);
- $scripts_for_layout[] = $this->Html->css('/' . $this->cachePaths['css'] . '/' . $processed);
+ $scripts_for_layout[] = $this->Html->css('/' . $this->options['cachePaths']['css'] . '/' . $processed);
break;
default:
$scripts_for_layout[] = $asset['assets']['script'];
@@ -118,7 +126,7 @@ function __init() {
array_slice($this->View->__scripts, $this->viewScriptCount),
array_slice($this->View->__scripts, 0, $this->viewScriptCount)
);
- if (Configure::read('debug') && $this->debug == false) {
+ if (Configure::read('debug') && $this->options['debug'] == false) {
return;
}
@@ -127,14 +135,6 @@ function __init() {
$this->Lang->init();
}
- if (Configure::read('Asset.jsPath')) {
- $this->cachePaths['js'] = Configure::read('Asset.jsPath');
- }
-
- if (Configure::read('Asset.cssPath')) {
- $this->cachePaths['css'] = Configure::read('Asset.cssPath');
- }
-
//split the scripts into js and css
$slot = 0;
$prev = '';
@@ -225,7 +225,7 @@ function __normalizeImageUrl($url) {
function __process($type, $assets) {
$path = $this->__getPath($type);
- $folder = new Folder($this->paths['wwwRoot'] . $this->cachePaths[$type], true);
+ $folder = new Folder($this->paths['wwwRoot'] . $this->options['cachePaths'][$type], true);
//check if the cached file exists
$scripts = Set::extract('/script', $assets);
@@ -237,8 +237,8 @@ function __process($type, $assets) {
//make sure all the pieces that went into the packed script
//are OLDER then the packed version
- if ($this->checkTs && $fileName) {
- $packed_ts = filemtime($this->paths['wwwRoot'] . $this->cachePaths[$type] . DS . $fileName);
+ if ($this->options['checkTs'] && $fileName) {
+ $packed_ts = filemtime($this->paths['wwwRoot'] . $this->options['cachePaths'][$type] . DS . $fileName);
$latest_ts = 0;
foreach($assets as $asset) {
@@ -251,7 +251,7 @@ function __process($type, $assets) {
//an original file is newer. need to rebuild
if ($latest_ts > $packed_ts) {
- unlink($this->paths['wwwRoot'] . $this->cachePaths[$type] . DS . $fileName);
+ unlink($this->paths['wwwRoot'] . $this->options['cachePaths'][$type] . DS . $fileName);
$fileName = null;
}
}
@@ -268,7 +268,7 @@ function __process($type, $assets) {
case 'css':
App::import('Vendor', 'csstidy', array('file' => 'class.csstidy.php'));
$tidy = new csstidy();
- $tidy->load_template($this->cssCompression);
+ $tidy->load_template($this->options['cssCompression']);
break;
}
@@ -287,7 +287,11 @@ function __process($type, $assets) {
break;
case 'css':
- $tidy->parse($this->__preprocessCss($asset, $buffer));
+ if($this->options['fixCssImg']) {
+ $buffer = $this->__preprocessCss($asset, $buffer);
+ }
+
+ $tidy->parse($buffer);
$buffer = $tidy->print->plain();
break;
}
@@ -302,7 +306,7 @@ function __process($type, $assets) {
//write the file
$fileName = $this->__generateFileName($scripts) . '_' . $ts . '.' . $type;
- $file = new File($this->paths['wwwRoot'] . $this->cachePaths[$type] . DS . $fileName);
+ $file = new File($this->paths['wwwRoot'] . $this->options['cachePaths'][$type] . DS . $fileName);
$file->write(trim($scriptBuffer));
}
@@ -343,9 +347,8 @@ function __findFile(&$asset, $type) {
}
$paths = array($this->__getPath($type));
- if (Configure::read('Asset.searchPaths')) {
- $paths = array_merge($paths, Configure::read('Asset.searchPaths'));
- }
+ $paths = array_merge($paths, $this->options['searchPaths']);
+
if (!empty($asset['plugin']) > 0) {
$pluginPaths = App::path('plugins');
@@ -390,7 +393,7 @@ function __findFile(&$asset, $type) {
function __generateFileName($names) {
$fileName = Sanitize::paranoid(str_replace('/', '-', implode('_', $names)), array('_', '-'));
- if ($this->md5FileName) {
+ if ($this->options['md5FileName']) {
$fileName = md5($fileName);
}

0 comments on commit 9a12910

Please sign in to comment.
Something went wrong with that request. Please try again.