advagg_js_compress.module

  1. cis7 sites/all/modules/ulmus/advagg/advagg_js_compress/advagg_js_compress.module
  2. cle7 sites/all/modules/ulmus/advagg/advagg_js_compress/advagg_js_compress.module
  3. ecd7 sites/all/modules/ulmus/advagg/advagg_js_compress/advagg_js_compress.module
  4. elmsmedia7 sites/all/modules/ulmus/advagg/advagg_js_compress/advagg_js_compress.module
  5. harmony7 sites/all/modules/ulmus/advagg/advagg_js_compress/advagg_js_compress.module
  6. icor7 sites/all/modules/ulmus/advagg/advagg_js_compress/advagg_js_compress.module
  7. meedjum_blog7 sites/all/modules/ulmus/advagg/advagg_js_compress/advagg_js_compress.module
  8. mooc7 sites/all/modules/ulmus/advagg/advagg_js_compress/advagg_js_compress.module

Advanced CSS/JS aggregation js compression module.

Functions

Constants

Namesort descending Description
ADVAGG_JS_COMPRESSOR Default value to see what compressor to use. 0 is Disabled.
ADVAGG_JS_COMPRESSOR_FILE_SETTINGS Default value for per file compression settings.
ADVAGG_JS_COMPRESS_PACKER Default value to see packer is enabled.
ADVAGG_JS_COMPRESS_RATIO Default value for the compression ratio test.
ADVAGG_JS_INLINE_COMPRESSOR Default value to see what compressor to use. 0 is Disabled.
ADVAGG_JS_INLINE_COMPRESS_IF_NOT_CACHEABLE Default value to if inline compression is used if page is not cacheable.
ADVAGG_JS_MAX_COMPRESS_RATIO Default value for the compression ratio test.

File

