Build.class.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 Think;
  12. /**
  13. * 用于ThinkPHP的自动生成
  14. */
  15. class Build
  16. {
  17. protected static $controller = '<?php
  18. namespace [MODULE]\Controller;
  19. use Think\Controller;
  20. class [CONTROLLER]Controller extends Controller
  21. {
  22. public function index()
  23. {
  24. [CONTENT]
  25. }
  26. }';
  27. protected static $model = '<?php
  28. namespace [MODULE]\Model;
  29. use Think\Model;
  30. class [MODEL]Model extends Model
  31. {
  32. }';
  33. // 检测应用目录是否需要自动创建
  34. public static function checkDir($module)
  35. {
  36. if (!is_dir(APP_PATH . $module)) {
  37. // 创建模块的目录结构
  38. self::buildAppDir($module);
  39. } elseif (!is_dir(LOG_PATH)) {
  40. // 检查缓存目录
  41. self::buildRuntime();
  42. }
  43. }
  44. // 创建应用和模块的目录结构
  45. public static function buildAppDir($module)
  46. {
  47. // 没有创建的话自动创建
  48. if (!is_dir(APP_PATH)) {
  49. mkdir(APP_PATH, 0755, true);
  50. }
  51. if (is_writeable(APP_PATH)) {
  52. $dirs = array(
  53. COMMON_PATH,
  54. COMMON_PATH . 'Common/',
  55. CONF_PATH,
  56. APP_PATH . $module . '/',
  57. APP_PATH . $module . '/Common/',
  58. APP_PATH . $module . '/Controller/',
  59. APP_PATH . $module . '/Model/',
  60. APP_PATH . $module . '/Conf/',
  61. APP_PATH . $module . '/View/',
  62. RUNTIME_PATH,
  63. CACHE_PATH,
  64. CACHE_PATH . $module . '/',
  65. LOG_PATH,
  66. LOG_PATH . $module . '/',
  67. TEMP_PATH,
  68. DATA_PATH,
  69. );
  70. foreach ($dirs as $dir) {
  71. if (!is_dir($dir)) {
  72. mkdir($dir, 0755, true);
  73. }
  74. }
  75. // 写入目录安全文件
  76. self::buildDirSecure($dirs);
  77. // 写入应用配置文件
  78. if (!is_file(CONF_PATH . 'config' . CONF_EXT)) {
  79. file_put_contents(CONF_PATH . 'config' . CONF_EXT, '.php' == CONF_EXT ? "<?php\nreturn array(\n\t//'配置项'=>'配置值'\n);" : '');
  80. }
  81. // 写入模块配置文件
  82. if (!is_file(APP_PATH . $module . '/Conf/config' . CONF_EXT)) {
  83. file_put_contents(APP_PATH . $module . '/Conf/config' . CONF_EXT, '.php' == CONF_EXT ? "<?php\nreturn array(\n\t//'配置项'=>'配置值'\n);" : '');
  84. }
  85. // 自动生成控制器类
  86. self::buildController($module, defined('BUILD_CONTROLLER_LIST') ? BUILD_CONTROLLER_LIST : C('DEFAULT_CONTROLLER'));
  87. // 自动生成模型类
  88. if (defined('BUILD_MODEL_LIST')) {
  89. self::buildModel($module, BUILD_MODEL_LIST);
  90. }
  91. } else {
  92. header('Content-Type:text/html; charset=utf-8');
  93. exit('应用目录[' . APP_PATH . ']不可写,目录无法自动生成!<BR>请手动生成项目目录~');
  94. }
  95. }
  96. // 检查缓存目录(Runtime) 如果不存在则自动创建
  97. public static function buildRuntime()
  98. {
  99. if (!is_dir(RUNTIME_PATH)) {
  100. mkdir(RUNTIME_PATH);
  101. } elseif (!is_writeable(RUNTIME_PATH)) {
  102. header('Content-Type:text/html; charset=utf-8');
  103. exit('目录 [ ' . RUNTIME_PATH . ' ] 不可写!');
  104. }
  105. mkdir(CACHE_PATH); // 模板缓存目录
  106. if (!is_dir(LOG_PATH)) {
  107. mkdir(LOG_PATH);
  108. }
  109. // 日志目录
  110. if (!is_dir(TEMP_PATH)) {
  111. mkdir(TEMP_PATH);
  112. }
  113. // 数据缓存目录
  114. if (!is_dir(DATA_PATH)) {
  115. mkdir(DATA_PATH);
  116. }
  117. // 数据文件目录
  118. return true;
  119. }
  120. // 创建控制器类
  121. public static function buildController($module, $controllers)
  122. {
  123. $list = is_array($controllers) ? $controllers : explode(',', $controllers);
  124. $hello = '$this->show(\'<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>!</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>\',\'utf-8\');';
  125. foreach ($list as $controller) {
  126. $hello = C('DEFAULT_CONTROLLER') == $controller ? $hello : '';
  127. $file = APP_PATH . $module . '/Controller/' . $controller . 'Controller' . EXT;
  128. if (!is_file($file)) {
  129. $content = str_replace(array('[MODULE]', '[CONTROLLER]', '[CONTENT]'), array($module, $controller, $hello), self::$controller);
  130. if (!C('APP_USE_NAMESPACE')) {
  131. $content = preg_replace('/namespace\s(.*?);/', '', $content, 1);
  132. }
  133. $dir = dirname($file);
  134. if (!is_dir($dir)) {
  135. mkdir($dir, 0755, true);
  136. }
  137. file_put_contents($file, $content);
  138. }
  139. }
  140. }
  141. // 创建模型类
  142. public static function buildModel($module, $models)
  143. {
  144. $list = is_array($models) ? $models : explode(',', $models);
  145. foreach ($list as $model) {
  146. $file = APP_PATH . $module . '/Model/' . $model . 'Model' . EXT;
  147. if (!is_file($file)) {
  148. $content = str_replace(array('[MODULE]', '[MODEL]'), array($module, $model), self::$model);
  149. if (!C('APP_USE_NAMESPACE')) {
  150. $content = preg_replace('/namespace\s(.*?);/', '', $content, 1);
  151. }
  152. $dir = dirname($file);
  153. if (!is_dir($dir)) {
  154. mkdir($dir, 0755, true);
  155. }
  156. file_put_contents($file, $content);
  157. }
  158. }
  159. }
  160. // 生成目录安全文件
  161. public static function buildDirSecure($dirs = array())
  162. {
  163. // 目录安全写入(默认开启)
  164. defined('BUILD_DIR_SECURE') or define('BUILD_DIR_SECURE', true);
  165. if (BUILD_DIR_SECURE) {
  166. defined('DIR_SECURE_FILENAME') or define('DIR_SECURE_FILENAME', 'index.html');
  167. defined('DIR_SECURE_CONTENT') or define('DIR_SECURE_CONTENT', ' ');
  168. // 自动写入目录安全文件
  169. $content = DIR_SECURE_CONTENT;
  170. $files = explode(',', DIR_SECURE_FILENAME);
  171. foreach ($files as $filename) {
  172. foreach ($dirs as $dir) {
  173. file_put_contents($dir . $filename, $content);
  174. }
  175. }
  176. }
  177. }
  178. }