utils.php 2.3 KB

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