utils.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Utility functions for generating URIs in HTML files
  4. *
  5. * @warning These functions execute min/groupsConfig.php, sometimes multiple times.
  6. * You must make sure that functions are not redefined, and if your use custom sources,
  7. * you must require_once dirname(__FILE__) . '/lib/Minify/Source.php' so that
  8. * class is available.
  9. *
  10. * @package Minify
  11. */
  12. if (! class_exists('Minify_Loader', false)) {
  13. require dirname(__FILE__) . '/lib/Minify/Loader.php';
  14. Minify_Loader::register();
  15. }
  16. /*
  17. * Get an HTML-escaped Minify URI for a group or set of files. By default, URIs
  18. * will contain timestamps to allow far-future Expires headers.
  19. *
  20. * <code>
  21. * <link rel="stylesheet" type="text/css" href="<?= Minify_getUri('css'); ?>" />
  22. * <script src="<?= Minify_getUri('js'); ?>"></script>
  23. * <script src="<?= Minify_getUri(array(
  24. * '//scripts/file1.js'
  25. * ,'//scripts/file2.js'
  26. * )); ?>"></script>
  27. * </code>
  28. *
  29. * @param mixed $keyOrFiles a group key or array of file paths/URIs
  30. * @param array $opts options:
  31. * 'farExpires' : (default true) append a modified timestamp for cache revving
  32. * 'debug' : (default false) append debug flag
  33. * 'charset' : (default 'UTF-8') for htmlspecialchars
  34. * 'minAppUri' : (default '/min') URI of min directory
  35. * 'rewriteWorks' : (default true) does mod_rewrite work in min app?
  36. * 'groupsConfigFile' : specify if different
  37. * @return string
  38. */
  39. function Minify_getUri($keyOrFiles, $opts = array())
  40. {
  41. return Minify_HTML_Helper::getUri($keyOrFiles, $opts);
  42. }
  43. /**
  44. * Get the last modification time of several source js/css files. If you're
  45. * caching the output of Minify_getUri(), you might want to know if one of the
  46. * dependent source files has changed so you can update the HTML.
  47. *
  48. * Since this makes a bunch of stat() calls, you might not want to check this
  49. * on every request.
  50. *
  51. * @param array $keysAndFiles group keys and/or file paths/URIs.
  52. * @return int latest modification time of all given keys/files
  53. */
  54. function Minify_mtime($keysAndFiles, $groupsConfigFile = null)
  55. {
  56. $gc = null;
  57. if (! $groupsConfigFile) {
  58. $groupsConfigFile = dirname(__FILE__) . '/groupsConfig.php';
  59. }
  60. $sources = array();
  61. foreach ($keysAndFiles as $keyOrFile) {
  62. if (is_object($keyOrFile)
  63. || 0 === strpos($keyOrFile, '/')
  64. || 1 === strpos($keyOrFile, ':\\')) {
  65. // a file/source obj
  66. $sources[] = $keyOrFile;
  67. } else {
  68. if (! $gc) {
  69. $gc = (require $groupsConfigFile);
  70. }
  71. foreach ($gc[$keyOrFile] as $source) {
  72. $sources[] = $source;
  73. }
  74. }
  75. }
  76. return Minify_HTML_Helper::getLastModified($sources);
  77. }