Permalink
Browse files

Fixed undefined sourcePath issue with HeadScriptBundler.

  • Loading branch information...
1 parent 5a1b2ab commit b3944045293b7b48245d6d2df0fbb74d7f8cdb92 @balupton balupton committed Jan 7, 2011
Showing with 49 additions and 48 deletions.
  1. +49 −48 lib/Bal/View/Helper/HeadScriptBundler.php
@@ -1,74 +1,74 @@
<?
class Bal_View_Helper_HeadScriptBundler extends Zend_View_Helper_HeadScript {
-
+
# =========================
# Custom: Variables
-
+
protected $_extension = 'js';
protected $_compiledOffset = null;
-
+
protected $_compiler = 'closure-webservice';
protected $_compilerPath = null;
-
+
/**
* Optional allowed attributes for script tag
* @var array
*/
protected $_optionalAttributes = array(
'charset', 'defer', 'language', 'src', 'compress'
);
-
+
# =========================
# Custom: Handling
-
+
protected function isCompressable($item){
return ($item->type === 'text/javascript') && (!empty($item->attributes['src']) || !empty($item->source));
}
-
+
protected function addFile($url){
- if ( $this->_compiledOffset !== null )
+ if ( $this->_compiledOffset !== null )
$this->offsetSetFile($this->_compiledOffset, $url);
else
$this->prependFile($url);
}
-
+
# =========================
# Custom: Paths
-
+
protected function getCachePath ( ) {
return CACHE_SCRIPTS_PATH;
}
-
+
protected function getCacheUrl ( ) {
return CACHE_SCRIPTS_URL;
}
-
+
# =========================
# Custom: Compilers
-
+
protected function compileConcat ( $files, $output_path ) {
file_put_contents($files,'');
foreach ( $files as $file ) {
$file_path = $file['path'];
file_put_contents($output_path,file_get_contents($file_path),FILE_APPEND);
}
}
-
+
protected function compileClosureWebservice ( $files, $output_path ) {
$Compiler = new Bal_Service_GoogleClosure();
$Compiler->compile($files,$output_path);
}
-
+
protected function compileClosureCompiler ( $files, $output_path ) {
# Check
if ( !$this->_compilerPath ) {
throw new Exception('Unknown Google Closure Compiler Path');
}
-
+
# Prepare Output File
file_put_contents($output_path,'');
-
+
# Compile Each File and Append to Output File
foreach ( $files as $file ) {
$out_path = $file_path = $file['path'];
@@ -80,69 +80,70 @@ protected function compileClosureCompiler ( $files, $output_path ) {
file_put_contents($output_path,file_get_contents($out_path),FILE_APPEND);
}
}
-
+
# =========================
# Generic
-
+
public function setCompiler( $value ) {
$this->_compiler = $value;
return $this;
}
-
+
public function setCompilerPath( $value ) {
$this->_compilerPath = $value;
return $this;
}
-
+
public function setCompiledOffset ( $value ) {
$this->_compiledOffset = $value;
return $this;
}
-
+
protected function isMinified ( $path ) {
$minified = preg_match('/[\.\/\-](min(ified)?|compressed)[\.\/\-]/i', $path);
return $minified ? true : false;
}
-
+
protected function compile($files,$path){
$compiler = str_replace(' ','',ucwords(str_replace('-',' ',$this->_compiler)));
$function = 'compile'.$compiler;
-
+
if ( method_exists($this,$function) ) {
$this->$function($files,$path);
}
else {
throw new Exception('Compiler ['.$this->_compiler.']['.$function.'] not supported.');
}
-
+
return true;
}
-
+
public function toString ($indent = null) {
# Sort the Items
- $this->getContainer()->ksort();
-
+ $this->getContainer()->ksort();
+
# Prepare
$files = array();
$hash = '';
+ $sourcePath = '';
$refresh = false;
$error = false;
-
+
# Cycle Through the Items
foreach ($this as $key => $item) {
# Is the Item Valid?
if ( !$this->isCompressable($item) ) {
continue;
}
-
+
# Handle Item (Source or URL)
if ( !empty($item->source) ) {
# Generate File Name
$source = $item->source;
$filename = 'source-'.md5($source).'.'.$this->_extension;
$sourcePath = $this->getCachePath().'/'.$filename;
$url = ROOT_URL.$this->getCacheUrl().'/'.$filename;
-
+
# Write to file
if ( !file_exists($sourcePath) ) {
// we can do an if here, as the filename is based on the contents, if the contents has changed the filename would be different
@@ -158,7 +159,7 @@ public function toString ($indent = null) {
elseif ( strpos($url,'http') === false ) {
$url = ROOT_URL.BASE_URL.$url;
}
-
+
# Determine Original Path
if ( strpos($url,ROOT_URL) === 0 ) {
// We are a local file, so determine the full path from the base url
@@ -168,13 +169,13 @@ public function toString ($indent = null) {
}
}
}
-
+
# Determine Cache Path
$cachePath = $this->getCachePath().'/cached-'.preg_replace('/[^a-zA-Z0-9\.]+/','-',$url);
-
+
# Ensure Correct Path
- $sourcePath = str_replace('//','/',$sourcePath);
-
+ $sourcePath = $sourcePath ? str_replace('//','/',$sourcePath) : false;
+
# Apply
$minified = $this->isMinified($cachePath);
$compress = !$minified && (isset($item->attributes['compress']) && $item->attributes['compress'] !== 'false');
@@ -188,13 +189,13 @@ public function toString ($indent = null) {
);
$hash .= $url;
}
-
+
# Hash
$hash = md5($hash);
$compiledFileName = 'compiled-'.$hash.'.'.$this->_extension;
$compiledFileUrl = $this->getCacheUrl().'/'.$compiledFileName;
$compiledFilePath = $this->getCachePath().'/'.$compiledFileName;
-
+
# Get last modified time of cache file
if ( is_file($compiledFilePath) && filesize($compiledFilePath) ) {
$compiledFilemtime = filemtime($compiledFilePath);
@@ -204,14 +205,14 @@ public function toString ($indent = null) {
$refresh = true;
$compiledFilemtime = 0;
}
-
+
# Determine if Refresh is Needed
foreach ( $files as $key => $file ) {
# Preset
$url = $file['url'];
$sourcePath = $file['sourcePath'];
$cachePath = $file['cachePath'];
-
+
# Determine Cache Modified Time
if ( !is_file($cachePath) ) {
touch($cachePath);
@@ -220,15 +221,15 @@ public function toString ($indent = null) {
else {
$cacheFilemtime = filemtime($cachePath);
}
-
+
# Determine Real Modified Time
if ( $sourcePath ) {
$filemtime = filemtime($sourcePath);
}
else {
$filemtime = $cacheFilemtime;
}
-
+
# Should we refresh?
if ( $filemtime >= $cacheFilemtime ) {
$refresh = true;
@@ -244,7 +245,7 @@ public function toString ($indent = null) {
$refresh = true;
}
}
-
+
# Refresh the Cache File
if ( $refresh ) {
try {
@@ -261,28 +262,28 @@ public function toString ($indent = null) {
}
$compiledFilemtime = filemtime($compiledFilePath);
}
-
+
# Delete the Files
if ( !$error ) {
foreach ( $files as $key => $url ) {
unset($this[$key]);
}
-
+
# Use the Cached File
$compiledUrl = $compiledFileUrl.'?'.$compiledFilemtime;
$this->addFile($compiledUrl);
}
-
+
# Let's hand back to our parent
$result = parent::toString($indent);
-
+
# Add Refresh Status
if ( $refresh && !$error ) {
$Log = Bal_Log::getInstance();
$log_details = array();
$Log->log(array('log-scripts-refreshed',$log_details),Bal_Log::NOTICE,array('friendly'=>true,'class'=>'success','details'=>$log_details));
}
-
+
# Return result
return $result;
}

0 comments on commit b394404

Please sign in to comment.