Build.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Class Minify_Build
  4. * @package Minify
  5. */
  6. /**
  7. * Maintain a single last modification time for a group of Minify sources to
  8. * allow use of far off Expires headers in Minify.
  9. *
  10. * <code>
  11. * // in config file
  12. * $groupSources = array(
  13. * 'js' => array('file1.js', 'file2.js')
  14. * ,'css' => array('file1.css', 'file2.css', 'file3.css')
  15. * )
  16. *
  17. * // during HTML generation
  18. * $jsBuild = new Minify_Build($groupSources['js']);
  19. * $cssBuild = new Minify_Build($groupSources['css']);
  20. *
  21. * $script = "<script type='text/javascript' src='"
  22. * . $jsBuild->uri('/min.php/js') . "'></script>";
  23. * $link = "<link rel='stylesheet' type='text/css' href='"
  24. * . $cssBuild->uri('/min.php/css') . "'>";
  25. *
  26. * // in min.php
  27. * Minify::serve('Groups', array(
  28. * 'groups' => $groupSources
  29. * ,'setExpires' => (time() + 86400 * 365)
  30. * ));
  31. * </code>
  32. *
  33. * @package Minify
  34. * @author Stephen Clay <steve@mrclay.org>
  35. */
  36. class Minify_Build {
  37. /**
  38. * Last modification time of all files in the build
  39. *
  40. * @var int
  41. */
  42. public $lastModified = 0;
  43. /**
  44. * String to use as ampersand in uri(). Set this to '&' if
  45. * you are not HTML-escaping URIs.
  46. *
  47. * @var string
  48. */
  49. public static $ampersand = '&amp;';
  50. /**
  51. * Get a time-stamped URI
  52. *
  53. * <code>
  54. * echo $b->uri('/site.js');
  55. * // outputs "/site.js?1678242"
  56. *
  57. * echo $b->uri('/scriptaculous.js?load=effects');
  58. * // outputs "/scriptaculous.js?load=effects&amp1678242"
  59. * </code>
  60. *
  61. * @param string $uri
  62. * @param boolean $forceAmpersand (default = false) Force the use of ampersand to
  63. * append the timestamp to the URI.
  64. * @return string
  65. */
  66. public function uri($uri, $forceAmpersand = false) {
  67. $sep = ($forceAmpersand || strpos($uri, '?') !== false)
  68. ? self::$ampersand
  69. : '?';
  70. return "{$uri}{$sep}{$this->lastModified}";
  71. }
  72. /**
  73. * Create a build object
  74. *
  75. * @param array $sources array of Minify_Source objects and/or file paths
  76. *
  77. * @return null
  78. */
  79. public function __construct($sources)
  80. {
  81. $max = 0;
  82. foreach ((array)$sources as $source) {
  83. if ($source instanceof Minify_Source) {
  84. $max = max($max, $source->lastModified);
  85. } elseif (is_string($source)) {
  86. if (0 === strpos($source, '//')) {
  87. $source = $_SERVER['DOCUMENT_ROOT'] . substr($source, 1);
  88. }
  89. if (is_file($source)) {
  90. $max = max($max, filemtime($source));
  91. }
  92. }
  93. }
  94. $this->lastModified = $max;
  95. }
  96. }