build.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * 压缩框架
  4. */
  5. defined('InShopNC') or exit('Access Invalid!');
  6. /**
  7. * 压缩框架文件
  8. *
  9. */
  10. function build(){
  11. $args = func_get_args();
  12. extract($args[0]);
  13. $compile = '';
  14. $list = array(
  15. BASE_PATH.'/framework/core/runtime.php',
  16. BASE_PATH.'/framework/core/base.php',
  17. BASE_PATH.'/framework/core/model.php',
  18. BASE_PATH.'/framework/cache/cache.php',
  19. BASE_PATH.'/framework/cache/cache.file.php',
  20. BASE_PATH.'/framework/db/'.strtolower($dbdriver).'.php',
  21. BASE_PATH.'/framework/function/goods.php',
  22. BASE_PATH.'/framework/libraries/email.php',
  23. BASE_PATH.'/framework/libraries/language.php',
  24. BASE_PATH.'/framework/libraries/log.php',
  25. BASE_PATH.'/framework/libraries/page.php',
  26. BASE_PATH.'/framework/libraries/security.php',
  27. BASE_PATH.'/framework/libraries/validator.php',
  28. BASE_PATH.'/framework/libraries/upload.php',
  29. BASE_PATH.'/framework/function/core.php',
  30. BASE_PATH.'/framework/tpl/nc.php',
  31. BASE_PATH.'/control/control.php',
  32. BASE_PATH.'/language/'.$lang_type.'/core_lang_index.php',
  33. BASE_PATH.'/classes/process.class.php'
  34. );
  35. if (!empty($cache_type) && strtolower($cache_type) != 'file'){
  36. $list[] = BASE_PATH.'/framework/cache/cache.'.strtolower($cache_type).'.php';
  37. }
  38. foreach ($list as $file) {
  39. if (file_exists($file)) {
  40. $compile .= compile($file);
  41. }else{
  42. exit(str_replace(BASE_PATH,'',$file)." isn't exists!");
  43. }
  44. }
  45. //加载核心语言包
  46. // $lang_file = BASE_PATH.'/language/'.$lang_type.'/core_lang_index.php';
  47. // if (!file_exists($lang_file)){
  48. // exit(str_replace(BASE_PATH,'',$lang_file)." isn't exists!");
  49. // }
  50. $compile .= compile($lang_file);
  51. $compile .= "\nLanguage::appendLanguage(\$lang);";
  52. $compile .= "\nBase::run();";
  53. file_put_contents(RUNCOREPATH,compress_code("<?php defined('InShopNC') or exit('Access Invalid!');".$compile));
  54. }
  55. /**
  56. * 过滤掉不需要压缩的内容
  57. *
  58. * @param string $filename 待压缩文件
  59. * @return string
  60. */
  61. function compile($filename) {
  62. $content = file_get_contents($filename);
  63. //过滤不需要编译的内容
  64. $content = preg_replace('/\/\/\[NC_SKIPBUILD\](.*?)\/\/\[\/NC_SKIPBUILD\]/s', '', $content);
  65. $content = str_ireplace("defined('InShopNC') or exit('Access Invalid!')", '', $content);
  66. $content = substr(trim($content), 5);
  67. if ('?>' == substr($content, -2))
  68. $content = substr($content, 0, -2);
  69. return $content;
  70. }
  71. /**
  72. * 压缩PHP代码
  73. *
  74. * @param string $content 压缩内容
  75. * @return string
  76. */
  77. function compress_code($content) {
  78. $strip_str = '';
  79. //分析php源码
  80. $tokens = token_get_all($content);
  81. $last_space = false;
  82. for ($i = 0, $j = count($tokens); $i < $j; $i++) {
  83. if (is_string($tokens[$i])) {
  84. $last_space = false;
  85. $strip_str .= $tokens[$i];
  86. } else {
  87. switch ($tokens[$i][0]) {
  88. //过滤各种PHP注释
  89. case T_COMMENT:
  90. case T_DOC_COMMENT:
  91. break;
  92. //过滤空格
  93. case T_WHITESPACE:
  94. if (!$last_space) {
  95. $strip_str .= ' ';
  96. $last_space = true;
  97. }
  98. break;
  99. default:
  100. $last_space = false;
  101. $strip_str .= $tokens[$i][1];
  102. }
  103. }
  104. }
  105. return $strip_str;
  106. }
  107. ?>