Min.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Qii\Library;
  3. use Qii\Library\Min\CssMin;
  4. use Qii\Library\Min\JSMin;
  5. class Min
  6. {
  7. const CSS_TYPE = 'text/css';
  8. const JS_TYPE = 'application/x-javascript';
  9. const CSS = 'css';
  10. const JS = 'js';
  11. protected $expired = 0;
  12. /**
  13. * Min constructor.
  14. */
  15. public function __construct()
  16. {
  17. //默认5分钟
  18. $this->expired = strtotime("now +5 minutes");
  19. }
  20. /**
  21. * 设置过期时间 以当前时间戳+过期的秒数
  22. *
  23. * @param $minutes
  24. * @return void
  25. */
  26. public function setExpired($minutes) {
  27. $this->expired = strtotime("now +".$minutes." minutes");
  28. return $this;
  29. }
  30. /**
  31. * Min constructor.
  32. * @param array $map ['type' => 'js/css', 'files' => []]
  33. * @return string
  34. */
  35. public function minify($map)
  36. {
  37. if(empty($map)) return;
  38. if(empty($map['type'])) return;
  39. if(!in_array($map['type'], [self::CSS, self::JS]))
  40. {
  41. throw new MinException('Unsupported files');
  42. }
  43. switch ($map['type'])
  44. {
  45. case self::CSS:
  46. return $this->minifyCSS($map['files'], $map['root'] ?? '', $map['version'] ?? '');
  47. break;
  48. case self::JS:
  49. return $this->minifyJS($map['files']);
  50. break;
  51. default:
  52. return '';
  53. break;
  54. }
  55. return '';
  56. }
  57. public function minifyCSS($files, $root = '', $version = '')
  58. {
  59. if(is_array($files))
  60. {
  61. foreach($files AS $file)
  62. {
  63. $css[] = $this->minifyCSS($file);
  64. }
  65. return join("\n", $css);
  66. }
  67. if(!is_file($files))
  68. {
  69. return "/*文件'". $files ."'未找到*/";
  70. }
  71. $content = $this->getContent($files);
  72. $currentDir = dirname($files);
  73. return CssMin::rewrite($content, $currentDir, $version, $root);
  74. }
  75. /**
  76. * @param array $files 文件列表
  77. * @return array
  78. */
  79. public function minifyJS($files)
  80. {
  81. if(is_array($files))
  82. {
  83. $js = [];
  84. foreach ($files as $file)
  85. {
  86. $js[] = $this->minifyJS($file);
  87. }
  88. return join("\n", $js);
  89. }
  90. if(!is_file($files))
  91. {
  92. return "/*文件'". $files ."'未找到*/";
  93. }
  94. return JSMin::minify($this->getContent($files));
  95. }
  96. /**
  97. * 返回文件内容
  98. * @param string $file 文件名称
  99. * @return bool|string
  100. */
  101. protected function getContent($file)
  102. {
  103. $content = file_get_contents($file);
  104. // remove UTF-8 BOM
  105. return (pack("CCC", 0xef, 0xbb, 0xbf) === substr($content, 0, 3)) ? substr($content, 3) : $content;
  106. }
  107. /**
  108. * @param string $type 类型
  109. */
  110. public function sendHeader($type)
  111. {
  112. $header = [
  113. 'js' => 'Content-Type: application/x-javascript',
  114. 'css' => 'Content-Type: text/css',
  115. ];
  116. if(!isset($header[$type]))
  117. {
  118. return;
  119. }
  120. header("Access-Control-Allow-Origin:'*'");
  121. header("Expires: " . date("D, j M Y H:i:s", $this->expired) ." GMT");
  122. header($header[$type]);
  123. return $this;
  124. }
  125. }
  126. class MinException extends \Exception{
  127. }