Interim commit.
[yaffs-website] / web / modules / contrib / advagg / advagg_ext_minify / advagg_ext_minify.module
1 <?php
2
3 /**
4  * @file
5  * Advanced CSS/JS aggregation external minification module.
6  */
7
8 // Contrib hook implementations.
9 /**
10  * Implements hook_advagg_current_hooks_hash_array_alter().
11  */
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();
14 }
15
16 /**
17  * Implements hook_advagg_js_minify_configuration_alter().
18  */
19 function advagg_ext_minify_advagg_js_minify_configuration_alter(&$options_desc, &$compressors, &$functions) {
20
21   // Test that shell_exec works - may be disabled on many hosts.
22   if (!@shell_exec('ls') && !@shell_exec('dir')) {
23     return;
24   }
25
26   // Test that an command has been configured.
27   if (!\Drupal::config('advagg_ext_minify.settings')->get('js_cmd')) {
28     return;
29   }
30
31   list($options, $description) = $options_desc;
32   $options[] = t('AdvAgg Command Line Minifier');
33   $compressors[] = 'advagg_cmdline';
34   $functions[] = 'advagg_ext_minify_js_minify';
35
36   $options_desc = [$options, $description];
37 }
38
39 /**
40  * Implements hook_advagg_css_minify_configuration_alter().
41  */
42 function advagg_ext_minify_advagg_css_minify_configuration_alter(&$options_desc, &$compressors, &$functions) {
43
44   // Test that shell_exec works - may be disabled on many hosts.
45   if (!@shell_exec('ls') && !@shell_exec('dir')) {
46     return;
47   }
48
49   // Test that an command has been configured.
50   if (!\Drupal::config('advagg_ext_minify.settings')->get('js_cmd')) {
51     return;
52   }
53
54   list($options, $description) = $options_desc;
55   $options[] = t('AdvAgg Command Line Minifier');
56   $compressors[] = 'advagg_cmdline';
57   $functions[] = 'advagg_ext_minify_css_minify';
58
59   $options_desc = [$options, $description];
60 }
61
62 // Helper functions.
63 /**
64  * Minify JavaScript via the command line.
65  *
66  * @param string $input_file
67  *   The file containing the unaltered js data.
68  * @param string $ext
69  *   The string css or js.
70  *
71  * @return string
72  *   The file containing the minified js data.
73  */
74 function advagg_ext_minify_execute_cmd($input_file, $ext = '') {
75   // Get file extension.
76   if (empty($ext)) {
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'];
81     }
82   }
83
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');
90
91   $run = str_replace([
92     '{%CWD%}',
93     '{%IN%}',
94     '{%IN_URL_ENC%}',
95     '{%OUT%}',
96   ], [
97     \Drupal::root(),
98     $input_file,
99     urlencode(file_create_url($input_file)),
100     escapeshellarg(realpath($output)),
101   ], $run);
102
103   // Run command and return the output file.
104   shell_exec($run);
105   return $output;
106 }
107
108 /**
109  * Minify Javascript using via command line.
110  *
111  * @param string $contents
112  *   The JavaScript to minify.
113  */
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);
120
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);
124
125   // Cleanup.
126   unset($new_temp_file);
127   unset($temp_file_full);
128   unset($output);
129 }
130
131 /**
132  * Minify CSS using via command line.
133  *
134  * @param string $contents
135  *   The CSS to minify.
136  */
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);
143
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);
147
148   // Cleanup.
149   unset($new_temp_file);
150   unset($temp_file_full);
151   unset($output);
152 }