sites/all/modules/ulmus/advagg/advagg_js_compress/advagg_js_compress.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Advanced CSS/JS aggregation js compression module.
  5. */
  6. /**
  7. * Default value to see packer is enabled.
  8. */
  9. define('ADVAGG_JS_COMPRESS_PACKER', FALSE);
  10. /**
  11. * Default value to see what compressor to use. 0 is Disabled.
  12. */
  13. define('ADVAGG_JS_COMPRESSOR', 0);
  14. /**
  15. * Default value to see what compressor to use. 0 is Disabled.
  16. */
  17. define('ADVAGG_JS_INLINE_COMPRESSOR', 0);
  18. /**
  19. * Default value to if inline compression is used if page is not cacheable.
  20. */
  21. define('ADVAGG_JS_INLINE_COMPRESS_IF_NOT_CACHEABLE', FALSE);
  22. /**
  23. * Default value for the compression ratio test.
  24. */
  25. define('ADVAGG_JS_COMPRESS_RATIO', 0.1);
  26. /**
  27. * Default value for the compression ratio test.
  28. */
  29. define('ADVAGG_JS_MAX_COMPRESS_RATIO', 0.9);
  30. /**
  31. * Default value for per file compression settings.
  32. */
  33. define('ADVAGG_JS_COMPRESSOR_FILE_SETTINGS', -1);
  34. /**
  35. * Implements hook_menu().
  36. */
  37. function advagg_js_compress_menu() {
  38. $file_path = drupal_get_path('module', 'advagg_js_compress');
  39. $config_path = advagg_admin_config_root_path();
  40. $items[$config_path . '/advagg/js-compress'] = array(
  41. 'title' => 'JS Compression',
  42. 'description' => 'Adjust JS Compression settings.',
  43. 'page callback' => 'drupal_get_form',
  44. 'page arguments' => array('advagg_js_compress_admin_settings_form'),
  45. 'type' => MENU_LOCAL_TASK,
  46. 'access arguments' => array('administer site configuration'),
  47. 'file path' => $file_path,
  48. 'file' => 'advagg_js_compress.admin.inc',
  49. 'weight' => 10,
  50. );
  51. return $items;
  52. }
  53. /**
  54. * Implements hook_advagg_current_hooks_hash_array_alter().
  55. */
  56. function advagg_js_compress_advagg_current_hooks_hash_array_alter(&$aggregate_settings) {
  57. $aggregate_settings['variables']['advagg_js_compressor'] = variable_get('advagg_js_compressor', ADVAGG_JS_COMPRESSOR);
  58. $aggregate_settings['variables']['advagg_js_compress_packer'] = variable_get('advagg_js_compress_packer', ADVAGG_JS_COMPRESS_PACKER);
  59. $aggregate_settings['variables']['advagg_js_max_compress_ratio'] = variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO);
  60. $aggregate_settings['variables']['advagg_js_compressor_file_settings'] = variable_get('advagg_js_compressor_file_settings', array());
  61. }
  62. /**
  63. * Implements hook_advagg_modify_js_pre_render_alter().
  64. *
  65. * Used compress inline js.
  66. */
  67. function advagg_js_compress_advagg_modify_js_pre_render_alter(&$children, &$elements) {
  68. // Get variables.
  69. $aggregate_settings['variables']['advagg_js_compressor'] = variable_get('advagg_js_inline_compressor', ADVAGG_JS_INLINE_COMPRESSOR);
  70. $aggregate_settings['variables']['advagg_js_max_compress_ratio'] = variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO);
  71. // Do nothing if the compressor is disabled.
  72. if (empty($aggregate_settings['variables']['advagg_js_compressor'])) {
  73. return;
  74. }
  75. // Do nothing if the page is not cacheable and inline compress if not
  76. // cacheable is not checked.
  77. if (!variable_get('advagg_js_inline_compress_if_not_cacheable', ADVAGG_JS_INLINE_COMPRESS_IF_NOT_CACHEABLE) && !drupal_page_is_cacheable()) {
  78. return;
  79. }
  80. // Compress any inline JS.
  81. module_load_include('inc', 'advagg_js_compress', 'advagg_js_compress.advagg');
  82. foreach ($children as $key => &$values) {
  83. if (!empty($values['#value'])) {
  84. $contents = $values['#value'];
  85. $filename = drupal_hash_base64($contents);
  86. advagg_js_compress_prep($contents, $filename, $aggregate_settings, FALSE);
  87. $values['#value'] = $contents;
  88. }
  89. }
  90. }
  91. /**
  92. * Test a file, making sure it is compressible.
  93. *
  94. * @param string $filename
  95. * Path and filename of the js file to test.
  96. * @param array $compressors
  97. * List of compressors to test.
  98. *
  99. * @return array
  100. * Array showing the results of the compression tests.
  101. */
  102. function advagg_js_compress_test_file($filename, $compressors) {
  103. $contents = file_get_contents($filename);
  104. // Get the JS string length before the compression operation.
  105. $contents_before = $contents;
  106. $before = strlen($contents);
  107. module_load_include('inc', 'advagg_js_compress', 'advagg_js_compress.advagg');
  108. $results = array();
  109. foreach ($compressors as $key => $name) {
  110. $contents = $contents_before;
  111. $aggregate_settings['variables']['advagg_js_compressor'] = $key;
  112. $aggregate_settings['variables']['advagg_js_max_compress_ratio'] = variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO);
  113. // Compress it.
  114. advagg_js_compress_prep($contents, $filename, $aggregate_settings, FALSE, FALSE, FALSE);
  115. $after = strlen($contents);
  116. $ratio = 0;
  117. if ($before != 0) {
  118. $ratio = ($before - $after) / $before;
  119. }
  120. // Set to "-2" if compression ratio sucks (it's already compressed).
  121. if ($ratio < variable_get('advagg_js_compress_ratio', ADVAGG_JS_COMPRESS_RATIO)) {
  122. $results[$key] = array(
  123. 'code' => -2,
  124. 'ratio' => round($ratio, 5),
  125. 'name' => $name,
  126. );
  127. }
  128. // Set to "-3" if the compression ratio is way too good (bad js output).
  129. elseif ($ratio > variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO)) {
  130. $results[$key] = array(
  131. 'code' => -3,
  132. 'ratio' => round($ratio, 5),
  133. 'name' => $name,
  134. );
  135. }
  136. // Set to "1". Everything worked, mark this file as compressible.
  137. else {
  138. $results[$key] = array(
  139. 'code' => 1,
  140. 'ratio' => round($ratio, 5),
  141. 'name' => $name,
  142. );
  143. }
  144. }
  145. return $results;
  146. }
  147. /**
  148. * Test a JS file to see if it compresses well; called from httprl background.
  149. *
  150. * @param string $filename
  151. * Path and filename of JS file.
  152. *
  153. * @return array
  154. * info about the file.
  155. */
  156. function advagg_js_compress_run_test_httprl($filename) {
  157. module_load_include('inc', 'advagg_js_compress', 'advagg_js_compress.advagg');
  158. return advagg_js_compress_run_test($filename);
  159. }