Build.php 2.7 KB

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