utils.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Utility functions for generating group URIs in HTML files
  4. *
  5. * Before including this file, /min/lib must be in your include_path.
  6. *
  7. * @package Minify
  8. */
  9. require_once 'Minify/Build.php';
  10. /**
  11. * Get a timestamped URI to a minified resource using the default Minify install
  12. *
  13. * <code>
  14. * <link rel="stylesheet" type="text/css" href="<?php echo Minify_groupUri('css'); ?>" />
  15. * <script type="text/javascript" src="<?php echo Minify_groupUri('js'); ?>"></script>
  16. * </code>
  17. *
  18. * If you do not want ampersands as HTML entities, set Minify_Build::$ampersand = "&"
  19. * before using this function.
  20. *
  21. * @param string $group a key from groupsConfig.php
  22. * @param boolean $forceAmpersand (default false) Set to true if the RewriteRule
  23. * directives in .htaccess are functional. This will remove the "?" from URIs, making them
  24. * more cacheable by proxies.
  25. * @return string
  26. */
  27. function Minify_groupUri($group, $forceAmpersand = false)
  28. {
  29. $path = $forceAmpersand
  30. ? "/g={$group}"
  31. : "/?g={$group}";
  32. return _Minify_getBuild($group)->uri(
  33. '/' . basename(dirname(__FILE__)) . $path
  34. ,$forceAmpersand
  35. );
  36. }
  37. /**
  38. * Get the last modification time of the source js/css files used by Minify to
  39. * build the page.
  40. *
  41. * If you're caching the output of Minify_groupUri(), you'll want to rebuild
  42. * the cache if it's older than this timestamp.
  43. *
  44. * <code>
  45. * // simplistic HTML cache system
  46. * $file = '/path/to/cache/file';
  47. * if (! file_exists($file) || filemtime($file) < Minify_groupsMtime(array('js', 'css'))) {
  48. * // (re)build cache
  49. * $page = buildPage(); // this calls Minify_groupUri() for js and css
  50. * file_put_contents($file, $page);
  51. * echo $page;
  52. * exit();
  53. * }
  54. * readfile($file);
  55. * </code>
  56. *
  57. * @param array $groups an array of keys from groupsConfig.php
  58. * @return int Unix timestamp of the latest modification
  59. */
  60. function Minify_groupsMtime($groups)
  61. {
  62. $max = 0;
  63. foreach ((array)$groups as $group) {
  64. $max = max($max, _Minify_getBuild($group)->lastModified);
  65. }
  66. return $max;
  67. }
  68. /**
  69. * @param string $group a key from groupsConfig.php
  70. * @return Minify_Build
  71. * @private
  72. */
  73. function _Minify_getBuild($group)
  74. {
  75. static $builds = array();
  76. static $gc = false;
  77. if (false === $gc) {
  78. $gc = (require dirname(__FILE__) . '/groupsConfig.php');
  79. }
  80. if (! isset($builds[$group])) {
  81. $builds[$group] = new Minify_Build($gc[$group]);
  82. }
  83. return $builds[$group];
  84. }