get(); } /** * Implements hook_advagg_js_minify_configuration_alter(). */ function advagg_ext_minify_advagg_js_minify_configuration_alter(&$options_desc, &$compressors, &$functions) { // Test that shell_exec works - may be disabled on many hosts. if (!@shell_exec('ls') && !@shell_exec('dir')) { return; } // Test that an command has been configured. if (!\Drupal::config('advagg_ext_minify.settings')->get('js_cmd')) { return; } list($options, $description) = $options_desc; $options[] = t('AdvAgg Command Line Minifier'); $compressors[] = 'advagg_cmdline'; $functions[] = 'advagg_ext_minify_js_minify'; $options_desc = [$options, $description]; } /** * Implements hook_advagg_css_minify_configuration_alter(). */ function advagg_ext_minify_advagg_css_minify_configuration_alter(&$options_desc, &$compressors, &$functions) { // Test that shell_exec works - may be disabled on many hosts. if (!@shell_exec('ls') && !@shell_exec('dir')) { return; } // Test that an command has been configured. if (!\Drupal::config('advagg_ext_minify.settings')->get('js_cmd')) { return; } list($options, $description) = $options_desc; $options[] = t('AdvAgg Command Line Minifier'); $compressors[] = 'advagg_cmdline'; $functions[] = 'advagg_ext_minify_css_minify'; $options_desc = [$options, $description]; } // Helper functions. /** * Minify JavaScript via the command line. * * @param string $input_file * The file containing the unaltered js data. * @param string $ext * The string css or js. * * @return string * The file containing the minified js data. */ function advagg_ext_minify_execute_cmd($input_file, $ext = '') { // Get file extension. if (empty($ext)) { $ext = strtolower(pathinfo($input_file, PATHINFO_EXTENSION)); if ($ext !== 'css' && $ext !== 'js') { $info = \Drupal::service('state.advagg.files')->get($input_file); $ext = $info['fileext']; } } // Generate temp file. $temp_file = \Drupal::service("file_system")->tempnam('temporary://', 'file_advagg_'); $new_temp_file = $temp_file . '.' . basename($input_file); @rename($temp_file, $new_temp_file); $output = \Drupal::service('file_system')->realpath($new_temp_file); $run = \Drupal::config('advagg_ext_minify.settings')->get($ext . '_cmd'); $run = str_replace([ '{%CWD%}', '{%IN%}', '{%IN_URL_ENC%}', '{%OUT%}', ], [ \Drupal::root(), $input_file, urlencode(file_create_url($input_file)), escapeshellarg(realpath($output)), ], $run); // Run command and return the output file. shell_exec($run); return $output; } /** * Minify Javascript using via command line. * * @param string $contents * The JavaScript to minify. */ function advagg_ext_minify_js_minify(&$contents) { $js_path = \Drupal::config('advagg.settings')->get('root_dir_prefix') . 'advagg_js'; $temp_file = \Drupal::service("file_system")->tempnam($js_path, 'file_advagg_'); $new_temp_file = $temp_file . '.js'; rename($temp_file, $new_temp_file); $temp_file_full = advagg_get_relative_path($new_temp_file); file_put_contents($new_temp_file, $contents); $output = advagg_ext_minify_execute_cmd($temp_file_full, 'js'); $contents = file_get_contents($output); // Cleanup. unset($new_temp_file); unset($temp_file_full); unset($output); } /** * Minify CSS using via command line. * * @param string $contents * The CSS to minify. */ function advagg_ext_minify_css_minify(&$contents) { $css_path = \Drupal::config('advagg.settings')->get('root_dir_prefix') . 'advagg_css'; $temp_file = \Drupal::service("file_system")->tempnam($css_path, 'file_advagg_'); $new_temp_file = $temp_file . '.css'; rename($temp_file, $new_temp_file); $temp_file_full = advagg_get_relative_path($new_temp_file); file_put_contents($new_temp_file, $contents); $output = advagg_ext_minify_execute_cmd($temp_file_full, 'css'); $contents = file_get_contents($output); // Cleanup. unset($new_temp_file); unset($temp_file_full); unset($output); }