Build.class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * 洛阳赤炎鹰网络科技有限公司
  4. * https://www.cyyvip.com
  5. * Copyright (c) 2022 赤店商城 All rights reserved.
  6. */
  7. // +----------------------------------------------------------------------
  8. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  9. // +----------------------------------------------------------------------
  10. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  11. // +----------------------------------------------------------------------
  12. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  13. // +----------------------------------------------------------------------
  14. // | Author: liu21st <liu21st@gmail.com>
  15. // +----------------------------------------------------------------------
  16. namespace Think;
  17. /**
  18. * 用于ThinkPHP的自动生成
  19. */
  20. class Build {
  21. static protected $controller = '<?php
  22. /**
  23. * 洛阳赤炎鹰网络科技有限公司
  24. * https://www.cyyvip.com
  25. * Copyright (c) 2022 赤店商城 All rights reserved.
  26. */
  27. namespace [MODULE]\Controller;
  28. use Think\Controller;
  29. class [CONTROLLER]Controller extends Controller {
  30. public function index(){
  31. $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\');
  32. }
  33. }';
  34. static protected $model = '<?php
  35. /**
  36. * 洛阳赤炎鹰网络科技有限公司
  37. * https://www.cyyvip.com
  38. * Copyright (c) 2022 赤店商城 All rights reserved.
  39. */
  40. namespace [MODULE]\Model;
  41. use Think\Model;
  42. class [MODEL]Model extends Model {
  43. }';
  44. // 检测应用目录是否需要自动创建
  45. static public function checkDir($module){
  46. if(!is_dir(APP_PATH.$module)) {
  47. // 创建模块的目录结构
  48. self::buildAppDir($module);
  49. }elseif(!is_dir(LOG_PATH)){
  50. // 检查缓存目录
  51. self::buildRuntime();
  52. }
  53. }
  54. // 创建应用和模块的目录结构
  55. static public function buildAppDir($module) {
  56. // 没有创建的话自动创建
  57. if(!is_dir(APP_PATH)) mkdir(APP_PATH,0755,true);
  58. if(is_writeable(APP_PATH)) {
  59. $dirs = array(
  60. COMMON_PATH,
  61. COMMON_PATH.'Common/',
  62. CONF_PATH,
  63. APP_PATH.$module.'/',
  64. APP_PATH.$module.'/Common/',
  65. APP_PATH.$module.'/Controller/',
  66. APP_PATH.$module.'/Model/',
  67. APP_PATH.$module.'/Conf/',
  68. APP_PATH.$module.'/View/',
  69. RUNTIME_PATH,
  70. CACHE_PATH,
  71. CACHE_PATH.$module.'/',
  72. LOG_PATH,
  73. LOG_PATH.$module.'/',
  74. TEMP_PATH,
  75. DATA_PATH,
  76. );
  77. foreach ($dirs as $dir){
  78. if(!is_dir($dir)) mkdir($dir,0755,true);
  79. }
  80. // 写入目录安全文件
  81. self::buildDirSecure($dirs);
  82. // 写入应用配置文件
  83. if(!is_file(CONF_PATH.'config'.CONF_EXT))
  84. file_put_contents(CONF_PATH.'config'.CONF_EXT,'.php' == CONF_EXT ? "<?php
  85. /**
  86. * 洛阳赤炎鹰网络科技有限公司
  87. * https://www.cyyvip.com
  88. * Copyright (c) 2022 赤店商城 All rights reserved.
  89. */
  90. \nreturn array(\n\t//'配置项'=>'配置值'\n);":'');
  91. // 写入模块配置文件
  92. if(!is_file(APP_PATH.$module.'/Conf/config'.CONF_EXT))
  93. file_put_contents(APP_PATH.$module.'/Conf/config'.CONF_EXT,'.php' == CONF_EXT ? "<?php
  94. /**
  95. * 洛阳赤炎鹰网络科技有限公司
  96. * https://www.cyyvip.com
  97. * Copyright (c) 2022 赤店商城 All rights reserved.
  98. */
  99. \nreturn array(\n\t//'配置项'=>'配置值'\n);":'');
  100. // 生成模块的测试控制器
  101. if(defined('BUILD_CONTROLLER_LIST')){
  102. // 自动生成的控制器列表(注意大小写)
  103. $list = explode(',',BUILD_CONTROLLER_LIST);
  104. foreach($list as $controller){
  105. self::buildController($module,$controller);
  106. }
  107. }else{
  108. // 生成默认的控制器
  109. self::buildController($module);
  110. }
  111. // 生成模块的模型
  112. if(defined('BUILD_MODEL_LIST')){
  113. // 自动生成的控制器列表(注意大小写)
  114. $list = explode(',',BUILD_MODEL_LIST);
  115. foreach($list as $model){
  116. self::buildModel($module,$model);
  117. }
  118. }
  119. }else{
  120. header('Content-Type:text/html; charset=utf-8');
  121. exit('应用目录['.APP_PATH.']不可写,目录无法自动生成!<BR>请手动生成项目目录~');
  122. }
  123. }
  124. // 检查缓存目录(Runtime) 如果不存在则自动创建
  125. static public function buildRuntime() {
  126. if(!is_dir(RUNTIME_PATH)) {
  127. mkdir(RUNTIME_PATH);
  128. }elseif(!is_writeable(RUNTIME_PATH)) {
  129. header('Content-Type:text/html; charset=utf-8');
  130. exit('目录 [ '.RUNTIME_PATH.' ] 不可写!');
  131. }
  132. mkdir(CACHE_PATH); // 模板缓存目录
  133. if(!is_dir(LOG_PATH)) mkdir(LOG_PATH); // 日志目录
  134. if(!is_dir(TEMP_PATH)) mkdir(TEMP_PATH); // 数据缓存目录
  135. if(!is_dir(DATA_PATH)) mkdir(DATA_PATH); // 数据文件目录
  136. return true;
  137. }
  138. // 创建控制器类
  139. static public function buildController($module,$controller='Index') {
  140. $file = APP_PATH.$module.'/Controller/'.$controller.'Controller'.EXT;
  141. if(!is_file($file)){
  142. $content = str_replace(array('[MODULE]','[CONTROLLER]'),array($module,$controller),self::$controller);
  143. if(!C('APP_USE_NAMESPACE')){
  144. $content = preg_replace('/namespace\s(.*?);/','',$content,1);
  145. }
  146. $dir = dirname($file);
  147. if(!is_dir($dir)){
  148. mkdir($dir, 0755, true);
  149. }
  150. file_put_contents($file,$content);
  151. }
  152. }
  153. // 创建模型类
  154. static public function buildModel($module,$model) {
  155. $file = APP_PATH.$module.'/Model/'.$model.'Model'.EXT;
  156. if(!is_file($file)){
  157. $content = str_replace(array('[MODULE]','[MODEL]'),array($module,$model),self::$model);
  158. if(!C('APP_USE_NAMESPACE')){
  159. $content = preg_replace('/namespace\s(.*?);/','',$content,1);
  160. }
  161. $dir = dirname($file);
  162. if(!is_dir($dir)){
  163. mkdir($dir, 0755, true);
  164. }
  165. file_put_contents($file,$content);
  166. }
  167. }
  168. // 生成目录安全文件
  169. static public function buildDirSecure($dirs=array()) {
  170. // 目录安全写入(默认开启)
  171. defined('BUILD_DIR_SECURE') or define('BUILD_DIR_SECURE', true);
  172. if(BUILD_DIR_SECURE) {
  173. defined('DIR_SECURE_FILENAME') or define('DIR_SECURE_FILENAME', 'index.html');
  174. defined('DIR_SECURE_CONTENT') or define('DIR_SECURE_CONTENT', ' ');
  175. // 自动写入目录安全文件
  176. $content = DIR_SECURE_CONTENT;
  177. $files = explode(',', DIR_SECURE_FILENAME);
  178. foreach ($files as $filename){
  179. foreach ($dirs as $dir)
  180. file_put_contents($dir.$filename,$content);
  181. }
  182. }
  183. }
  184. }