BuildLiteBehavior.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Behavior;
  12. // 创建Lite运行文件
  13. // 可以替换框架入口文件运行
  14. // 建议绑定位置app_init
  15. class BuildLiteBehavior {
  16. public function run(&$params) {
  17. if(!defined('BUILD_LITE_FILE')) return ;
  18. $litefile = C('RUNTIME_LITE_FILE',null,RUNTIME_PATH.'lite.php');
  19. if(is_file($litefile)) return;
  20. $defs = get_defined_constants(TRUE);
  21. $content = 'namespace {$GLOBALS[\'_beginTime\'] = microtime(TRUE);';
  22. if(MEMORY_LIMIT_ON) {
  23. $content .= '$GLOBALS[\'_startUseMems\'] = memory_get_usage();';
  24. }
  25. // 生成数组定义
  26. unset($defs['user']['BUILD_LITE_FILE']);
  27. $content .= $this->buildArrayDefine($defs['user']).'}';
  28. // 读取编译列表文件
  29. $filelist = is_file(CONF_PATH.'lite.php')?
  30. include CONF_PATH.'lite.php':
  31. array(
  32. THINK_PATH.'Common/functions.php',
  33. COMMON_PATH.'Common/function.php',
  34. CORE_PATH . 'Think'.EXT,
  35. CORE_PATH . 'Hook'.EXT,
  36. CORE_PATH . 'App'.EXT,
  37. CORE_PATH . 'Dispatcher'.EXT,
  38. CORE_PATH . 'Log'.EXT,
  39. CORE_PATH . 'Log/Driver/File'.EXT,
  40. CORE_PATH . 'Route'.EXT,
  41. CORE_PATH . 'Controller'.EXT,
  42. CORE_PATH . 'View'.EXT,
  43. CORE_PATH . 'Storage'.EXT,
  44. CORE_PATH . 'Storage/Driver/File'.EXT,
  45. CORE_PATH . 'Exception'.EXT,
  46. BEHAVIOR_PATH . 'ParseTemplateBehavior'.EXT,
  47. BEHAVIOR_PATH . 'ContentReplaceBehavior'.EXT,
  48. );
  49. // 编译文件
  50. foreach ($filelist as $file){
  51. if(is_file($file)) {
  52. $content .= compile($file);
  53. }
  54. }
  55. // 处理Think类的start方法
  56. $content = preg_replace('/\$runtimefile = RUNTIME_PATH(.+?)(if\(APP_STATUS)/','\2',$content,1);
  57. $content .= "\nnamespace { Think\Think::addMap(".var_export(\Think\Think::getMap(),true).");";
  58. $content .= "\nL(".var_export(L(),true).");\nC(".var_export(C(),true).');Think\Hook::import('.var_export(\Think\Hook::get(),true).');Think\Think::start();}';
  59. // 生成运行Lite文件
  60. file_put_contents($litefile,strip_whitespace('<?php '.$content));
  61. }
  62. // 根据数组生成常量定义
  63. private function buildArrayDefine($array) {
  64. $content = "\n";
  65. foreach ($array as $key => $val) {
  66. $key = strtoupper($key);
  67. $content .= 'defined(\'' . $key . '\') or ';
  68. if (is_int($val) || is_float($val)) {
  69. $content .= "define('" . $key . "'," . $val . ');';
  70. } elseif (is_bool($val)) {
  71. $val = ($val) ? 'true' : 'false';
  72. $content .= "define('" . $key . "'," . $val . ');';
  73. } elseif (is_string($val)) {
  74. $content .= "define('" . $key . "','" . addslashes($val) . "');";
  75. }
  76. $content .= "\n";
  77. }
  78. return $content;
  79. }
  80. }