Chinaunix首页 | 论坛 | 博客
  • 博客访问: 65090
  • 博文数量: 20
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 198
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-31 17:15
文章分类

全部博文(20)

文章存档

2014年(1)

2013年(19)

我的朋友

分类: PHP

2013-10-15 18:43:13


点击(此处)折叠或打开

  1. #! /usr/bin/php
  2. <?php

  3. # ================================================================================
  4. # CSS Compression Programme
  5. # author: Zhang Jiuzhou
  6. # date : 2013-10-15
  7. # descp : compress multiple css files within several single files.
  8. # usage : php main.php source_folder [destination folder]


  9. # sapi check
  10. if (stripos(php_sapi_name(), 'cli') === false) {
  11.     throw new UsageError('This programme is cli only.');
  12. }

  13. # register exception handler
  14. set_exception_handler('exception_handler');

  15. # arg parse and validate
  16. if ($argc < 2) {
  17.     throw new UsageError('Require an argument of your source dir.');
  18. }
  19. $source = rtrim(trim($argv[1]), '/');
  20. check_source_dir($source);
  21. if ($argc >= 3) {
  22.     $destination = rtrim(trim($argv[2]), '/');
  23. } else {
  24.     $destination = rtrim(getcwd(), '/');
  25. }
  26. check_source_dir($destination);

  27. # make main dir
  28. date_default_timezone_set('Asia/Shanghai');
  29. $dirname = $destination . '/' . date('Ymd');
  30. if (is_dir($dirname)) {
  31.     throw new UsageError(sprintf('Dir %s is already exists.', $dirname));
  32. }
  33. if (!@mkdir($dirname)) {
  34.     throw new IOError(sprintf('Cannot make new dir %s.', $source));
  35. }

  36. # import files and compress
  37. chdir($source);
  38. $files = glob('*.css');
  39. $imported_list = array();
  40. foreach ($files as $file) {
  41.     read_and_import($file, $dirname . '/' . basename($file), $imported_list);
  42. }

  43. # copy files
  44. $source_real = rtrim(realpath($source), '/') . '/';
  45. chdir($dirname);
  46. foreach ($imported_list as $srcname) {
  47.     $src_relative_path = str_replace($source_real, '', $srcname);
  48.     $src_dir_relative_path = dirname($src_relative_path);
  49.     if (!is_dir($src_dir_relative_path)) {
  50.         if (!@mkdir($src_dir_relative_path, 0755, true)) {
  51.             throw new IOError(sprintf('Cannot create directory %s while copying files.', $src_dir_relative_path));
  52.         }
  53.     }
  54.     if (!@copy($srcname, $src_relative_path)) {
  55.         throw new IOError(sprintf('Cannot copy file %s.', $src_relative_path));
  56.     }
  57. }

  58. exit(0);

  59. # ================================================================================
  60. # library

  61. # class definitions
  62. class UsageError extends Exception {
  63. }

  64. class IOError extends Exception {
  65. }


  66. # function definitions
  67. /**
  68.  * Check path param
  69.  * @param str $dirname
  70.  */
  71. function check_source_dir($dirname) {
  72.     if (empty($dirname)) {
  73.         throw new UsageError('Path should not be empty.');
  74.     }
  75.     if (!is_dir($dirname)) {
  76.         throw new UsageError(sprintf('Path %s is not a valid directory.', $dirname));
  77.     }
  78. }

  79. /**
  80.  * Exception handler function
  81.  */
  82. function exception_handler(Exception $e) {
  83.     if ($e instanceof UsageError) {
  84.         echo sprintf('Usage error: %s' . PHP_EOL, $e->getMessage());
  85.         exit(1);
  86.     } else {
  87.         echo sprintf('Unchaught exception: %s' . PHP_EOL, $e->getMessage());
  88.         debug_print_backtrace();
  89.         exit(2);
  90.     }
  91. }

  92. /**
  93.  * Read data from files, "import" and compress.
  94.  * @param str $src;
  95.  * @param str $dest;
  96.  * @param Array & $imported_list;
  97.  */
  98. function read_and_import($src, $dst, Array & $imported_list) {
  99.     $out = @fopen($dst, 'w');
  100.     if (!$out) {
  101.         throw new IOError(sprintf('Cannot open destination file %s.', $dst));
  102.     }

  103.     _read_and_import($src, $out, $imported_list);
  104. }

  105. function _read_and_import($src, $out, & $import_list) {
  106.     $in_comment_block = false;
  107.     $in = @fopen($src, 'r');
  108.     if (!$in) {
  109.         throw new IOError(sprintf('Cannot open source file %s.', $src));
  110.     }

  111.     while (!feof($in)) {
  112.         $line = trim(fgets($in));
  113.         if (preg_match('/^@import "(.*)";$/i', trim($line), $matches)) {
  114.             $import = $matches[1];
  115.             $orign = getcwd();
  116.             chdir(dirname($src));
  117.             $import_list[] = realpath($import);
  118.             _read_and_import($import, $out, $import_list);
  119.             chdir($orign);
  120.         } else {
  121.             # remove single-line comment
  122.             $line = preg_replace('#/\*.*\*/#i', '', $line);
  123.             
  124.             # remove multi-line comment
  125.             $startpos = strpos($line, '/*');
  126.             $endpos = strpos($line, '*/');
  127.             if (!$in_comment_block && $startpos !== false) {
  128.                 $line = substr($line, 0, $startpos);
  129.                 $in_comment_block = true;
  130.             } else if ($in_comment_block && $endpos !== false) {
  131.                 $in_comment_block = false;
  132.                 $line = substr($line, $endpos + 2);
  133.             } else if ($in_comment_block) {
  134.                 $line = '';
  135.             }

  136.             # filter other spaces
  137.             $line = preg_replace('/\s*;\s*/', ';', $line);
  138.             $line = preg_replace('/\s*{\s*/', '{', $line);
  139.             $line = preg_replace('/\s*}\s*/', '}', $line);
  140.             $line = trim($line);
  141.             
  142.             if (strlen($line) > 0) {
  143.                 fputs($out, $line, strlen($line));
  144.             }
  145.         }
  146.     }

  147.     fclose($in);
  148. }

阅读(476) | 评论(2) | 转发(0) |
0

上一篇:技术分享PPT

下一篇:8个最佳PHP库

给主人留下些什么吧!~~

nbyh1002013-10-15 19:01:22

自己顶一下~