/lib/minify.php

https://github.com/eizzumdm/Kurogo-Mobile-Web · PHP · 194 lines · 138 code · 32 blank · 24 comment · 20 complexity · 63c3e11f569d28da2bf07d96c4c46638 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Minify
  4. */
  5. /**
  6. */
  7. require_once LIB_DIR.'/DiskCache.php';
  8. //
  9. // Handle CSS and Javascript a little differently:
  10. //
  11. // CSS supports overrides so include all available CSS files.
  12. function getCSSFileConfigForDirs($page, $pagetype, $platform, $dirs, $subDirs, $pageOnly=false) {
  13. $config = array(
  14. 'include' => 'all',
  15. 'files' => array()
  16. );
  17. foreach ($dirs as $dir) {
  18. foreach ($subDirs as $subDir) {
  19. if (!$pageOnly) {
  20. $config['files'][] = "$dir$subDir/css/common.css";
  21. $config['files'][] = "$dir$subDir/css/$pagetype.css";
  22. $config['files'][] = "$dir$subDir/css/$pagetype-$platform.css";
  23. }
  24. $config['files'][] = "$dir$subDir/css/$page-common.css";
  25. $config['files'][] = "$dir$subDir/css/$page-$pagetype.css";
  26. $config['files'][] = "$dir$subDir/css/$page-$pagetype-$platform.css";
  27. }
  28. }
  29. return $config;
  30. }
  31. // Javascript does not support overrides so include common files
  32. // and the most specific platform file. Themes override js.
  33. function getJSFileConfigForDirs($page, $pagetype, $platform, $dirs, $subDirs, $pageOnly=false) {
  34. $config = array(
  35. 'include' => 'all',
  36. 'files' => array()
  37. );
  38. foreach ($subDirs as $subDir) {
  39. $dirConfig = array(
  40. 'include' => 'any',
  41. 'files' => array()
  42. );
  43. foreach ($dirs as $dir) {
  44. $files = array(
  45. 'include' => 'all',
  46. 'files' => array(),
  47. );
  48. if (!$pageOnly) {
  49. $files['files'][] = "$dir$subDir/javascript/common.js";
  50. $files['files'][] = array(
  51. 'include' => 'any',
  52. 'files' => array(
  53. "$dir$subDir/javascript/$pagetype-$platform.js",
  54. "$dir$subDir/javascript/$pagetype.js",
  55. ),
  56. );
  57. }
  58. $files['files'][] = "$dir$subDir/javascript/$page-common.js";
  59. $files['files'][] = array(
  60. 'include' => 'any',
  61. 'files' => array(
  62. "$dir$subDir/javascript/$page-$pagetype-$platform.js",
  63. "$dir$subDir/javascript/$page-$pagetype.js",
  64. ),
  65. );
  66. $dirConfig['files'][] = $files;
  67. }
  68. $config['files'][] = $dirConfig;
  69. }
  70. return $config;
  71. }
  72. function buildFileList($checkFiles) {
  73. $foundFiles = array();
  74. foreach ($checkFiles['files'] as $entry) {
  75. if (is_array($entry)) {
  76. $foundFiles = array_merge($foundFiles, buildFileList($entry));
  77. } else if (realpath_exists($entry)) {
  78. $foundFiles[] = $entry;
  79. }
  80. if ($checkFiles['include'] == 'any' && count($foundFiles)) {
  81. break;
  82. }
  83. }
  84. return $foundFiles;
  85. }
  86. function getMinifyGroupsConfig() {
  87. $minifyConfig = array();
  88. $key = $_GET['g'];
  89. //
  90. // Check for specific file request
  91. //
  92. if (strpos($key, MIN_FILE_PREFIX) === 0) {
  93. // file path relative to either templates or the theme (check theme first)
  94. $path = substr($key, strlen(MIN_FILE_PREFIX));
  95. $config = array(
  96. 'include' => 'all',
  97. 'files' => array(
  98. THEME_DIR.$path,
  99. SITE_DIR.$path,
  100. TEMPLATES_DIR.$path,
  101. ),
  102. );
  103. return array($key => buildFileList($config));
  104. }
  105. //
  106. // Page request
  107. //
  108. $pageOnly = isset($_GET['pageOnly']) && $_GET['pageOnly'];
  109. list($ext, $module, $page, $pagetype, $platform, $pathHash) = explode('-', $key);
  110. $cache = new DiskCache(CACHE_DIR.'/minify', 30, true);
  111. $cacheName = "group_$key";
  112. if ($cache->isFresh($cacheName)) {
  113. $minifyConfig = $cache->read($cacheName);
  114. } else {
  115. // CSS includes all in order. JS prefers theme
  116. $cssDirs = array(
  117. TEMPLATES_DIR,
  118. SITE_DIR,
  119. THEME_DIR,
  120. );
  121. $jsDirs = array(
  122. THEME_DIR,
  123. SITE_DIR,
  124. TEMPLATES_DIR,
  125. );
  126. if ($pageOnly || $module == 'info') {
  127. // Info module does not inherit from common files
  128. $subDirs = array(
  129. '/modules/'.$module,
  130. );
  131. } else {
  132. $subDirs = array(
  133. '/common',
  134. '/modules/'.$module,
  135. );
  136. }
  137. $checkFiles = array(
  138. 'css' => getCSSFileConfigForDirs(
  139. $page, $pagetype, $platform, $cssDirs, $subDirs, $pageOnly),
  140. 'js' => getJSFileConfigForDirs (
  141. $page, $pagetype, $platform, $jsDirs, $subDirs, $pageOnly),
  142. );
  143. //error_log(print_r($checkFiles, true));
  144. $minifyConfig[$key] = buildFileList($checkFiles[$ext]);
  145. //error_log(__FUNCTION__."($pagetype-$platform) scanned filesystem for $key");
  146. $cache->write($minifyConfig, $cacheName);
  147. }
  148. //error_log(__FUNCTION__."($pagetype-$platform) returning: ".print_r($minifyConfig, true));
  149. return $minifyConfig;
  150. }
  151. function minifyPostProcess($content, $type) {
  152. if ($type === Minify::TYPE_CSS) {
  153. $urlPrefix = URL_PREFIX;
  154. if ($GLOBALS['siteConfig']->getVar('DEVICE_DEBUG') && URL_PREFIX == URL_BASE) {
  155. // if device debugging is on, always append device classification
  156. $urlPrefix .= 'device/'.$GLOBALS['deviceClassifier']->getDevice().'/';
  157. }
  158. $content = "/* Adding url prefix '".$urlPrefix."' */\n\n".
  159. preg_replace(';url\("?\'?/([^"\'\)]+)"?\'?\);', 'url("'.$urlPrefix.'\1")', $content);
  160. }
  161. return $content;
  162. }