Created
February 24, 2012 02:13
-
-
Save rchavik/1896709 to your computer and use it in GitHub Desktop.
quick and dirty minify helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$pp = CakePlugin::path('Minify') . 'Vendor' .DS. 'Compressor' .DS; | |
App::build(array( | |
'Vendor' => array($pp), | |
)); | |
App::uses('JSMin', 'Vendor'); | |
App::import('Vendor', 'Minify.Compressor_Minify_CSS_Compressor', array( | |
'file' => 'Compressor/Minify/CSS/Compressor.php', | |
)); | |
class MinifyHelper extends AppHelper { | |
public $helpers = array( | |
'Html', | |
); | |
/** | |
* @param array $scripts to minify | |
* @param array $options theme | |
*/ | |
public function script($scripts, $options = array()) { | |
if (Configure::read('debug') || Configure::read('Site.minify') === false) { | |
return $this->Html->script($scripts); | |
} | |
$options = Set::merge(array( | |
'theme' => $this->_View->theme, | |
'plugin' => false, | |
'subdir' => false, | |
), $options); | |
extract($options); | |
if (!empty($theme)) { | |
$path = App::themePath($theme); | |
} elseif (!empty($plugin)) { | |
$path = CakePlugin::pluginPath($plugin); | |
} | |
$targetDirectory = $path .DS. 'webroot' .DS. 'js' .DS; | |
$outputfile = $targetDirectory .DS. $subdir .DS. 'minified-' . sha1(join(':', $scripts)) . '.js'; | |
if (file_exists($outputfile)) { | |
$outputfile = str_replace($targetDirectory . '/', '', $outputfile); | |
return $this->Html->script($outputfile); | |
} | |
$contents = ''; | |
foreach ($scripts as $script) { | |
$file = $targetDirectory . $script; | |
if (!preg_match('/\.js$/', $file)) { | |
$file .= '.js'; | |
} | |
$contents .= ";\r" . file_get_contents($file); | |
} | |
$contents = JSMin::minify($contents); | |
file_put_contents($outputfile, $contents); | |
return $this->Html->script($scripts); | |
} | |
/** | |
* @param array $scripts to minify | |
* @param array $options theme | |
*/ | |
public function css($scripts, $options = array()) { | |
if (Configure::read('debug') || Configure::read('Site.minify') === false) { | |
return $this->Html->css($scripts); | |
} | |
$options = Set::merge(array( | |
'theme' => $this->_View->theme, | |
'plugin' => false, | |
'subdir' => false, | |
), $options); | |
extract($options); | |
if (!empty($theme)) { | |
$path = App::themePath($theme); | |
} elseif (!empty($plugin)) { | |
$path = CakePlugin::pluginPath($plugin); | |
} | |
$targetDirectory = $path .DS. 'webroot' .DS. 'css' .DS; | |
$outputfile = $targetDirectory .DS. $subdir .DS. 'minified-' . sha1(join(':', $scripts)) . '.css'; | |
if (file_exists($outputfile)) { | |
$outputfile = str_replace($targetDirectory . '/', '', $outputfile); | |
return $this->Html->css($outputfile); | |
} | |
$contents = ''; | |
foreach ($scripts as $script) { | |
$file = $targetDirectory . $script; | |
if (!preg_match('/\.css$/', $file)) { | |
$file .= '.css'; | |
} | |
$contents .= file_get_contents($file); | |
} | |
$contents = Minify_CSS_Compressor::process($contents); | |
file_put_contents($outputfile, $contents); | |
return $this->Html->css($scripts); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment