Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

125 lines (112 sloc) 5.496 kb
<?php
// $Id: javascript_aggregator.module,v 1.17.2.1 2008/11/26 23:54:27 robloach Exp $
/**
* Implementation of hook_help().
*/
function compressor_help($path, $arg) {
switch ($path) {
case 'admin/help#javascript_aggregator':
$output = '<p>'. t('Uses the <a href="@jsmin">JSMin</a> library to minify the aggregated JavaScript file when <em>JavaScript optimization</em> has been enabled in the <a href="@performance">Performance settings</a>.', array('@performance' => url('admin/settings/performance'), '@jsmin' => 'http://code.google.com/p/jsmin-php/')) .'</p>';
return $output;
}
}
function getYuiCompressorPath()
{
$path = dirname(__FILE__) . '/yuicompressor-2.4.7.jar';
return variable_get('yuicompressorpath', $path);
}
/**
* Implementation of hook_form_alter().
*/
function compressor_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'system_performance_settings') {
$form['bandwidth_optimizations']['preprocess_js']['#title'] = t('Optimize and YUI Compress JavaScript files');
$form['bandwidth_optimizations']['preprocess_css']['#title'] = t('Optimize and YUI Compress CSS files');
$form['bandwidth_optimizations']['yuicompressorpath'] = array(
'#type' => 'textfield',
'#title' => t('Path to the yiu-compressor. <br>Download it into ' . $path . ''),
'#default_value' => getYuiCompressorPath(),
'#size' => 120,
);
$form['bandwidth_optimizations']['minify_js']['#description'] .= t(' Once the JavaScript files have been aggregated, they will be minified.');
}
}
/**
* Implementation of hook_preprocess_hook().
*
* Minify the aggregated JavaScript file if JavaScript Optimization is turned on.
*/
function compressor_preprocess_page(&$variables) {
$compressor = getYuiCompressorPath();
global $user;
// Only do this for pages that have CSS on them.
if (!empty($variables['styles'])) {
// Only process it is CSS Optimization is enabled.
if (variable_get('preprocess_css', 0)) {
// Strip out the aggregated JavaScript file.
$path = base_path() . file_directory_path();
$pattern = "|href=\"$path(.*)\"|mU";
preg_match_all($pattern, $variables['styles'], $matches);
$css = $variables['styles'];
$dir_file = file_directory_path();
if(substr($dir_file,0,1) != '/') {
$dir_file = getcwd() .'/'. $dir_file;
}
foreach($matches[1] as $aggregated_file) {
$css_file_path = file_directory_path() . $aggregated_file;
$cssmin_file_path = str_replace('.css', '.min.css', $css_file_path);
// Create the JSMinified file if it doesn't exist yet.
if (!file_exists($cssmin_file_path)) {
exec("java -jar $compressor --type=css -o $cssmin_file_path $css_file_path 2>&1", $out, $err);
if($err) {
drupal_set_message(t("<b>YuiCompressor crash</b> Check the CSS file and java disponibility. Don't forget to clear cache when you fix CSS. <pre>@crash</pre>", array('@crash' => join("\n", $out))), 'error');
watchdog('yuicompressor', 'yuicompressor can\'t do the job, maybe the css is invalid : '. $css_file_path, WATCHDOG_ERROR);
} else {
if($user->uid == 1) {
drupal_set_message(t('CSS compression of @file : @score %',
array(
'@file' => $css_file_path,
'@score' => round( 100 * filesize($cssmin_file_path) / filesize($css_file_path))))
, 'status');
}
}
}
}
$css = str_replace('.css', '.min.css', $css);
// Replace the aggregated file with the minified JavaScript file.
$variables['styles'] = $css;
}
}
// Only do this for pages that have JavaScript on them.
if (!empty($variables['scripts'])) {
// Only process it is JavaScript Optimization is enabled.
if (variable_get('preprocess_js', 0)) {
// Strip out the aggregated JavaScript file.
$path = base_path() . file_directory_path();
$pattern = "!(<script type=\"text\/javascript\" src=\"$path)(.*?)(\"(.*?)><\/script>)!";
preg_match_all($pattern, $variables['scripts'], $matches);
$aggregated_file = $matches[2][0];
$jsmin_file = $aggregated_file .'min.js';
$jsmin_file_path = file_directory_path() .$jsmin_file;
// Create the JSMinified file if it doesn't exist yet.
if (!file_exists($jsmin_file_path)) {
$js_file_path = file_directory_path() . $aggregated_file;
exec("java -jar $compressor --type=js -o $jsmin_file_path $js_file_path 2>&1", $out, $err);
if($err) {
drupal_set_message(t("<b>YuiCompressor crash</b> Check the JS file and java disponibility. Don't forget to clear cache when you fix CSS. <pre>@crash</pre>", array('@crash' => join("\n", $out))), 'error');
watchdog('yuicompressor', 'yuicompressor can\'t do the job, maybe the js is invalid : '. $js_file_path, WATCHDOG_ERROR);
} else {
if($user->uid == 1) {
drupal_set_message(t('JS compression of @file : @score %',
array(
'@file' => $css_file_path,
'@score' => round( 100 * filesize($jsmin_file_path) / filesize($js_file_path))))
, 'status');
}
}
}
// Replace the aggregated file with the minified JavaScript file.
$variables['scripts'] = str_replace($aggregated_file, $jsmin_file, $variables['scripts']);
}
}
}
Jump to Line
Something went wrong with that request. Please try again.