123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace Qii\Library;
- use Qii\Library\Min\CssMin;
- use Qii\Library\Min\JSMin;
- class Min
- {
- const CSS_TYPE = 'text/css';
- const JS_TYPE = 'application/x-javascript';
- const CSS = 'css';
- const JS = 'js';
- protected $expired = 0;
- /**
- * Min constructor.
- */
- public function __construct()
- {
- //默认5分钟
- $this->expired = strtotime("now +5 minutes");
- }
- /**
- * 设置过期时间 以当前时间戳+过期的秒数
- *
- * @param $minutes
- * @return void
- */
- public function setExpired($minutes) {
- $this->expired = strtotime("now +".$minutes." minutes");
- return $this;
- }
- /**
- * Min constructor.
- * @param array $map ['type' => 'js/css', 'files' => []]
- * @return string
- */
- public function minify($map)
- {
- if(empty($map)) return;
- if(empty($map['type'])) return;
- if(!in_array($map['type'], [self::CSS, self::JS]))
- {
- throw new MinException('Unsupported files');
- }
- switch ($map['type'])
- {
- case self::CSS:
- return $this->minifyCSS($map['files'], $map['root'] ?? '', $map['version'] ?? '');
- break;
- case self::JS:
- return $this->minifyJS($map['files']);
- break;
- default:
- return '';
- break;
- }
- return '';
- }
- public function minifyCSS($files, $root = '', $version = '')
- {
- if(is_array($files))
- {
- foreach($files AS $file)
- {
- $css[] = $this->minifyCSS($file);
- }
- return join("\n", $css);
- }
- if(!is_file($files))
- {
- return "/*文件'". $files ."'未找到*/";
- }
- $content = $this->getContent($files);
- $currentDir = dirname($files);
- return CssMin::rewrite($content, $currentDir, $version, $root);
- }
- /**
- * @param array $files 文件列表
- * @return array
- */
- public function minifyJS($files)
- {
- if(is_array($files))
- {
- $js = [];
- foreach ($files as $file)
- {
- $js[] = $this->minifyJS($file);
- }
- return join("\n", $js);
- }
- if(!is_file($files))
- {
- return "/*文件'". $files ."'未找到*/";
- }
- return JSMin::minify($this->getContent($files));
- }
- /**
- * 返回文件内容
- * @param string $file 文件名称
- * @return bool|string
- */
- protected function getContent($file)
- {
- $content = file_get_contents($file);
- // remove UTF-8 BOM
- return (pack("CCC", 0xef, 0xbb, 0xbf) === substr($content, 0, 3)) ? substr($content, 3) : $content;
- }
- /**
- * @param string $type 类型
- */
- public function sendHeader($type)
- {
- $header = [
- 'js' => 'Content-Type: application/x-javascript',
- 'css' => 'Content-Type: text/css',
- ];
- if(!isset($header[$type]))
- {
- return;
- }
- header("Access-Control-Allow-Origin:'*'");
- header("Expires: " . date("D, j M Y H:i:s", $this->expired) ." GMT");
- header($header[$type]);
- return $this;
- }
- }
- class MinException extends \Exception{
- }
|