5 * Advanced CSS/JS aggregation external minification module.
8 // Contrib hook implementations.
10 * Implements hook_advagg_current_hooks_hash_array_alter().
12 function advagg_ext_minify_advagg_current_hooks_hash_array_alter(&$aggregate_settings) {
13 $aggregate_settings['variables']['advagg_ext_minify'] = \Drupal::config('advagg_ext_minify.settings')->get();
17 * Implements hook_advagg_js_minify_configuration_alter().
19 function advagg_ext_minify_advagg_js_minify_configuration_alter(&$options_desc, &$compressors, &$functions) {
21 // Test that shell_exec works - may be disabled on many hosts.
22 if (!@shell_exec('ls') && !@shell_exec('dir')) {
26 // Test that an command has been configured.
27 if (!\Drupal::config('advagg_ext_minify.settings')->get('js_cmd')) {
31 list($options, $description) = $options_desc;
32 $options[] = t('AdvAgg Command Line Minifier');
33 $compressors[] = 'advagg_cmdline';
34 $functions[] = 'advagg_ext_minify_js_minify';
36 $options_desc = [$options, $description];
40 * Implements hook_advagg_css_minify_configuration_alter().
42 function advagg_ext_minify_advagg_css_minify_configuration_alter(&$options_desc, &$compressors, &$functions) {
44 // Test that shell_exec works - may be disabled on many hosts.
45 if (!@shell_exec('ls') && !@shell_exec('dir')) {
49 // Test that an command has been configured.
50 if (!\Drupal::config('advagg_ext_minify.settings')->get('js_cmd')) {
54 list($options, $description) = $options_desc;
55 $options[] = t('AdvAgg Command Line Minifier');
56 $compressors[] = 'advagg_cmdline';
57 $functions[] = 'advagg_ext_minify_css_minify';
59 $options_desc = [$options, $description];
64 * Minify JavaScript via the command line.
66 * @param string $input_file
67 * The file containing the unaltered js data.
69 * The string css or js.
72 * The file containing the minified js data.
74 function advagg_ext_minify_execute_cmd($input_file, $ext = '') {
75 // Get file extension.
77 $ext = strtolower(pathinfo($input_file, PATHINFO_EXTENSION));
78 if ($ext !== 'css' && $ext !== 'js') {
79 $info = \Drupal::service('state.advagg.files')->get($input_file);
80 $ext = $info['fileext'];
84 // Generate temp file.
85 $temp_file = \Drupal::service("file_system")->tempnam('temporary://', 'file_advagg_');
86 $new_temp_file = $temp_file . '.' . basename($input_file);
87 @rename($temp_file, $new_temp_file);
88 $output = \Drupal::service('file_system')->realpath($new_temp_file);
89 $run = \Drupal::config('advagg_ext_minify.settings')->get($ext . '_cmd');
99 urlencode(file_create_url($input_file)),
100 escapeshellarg(realpath($output)),
103 // Run command and return the output file.
109 * Minify Javascript using via command line.
111 * @param string $contents
112 * The JavaScript to minify.
114 function advagg_ext_minify_js_minify(&$contents) {
115 $js_path = \Drupal::config('advagg.settings')->get('root_dir_prefix') . 'advagg_js';
116 $temp_file = \Drupal::service("file_system")->tempnam($js_path, 'file_advagg_');
117 $new_temp_file = $temp_file . '.js';
118 rename($temp_file, $new_temp_file);
119 $temp_file_full = advagg_get_relative_path($new_temp_file);
121 file_put_contents($new_temp_file, $contents);
122 $output = advagg_ext_minify_execute_cmd($temp_file_full, 'js');
123 $contents = file_get_contents($output);
126 unset($new_temp_file);
127 unset($temp_file_full);
132 * Minify CSS using via command line.
134 * @param string $contents
137 function advagg_ext_minify_css_minify(&$contents) {
138 $css_path = \Drupal::config('advagg.settings')->get('root_dir_prefix') . 'advagg_css';
139 $temp_file = \Drupal::service("file_system")->tempnam($css_path, 'file_advagg_');
140 $new_temp_file = $temp_file . '.css';
141 rename($temp_file, $new_temp_file);
142 $temp_file_full = advagg_get_relative_path($new_temp_file);
144 file_put_contents($new_temp_file, $contents);
145 $output = advagg_ext_minify_execute_cmd($temp_file_full, 'css');
146 $contents = file_get_contents($output);
149 unset($new_temp_file);
150 unset($temp_file_full);