advagg.advagg.inc

  1. nittany7 modules/contrib/advagg/advagg.advagg.inc
  2. cis7 modules/contrib/advagg/advagg.advagg.inc
  3. mooc7 modules/contrib/advagg/advagg.advagg.inc

Advanced CSS/JS aggregation module.

File used to store hook_advagg_* hooks.

Functions

File

modules/contrib/advagg/advagg.advagg.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Advanced CSS/JS aggregation module.
  5. *
  6. * File used to store hook_advagg_* hooks.
  7. */
  8. /**
  9. * Implements hook_advagg_save_aggregate_alter().
  10. *
  11. * Used to add in a .gz file if none exits.
  12. *
  13. * @param $files_to_save
  14. * array($uri => $contents)
  15. * @param $aggregate_settings
  16. * array of settings.
  17. * @param $other_parameters
  18. * array of containing $files & $type.
  19. */
  20. function advagg_advagg_save_aggregate_alter(&$files_to_save, $aggregate_settings, $other_parameters) {
  21. // Return if gzip is disabled.
  22. if (empty($aggregate_settings['variables']['advagg_gzip'])) {
  23. return;
  24. }
  25. // See if a .gz file already exists.
  26. $gzip_exists = FALSE;
  27. foreach ($files_to_save as $uri => $contents) {
  28. // See if this uri contains .gz near the end of it.
  29. $pos = strripos($uri, '.gz', 91 + strlen(ADVAGG_SPACE)*3);
  30. if (!empty($pos)) {
  31. $len = strlen($uri);
  32. // .gz file exists, exit loop.
  33. if ($pos == $len-3) {
  34. $gzip_exists = TRUE;
  35. break;
  36. }
  37. }
  38. }
  39. // If a .gz file does not exist, create one.
  40. if (!$gzip_exists) {
  41. // Use the first file in the array.
  42. $data = reset($files_to_save);
  43. $uri = key($files_to_save);
  44. // Compress it and add it to the $files_to_save array.
  45. $compressed = gzencode($data, 9, FORCE_GZIP);
  46. $files_to_save[$uri . '.gz'] = $compressed;
  47. }
  48. }
  49. /**
  50. * Implements hook_advagg_build_aggregate_plans_alter().
  51. *
  52. * Used to alter the plan so it has the same grouping as cores.
  53. *
  54. * @param $files
  55. * List of files in the aggregate as well as the aggregate name.
  56. * @param $modified
  57. * Change this to TRUE if $files has been changed.
  58. * @param $type
  59. * css or js.
  60. */
  61. function advagg_advagg_build_aggregate_plans_alter(&$files, &$modified, $type) {
  62. // Do nothing if core grouping is disabled.
  63. if (!variable_get('advagg_core_groups', ADVAGG_CORE_GROUPS)) {
  64. return;
  65. }
  66. $temp_new_files = array();
  67. $counter = 0;
  68. foreach ($files as $filename => $data) {
  69. $group = NULL;
  70. $every_page = NULL;
  71. foreach ($data['files'] as $fileinfo) {
  72. // Grouped by group & every_page variables.
  73. if (is_null($group)) {
  74. $group = $fileinfo['group'];
  75. }
  76. if (is_null($every_page)) {
  77. $every_page = $fileinfo['every_page'];
  78. }
  79. // Bump Counter if group/every_page has changed from the last one.
  80. if ($group != $fileinfo['group'] || $every_page != $fileinfo['every_page']) {
  81. $counter++;
  82. $group = $fileinfo['group'];
  83. $every_page = $fileinfo['every_page'];
  84. $modified = TRUE;
  85. }
  86. $temp_new_files[$counter][] = $fileinfo;
  87. }
  88. $counter++;
  89. }
  90. // Replace $files array with new aggregate filenames.
  91. $files = advagg_generate_filenames(array($temp_new_files), $type);
  92. }