advagg_js_compress.module

  1. nittany7 modules/contrib/advagg/advagg_js_compress/advagg_js_compress.module
  2. cis7 modules/contrib/advagg/advagg_js_compress/advagg_js_compress.module
  3. mooc7 modules/contrib/advagg/advagg_js_compress/advagg_js_compress.module

Advanced CSS/JS aggregation js compression module.

Functions

Namesort descending Description
advagg_js_compress_advagg_current_hooks_hash_array_alter Implement hook_advagg_current_hooks_hash_array_alter.
advagg_js_compress_advagg_modify_js_pre_render_alter Implement hook_advagg_modify_js_pre_render_alter().
advagg_js_compress_menu Implementation of hook_menu
advagg_js_compress_test_file Test a file, making sure it is compressible.

Constants

Namesort descending Description
ADVAGG_JS_COMPRESSOR Default value to see what compressor to use. 0 is Disabled.
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

modules/contrib/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. * Implementation of hook_menu
  32. */
  33. function advagg_js_compress_menu() {
  34. $items = array();
  35. $file_path = drupal_get_path('module', 'advagg_js_compress');
  36. $config_path = advagg_admin_config_root_path();
  37. $items[$config_path . '/advagg/js-compress'] = array(
  38. 'title' => 'JS Compression',
  39. 'description' => 'Adjust JS Compression settings.',
  40. 'page callback' => 'drupal_get_form',
  41. 'page arguments' => array('advagg_js_compress_admin_settings_form'),
  42. 'type' => MENU_LOCAL_TASK,
  43. 'access arguments' => array('administer site configuration'),
  44. 'file path' => $file_path,
  45. 'file' => 'advagg_js_compress.admin.inc',
  46. 'weight' => 10,
  47. );
  48. return $items;
  49. }
  50. /**
  51. * Implement hook_advagg_current_hooks_hash_array_alter.
  52. */
  53. function advagg_js_compress_advagg_current_hooks_hash_array_alter(&$aggregate_settings) {
  54. $aggregate_settings['variables']['advagg_js_compressor'] = variable_get('advagg_js_compressor', ADVAGG_JS_COMPRESSOR);
  55. $aggregate_settings['variables']['advagg_js_compress_packer'] = variable_get('advagg_js_compress_packer', ADVAGG_JS_COMPRESS_PACKER);
  56. $aggregate_settings['variables']['advagg_js_max_compress_ratio'] = variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO);
  57. }
  58. /**
  59. * Implement hook_advagg_modify_js_pre_render_alter().
  60. *
  61. * Used compress inline js.
  62. */
  63. function advagg_js_compress_advagg_modify_js_pre_render_alter(&$children, &$elements) {
  64. // Get variables.
  65. $aggregate_settings['variables']['advagg_js_compressor'] = variable_get('advagg_js_inline_compressor', ADVAGG_JS_INLINE_COMPRESSOR);
  66. $aggregate_settings['variables']['advagg_js_max_compress_ratio'] = variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO);
  67. // Do nothing if the compressor is disabled.
  68. if (empty($aggregate_settings['variables']['advagg_js_compressor'])) {
  69. return;
  70. }
  71. // Do nothing if the page is not cacheable and inline compress if not
  72. // cacheable is not checked.
  73. if (!variable_get('advagg_js_inline_compress_if_not_cacheable', ADVAGG_JS_INLINE_COMPRESS_IF_NOT_CACHEABLE) && !drupal_page_is_cacheable()) {
  74. return;
  75. }
  76. // Compress any inline JS.
  77. module_load_include('inc', 'advagg_js_compress', 'advagg_js_compress.advagg');
  78. foreach ($children as $key => &$values) {
  79. if (!empty($values['#value'])) {
  80. $contents = $values['#value'];
  81. $filename = drupal_hash_base64($contents);
  82. advagg_js_compress_prep($contents, $filename, $aggregate_settings, FALSE);
  83. $values['#value'] = $contents;
  84. }
  85. }
  86. }
  87. /**
  88. * Test a file, making sure it is compressible.
  89. *
  90. * @param $filename
  91. * Path and filename of the js file to test.
  92. * @param $compressors
  93. * List of compressors to test.
  94. * @return
  95. * Array showing the results of the compression tests.
  96. */
  97. function advagg_js_compress_test_file($filename, $compressors) {
  98. $contents = file_get_contents($filename);
  99. // Get the JS string length before the compression operation.
  100. $contents_before = $contents;
  101. $before = strlen($contents);
  102. module_load_include('inc', 'advagg_js_compress', 'advagg_js_compress.advagg');
  103. $results = array();
  104. foreach ($compressors as $key => $name) {
  105. $contents = $contents_before;
  106. $aggregate_settings['variables']['advagg_js_compressor'] = $key;
  107. $aggregate_settings['variables']['advagg_js_max_compress_ratio'] = variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO);
  108. // Compress it.
  109. advagg_js_compress_prep($contents, $filename, $aggregate_settings, FALSE, FALSE, FALSE);
  110. $after = strlen($contents);
  111. $ratio = 0;
  112. if ($before != 0) {
  113. $ratio = ($before - $after) / $before;
  114. }
  115. // Set to "-2" if compression ratio sucks (it's already compressed).
  116. if ($ratio < variable_get('advagg_js_compress_ratio', ADVAGG_JS_COMPRESS_RATIO)) {
  117. $results[$key] = array('code' => -2, 'ratio' => round($ratio, 5), 'name' => $name);
  118. }
  119. // Set to "-3" if the compression ratio is way too good (bad js output).
  120. elseif ($ratio > variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO)) {
  121. $results[$key] = array('code' => -3, 'ratio' => round($ratio, 5), 'name' => $name);
  122. }
  123. // Set to "1". Everything worked, mark this file as compressible.
  124. else {
  125. $results[$key] = array('code' => 1, 'ratio' => round($ratio, 5), 'name' => $name);
  126. }
  127. }
  128. return $results;
  129. }