runtime.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 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. // $Id$
  12. // 生成核心编译缓存
  13. function build_runtime() {
  14. // 加载常量定义文件
  15. require THINK_PATH.'/Common/defines.php';
  16. // 加载路径定义文件
  17. require defined('PATH_DEFINE_FILE')?PATH_DEFINE_FILE:THINK_PATH.'/Common/paths.php';
  18. // 定义核心编译的文件
  19. $runtime[] = THINK_PATH.'/Common/functions.php'; // 系统函数
  20. if(version_compare(PHP_VERSION,'5.2.0','<') )
  21. // 加载兼容函数
  22. $runtime[] = THINK_PATH.'/Common/compat.php';
  23. // 核心基类必须加载
  24. $runtime[] = THINK_PATH.'/Lib/Think/Core/Think.class.php';
  25. // 读取核心编译文件列表
  26. if(is_file(CONFIG_PATH.'core.php')) {
  27. // 加载项目自定义的核心编译文件列表
  28. $list = include CONFIG_PATH.'core.php';
  29. }else{
  30. if(defined('THINK_MODE')) {
  31. // 根据设置的运行模式加载不同的核心编译文件
  32. $list = include THINK_PATH.'/Mode/'.strtolower(THINK_MODE).'.php';
  33. }else{
  34. // 默认核心
  35. $list = include THINK_PATH.'/Common/core.php';
  36. }
  37. }
  38. $runtime = array_merge($runtime,$list);
  39. // 加载核心编译文件列表
  40. foreach ($runtime as $key=>$file){
  41. if(is_file($file)) require $file;
  42. }
  43. // 检查项目目录结构 如果不存在则自动创建
  44. if(!is_dir(RUNTIME_PATH)) {
  45. // 创建项目目录结构
  46. build_app_dir();
  47. }else{
  48. // 检查缓存目录
  49. check_runtime();
  50. }
  51. // 生成核心编译缓存 去掉文件空白以减少大小
  52. if(!defined('NO_CACHE_RUNTIME')) {
  53. $compile = defined('RUNTIME_ALLINONE');
  54. $content = compile(THINK_PATH.'/Common/defines.php',$compile);
  55. $content .= compile(defined('PATH_DEFINE_FILE')? PATH_DEFINE_FILE : THINK_PATH.'/Common/paths.php',$compile);
  56. foreach ($runtime as $file){
  57. $content .= compile($file,$compile);
  58. }
  59. if(defined('STRIP_RUNTIME_SPACE') && STRIP_RUNTIME_SPACE == false ) {
  60. file_put_contents(RUNTIME_PATH.'~runtime.php','<?php'.$content);
  61. }else{
  62. file_put_contents(RUNTIME_PATH.'~runtime.php',strip_whitespace('<?php'.$content));
  63. }
  64. unset($content);
  65. }
  66. }
  67. // 批量创建目录
  68. function mkdirs($dirs,$mode=0777) {
  69. foreach ($dirs as $dir){
  70. if(!is_dir($dir)) mkdir($dir,$mode);
  71. }
  72. }
  73. // 创建项目目录结构
  74. function build_app_dir() {
  75. // 没有创建项目目录的话自动创建
  76. if(!is_dir(APP_PATH)) mk_dir(APP_PATH,0777);
  77. if(is_writeable(APP_PATH)) {
  78. $dirs = array(
  79. LIB_PATH,
  80. RUNTIME_PATH,
  81. CONFIG_PATH,
  82. COMMON_PATH,
  83. LANG_PATH,
  84. CACHE_PATH,
  85. TMPL_PATH,
  86. TMPL_PATH.'default/',
  87. LOG_PATH,
  88. TEMP_PATH,
  89. DATA_PATH,
  90. LIB_PATH.'Model/',
  91. LIB_PATH.'Action/',
  92. );
  93. mkdirs($dirs);
  94. // 目录安全写入
  95. if(!defined('BUILD_DIR_SECURE')) define('BUILD_DIR_SECURE',false);
  96. if(BUILD_DIR_SECURE) {
  97. if(!defined('DIR_SECURE_FILENAME')) define('DIR_SECURE_FILENAME','index.html');
  98. if(!defined('DIR_SECURE_CONTENT')) define('DIR_SECURE_CONTENT',' ');
  99. // 自动写入目录安全文件
  100. $content = DIR_SECURE_CONTENT;
  101. $a = explode(',', DIR_SECURE_FILENAME);
  102. foreach ($a as $filename){
  103. foreach ($dirs as $dir)
  104. file_put_contents($dir.$filename,$content);
  105. }
  106. }
  107. // 写入配置文件
  108. if(!is_file(CONFIG_PATH.'config.php'))
  109. file_put_contents(CONFIG_PATH.'config.php',"<?php\nreturn array(\n\t//'配置项'=>'配置值'\n);\n?>");
  110. // 写入测试Action
  111. if(!is_file(LIB_PATH.'Action/IndexAction.class.php')) {
  112. $content =
  113. '<?php
  114. // 本类由系统自动生成,仅供测试用途
  115. class IndexAction extends Action{
  116. public function index(){
  117. header("Content-Type:text/html; charset=utf-8");
  118. echo "<div style=\'font-weight:normal;color:blue;float:left;width:345px;text-align:center;border:1px solid silver;background:#E8EFFF;padding:8px;font-size:14px;font-family:Tahoma\'>^_^ Hello,欢迎使用<span style=\'font-weight:bold;color:red\'>ThinkPHP</span></div>";
  119. }
  120. }
  121. ?>';
  122. file_put_contents(LIB_PATH.'Action/IndexAction.class.php',$content);
  123. }
  124. }else{
  125. header("Content-Type:text/html; charset=utf-8");
  126. exit('<div style=\'font-weight:bold;float:left;width:345px;text-align:center;border:1px solid silver;background:#E8EFFF;padding:8px;color:red;font-size:14px;font-family:Tahoma\'>项目目录不可写,目录无法自动生成!<BR>请使用项目生成器或者手动生成项目目录~</div>');
  127. }
  128. }
  129. // 检查缓存目录(Runtime) 如果不存在则自动创建
  130. function check_runtime() {
  131. if(!is_writeable(RUNTIME_PATH)) {
  132. header("Content-Type:text/html; charset=utf-8");
  133. exit('<div style=\'font-weight:bold;float:left;width:345px;text-align:center;border:1px solid silver;background:#E8EFFF;padding:8px;color:red;font-size:14px;font-family:Tahoma\'>目录 [ '.RUNTIME_PATH.' ] 不可写!</div>');
  134. }
  135. if(!is_dir(CACHE_PATH)) // 模板缓存目录
  136. mkdir(CACHE_PATH);
  137. if(!is_dir(LOG_PATH)) // 日志目录
  138. mkdir(LOG_PATH);
  139. if(!is_dir(TEMP_PATH)) // 数据缓存目录
  140. mkdir(TEMP_PATH);
  141. if(!is_dir(DATA_PATH)) // 数据文件目录
  142. mkdir(DATA_PATH);
  143. return true;
  144. }
  145. ?>