You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
7.1 KiB

преди 3 години
  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. static protected $controller = '<?php
  17. namespace [MODULE]\Controller;
  18. use Think\Controller;
  19. class [CONTROLLER]Controller extends Controller {
  20. public function index(){
  21. $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\');
  22. }
  23. }';
  24. static protected $model = '<?php
  25. namespace [MODULE]\Model;
  26. use Think\Model;
  27. class [MODEL]Model extends Model {
  28. }';
  29. // 检测应用目录是否需要自动创建
  30. static public function checkDir($module){
  31. if(!is_dir(APP_PATH.$module)) {
  32. // 创建模块的目录结构
  33. self::buildAppDir($module);
  34. }elseif(!is_dir(LOG_PATH)){
  35. // 检查缓存目录
  36. self::buildRuntime();
  37. }
  38. }
  39. // 创建应用和模块的目录结构
  40. static public function buildAppDir($module) {
  41. // 没有创建的话自动创建
  42. if(!is_dir(APP_PATH)) mkdir(APP_PATH,0755,true);
  43. if(is_writeable(APP_PATH)) {
  44. $dirs = array(
  45. COMMON_PATH,
  46. COMMON_PATH.'Common/',
  47. CONF_PATH,
  48. APP_PATH.$module.'/',
  49. APP_PATH.$module.'/Common/',
  50. APP_PATH.$module.'/Controller/',
  51. APP_PATH.$module.'/Model/',
  52. APP_PATH.$module.'/Conf/',
  53. APP_PATH.$module.'/View/',
  54. RUNTIME_PATH,
  55. CACHE_PATH,
  56. CACHE_PATH.$module.'/',
  57. LOG_PATH,
  58. LOG_PATH.$module.'/',
  59. TEMP_PATH,
  60. DATA_PATH,
  61. );
  62. foreach ($dirs as $dir){
  63. if(!is_dir($dir)) mkdir($dir,0755,true);
  64. }
  65. // 写入目录安全文件
  66. self::buildDirSecure($dirs);
  67. // 写入应用配置文件
  68. if(!is_file(CONF_PATH.'config'.CONF_EXT))
  69. file_put_contents(CONF_PATH.'config'.CONF_EXT,'.php' == CONF_EXT ? "<?php\nreturn array(\n\t//'配置项'=>'配置值'\n);":'');
  70. // 写入模块配置文件
  71. if(!is_file(APP_PATH.$module.'/Conf/config'.CONF_EXT))
  72. file_put_contents(APP_PATH.$module.'/Conf/config'.CONF_EXT,'.php' == CONF_EXT ? "<?php\nreturn array(\n\t//'配置项'=>'配置值'\n);":'');
  73. // 生成模块的测试控制器
  74. if(defined('BUILD_CONTROLLER_LIST')){
  75. // 自动生成的控制器列表(注意大小写)
  76. $list = explode(',',BUILD_CONTROLLER_LIST);
  77. foreach($list as $controller){
  78. self::buildController($module,$controller);
  79. }
  80. }else{
  81. // 生成默认的控制器
  82. self::buildController($module);
  83. }
  84. // 生成模块的模型
  85. if(defined('BUILD_MODEL_LIST')){
  86. // 自动生成的控制器列表(注意大小写)
  87. $list = explode(',',BUILD_MODEL_LIST);
  88. foreach($list as $model){
  89. self::buildModel($module,$model);
  90. }
  91. }
  92. }else{
  93. header('Content-Type:text/html; charset=utf-8');
  94. exit('应用目录['.APP_PATH.']不可写,目录无法自动生成!<BR>请手动生成项目目录~');
  95. }
  96. }
  97. // 检查缓存目录(Runtime) 如果不存在则自动创建
  98. static public function buildRuntime() {
  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)) mkdir(LOG_PATH); // 日志目录
  107. if(!is_dir(TEMP_PATH)) mkdir(TEMP_PATH); // 数据缓存目录
  108. if(!is_dir(DATA_PATH)) mkdir(DATA_PATH); // 数据文件目录
  109. return true;
  110. }
  111. // 创建控制器类
  112. static public function buildController($module,$controller='Index') {
  113. $file = APP_PATH.$module.'/Controller/'.$controller.'Controller'.EXT;
  114. if(!is_file($file)){
  115. $content = str_replace(array('[MODULE]','[CONTROLLER]'),array($module,$controller),self::$controller);
  116. if(!C('APP_USE_NAMESPACE')){
  117. $content = preg_replace('/namespace\s(.*?);/','',$content,1);
  118. }
  119. $dir = dirname($file);
  120. if(!is_dir($dir)){
  121. mkdir($dir, 0755, true);
  122. }
  123. file_put_contents($file,$content);
  124. }
  125. }
  126. // 创建模型类
  127. static public function buildModel($module,$model) {
  128. $file = APP_PATH.$module.'/Model/'.$model.'Model'.EXT;
  129. if(!is_file($file)){
  130. $content = str_replace(array('[MODULE]','[MODEL]'),array($module,$model),self::$model);
  131. if(!C('APP_USE_NAMESPACE')){
  132. $content = preg_replace('/namespace\s(.*?);/','',$content,1);
  133. }
  134. $dir = dirname($file);
  135. if(!is_dir($dir)){
  136. mkdir($dir, 0755, true);
  137. }
  138. file_put_contents($file,$content);
  139. }
  140. }
  141. // 生成目录安全文件
  142. static public function buildDirSecure($dirs=array()) {
  143. // 目录安全写入(默认开启)
  144. defined('BUILD_DIR_SECURE') or define('BUILD_DIR_SECURE', true);
  145. if(BUILD_DIR_SECURE) {
  146. defined('DIR_SECURE_FILENAME') or define('DIR_SECURE_FILENAME', 'index.html');
  147. defined('DIR_SECURE_CONTENT') or define('DIR_SECURE_CONTENT', ' ');
  148. // 自动写入目录安全文件
  149. $content = DIR_SECURE_CONTENT;
  150. $files = explode(',', DIR_SECURE_FILENAME);
  151. foreach ($files as $filename){
  152. foreach ($dirs as $dir)
  153. file_put_contents($dir.$filename,$content);
  154. }
  155. }
  156. }
  157. }