_cli.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * @author Jinhui Zhu <jinhui.zhu@live.cn>
  4. * 通过命令行直接生成项目目录
  5. */
  6. ini_set("display_errors", "On");
  7. /**
  8. * 错误模式
  9. */
  10. error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
  11. /**
  12. * 自动生成目标目录
  13. *
  14. * php -q _cli.php create=yes workspace=../project cache=tmp useDB=1
  15. * create: 是否自动创建
  16. * workspace: 程序存放的目录
  17. * cache: 缓存目录
  18. * useDB: 是否使用数据库,值 0/1
  19. * 目前已经将配置文件写在db.ini中,配置内容将不在创建项目的时候提供,可以自行在目录中修改
  20. */
  21. class cmd
  22. {
  23. const VERSION = '1.2';
  24. public $dir = array('Configure', 'Controller', 'Model', 'View', 'Plugins', 'tmp');
  25. public function __construct($args)
  26. {
  27. $param = $this->parseArgvs($args);
  28. if (sizeof($param) < 1) {
  29. $this->stdout("命令行使用如下:\n
  30. >php -q _cli.php create=yes workspace=../project cache=tmp useDB=1\n
  31. * create: 是否自动创建:yes; \n
  32. * workspace: 工作目录\n
  33. * cache : 缓存目录\n
  34. * useDB : 是否使用数据库 : 使用:1 不使用: 0\n
  35. ");
  36. $this->stdout("创建 yes/no:");
  37. $param['create'] = trim(fgets(\STDIN));
  38. $this->stdout("工作目录:");
  39. $param['workspace'] = trim(fgets(\STDIN));
  40. $this->stdout("缓存目录:");
  41. $param['cache'] = trim(fgets(\STDIN));
  42. $this->stdout("是否使用数据库 使用:1 不使用 0:");
  43. $param['useDB'] = trim(fgets(\STDIN));
  44. $this->stdout('将要在'. $param['workspace'] .'创建项目,确认请输入yes,取消请输入no:');
  45. $param['create'] = trim(fgets(\STDIN));
  46. }
  47. if ($param['create'] == 'yes') {
  48. if ($this->workspace($param['workspace'])) {
  49. $cache = $param['cache'];
  50. if (empty($param['cache'])) $cache = 'tmp';
  51. $this->dir[5] = $cache;
  52. //创建目录工作区目录
  53. foreach ($this->dir AS $d) {
  54. $path = $param['workspace'] . '/' . $d;
  55. if (!is_dir($path)) {
  56. $date = date('Y-m-d H:i:s');
  57. echo "create path {$path} success.\n";
  58. mkdir($path, 0777, true);
  59. //写入.htaccess文件到包含的目录,不允许通过Apache浏览
  60. $htaccess = array();
  61. $htaccess[] = "##";
  62. $htaccess[] = "#";
  63. $htaccess[] = "# \$Id: .htaccess 268 {$date}Z Jinhui.Zhu $";
  64. $htaccess[] = "#";
  65. $htaccess[] = "# Copyright (C) 2010-2012 All Rights Reserved.";
  66. $htaccess[] = "#";
  67. $htaccess[] = "##";
  68. $htaccess[] = "";
  69. $htaccess[] = "Options Includes";
  70. file_put_contents($path . '/.htaccess', join("\n", $htaccess));
  71. } else {
  72. $this->stdout("{$path} 已经存在.". PHP_EOL);
  73. }
  74. }
  75. //拷贝网站配置文件site.xml到项目目录
  76. if($cache != 'tmp'){
  77. $appIni = file_get_contents('_cli/app.ini');
  78. $appIni = str_replace('tmp/compile', $cache . '/compile', $appIni);
  79. $appIni = str_replace('tmp/cache', $cache . '/cache', $appIni);
  80. file_put_contents($param['workspace'] . '/Configure/app.ini', $appIni);
  81. }else if (!copy("_cli/app.ini", $param['workspace'] . '/Configure/app.ini')) {
  82. $this->stdout('拷贝 app.ini 到 ' . $param['workspace'] . '/Configure/app.ini失败, 拒绝访问.');
  83. }
  84. if (!copy("_cli/router.config.php", $param['workspace'] . '/Configure/router.config.php')) {
  85. $this->stdout('拷贝 router.config.php 到' . $param['workspace'] . '/Configure/router.config.php 失败, 拒绝访问.');
  86. }
  87. if ($param['useDB'] != 'no') {
  88. if (!copy("_cli/db.ini", $param['workspace'] . '/Configure/db.ini')) {
  89. $this->stdout('拷贝 db.ini 到 ' . $param['workspace'] . '/Configure/db.ini false, 拒绝访问.');
  90. }
  91. }
  92. //生成数据库文件
  93. //--生成首页文件
  94. //--获取文件的相对路径
  95. $realPath = $this->getRealPath($param['workspace']);
  96. $this->stdout("真实路径 " . $realPath . "\n");
  97. $QiiPath = $this->getRelatePath($realPath . "/index.php", dirname(__FILE__) . "/Qii/Qii.php");
  98. $this->stdout("Qii 路径 " . $QiiPath . "\n");
  99. $date = date("Y/m/d H:i:s");
  100. $indexPage = array();
  101. $indexPage[] = "<?php";
  102. $indexPage[] = "/**";
  103. $indexPage[] = " * This is index page auto create by Qii, don't delete";
  104. $indexPage[] = " * ";
  105. $indexPage[] = " * @author Jinhui.zhu <jinhui.zhu@live.cn>";
  106. $indexPage[] = " * @version \$Id: index.php,v 1.1 {$date} Jinhui.Zhu Exp $";
  107. $indexPage[] = " */";
  108. $indexPage[] = 'require("'.$QiiPath.'");';
  109. $indexPage[] = '$app = \\Qii::getInstance();';
  110. $indexPage[] = '//如需更改网站源代码存储路径,请修改此路径';
  111. $indexPage[] = '$app->setWorkspace(\''.$param['workspace'].'\');';
  112. $indexPage[] = '$app->setCachePath(\''.$cache.'\');';
  113. $indexPage[] = '$app->setAppConfigure(\'Configure/app.ini\');';
  114. $indexPage[] = '$env = getenv(\'WEB_ENVIRONMENT\') ? getenv(\'WEB_ENVIRONMENT\') : \'product\';';
  115. $indexPage[] = '$app->setEnv($env);';
  116. if ($param['useDB']) $indexPage[] = '$app->setDB(\'Configure/db.ini\');';
  117. $indexPage[] = '$app->setRouter(\'Configure/router.config.php\')';
  118. $indexPage[] = '->run();';
  119. if (!file_exists($realPath . "/index.php")) {
  120. //如果文件不存在就写入
  121. file_put_contents($realPath . "/index.php", join("\n", $indexPage));
  122. }
  123. //写入首页controller
  124. if (!file_exists($realPath . "/Controller/index.php")) {
  125. $indexContents = array();
  126. $indexContents[] = "<?php";
  127. $indexContents[] = 'namespace Controller;' . PHP_EOL;
  128. $indexContents[] = 'use \Qii\Base\Controller;' . PHP_EOL;
  129. $indexContents[] = "class index extends Controller";
  130. $indexContents[] = "{";
  131. $indexContents[] = "\tpublic \$enableView = true;";
  132. $indexContents[] = "\tpublic function __construct()\n\t{";
  133. $indexContents[] = "\t\tparent::__construct();";
  134. $indexContents[] = "\t}";
  135. $indexContents[] = "\tpublic function indexAction()\n\t{";
  136. $indexContents[] = "\t\t return new \Qii\Base\Response(array('format' => 'html', 'body' => '请重写 '. __FILE__ . ' 中的 indexAction 方法, 第 ' . __LINE__ . ' 行'));";
  137. $indexContents[] = "\t}";
  138. $indexContents[] = "}";
  139. $indexContents[] = "?>";
  140. file_put_contents($realPath . "/Controller/index.php", join("\n", $indexContents));
  141. }
  142. //apache rewrite file
  143. $htaccessFile = $param['workspace'] . "/.htaccess";
  144. if(!file_exists($htaccessFile)){
  145. if(!copy('_cli/.htaccess', $htaccessFile)){
  146. $this->stdout($this->stdout("拷贝 .htaccess 到 ". $htaccessFile . ' 失败, 拒绝访问') . PHP_EOL);
  147. }
  148. }
  149. }
  150. } else if($param['create'] == 'no'){
  151. $this->stdout('您已经取消');
  152. }
  153. }
  154. /**
  155. * 获取文件相对路径
  156. *
  157. * @param String $cur 路径1
  158. * @param String $absp 路径2
  159. * @return String 路径2相对于路径1的路径
  160. */
  161. public function getRelatePath($cur, $absp)
  162. {
  163. $cur = str_replace('\\', '/', $cur);
  164. $absp = str_replace('\\', '/', $absp);
  165. $sabsp = explode('/', $absp);
  166. $scur = explode('/', $cur);
  167. $la = count($sabsp) - 1;
  168. $lb = count($scur) - 1;
  169. $l = max($la, $lb);
  170. for ($i = 0; $i <= $l; $i++) {
  171. if ($sabsp[$i] != $scur[$i])
  172. break;
  173. }
  174. $k = $i - 1;
  175. $path = "";
  176. for ($i = 1; $i <= ($lb - $k - 1); $i++)
  177. $path .= "../";
  178. for ($i = $k + 1; $i <= ($la - 1); $i++)
  179. $path .= $sabsp[$i] . "/";
  180. $path .= $sabsp[$la];
  181. return $path;
  182. }
  183. /**
  184. * 获取相对目录
  185. *
  186. * @param String $workspace 目标路径
  187. * @return String 目标路径相对于当期路径的相对路径
  188. */
  189. public function getRealPath($workspace)
  190. {
  191. $currentDir = str_replace("\\", "/", dirname(__FILE__));
  192. $workspace = str_replace("\\", "/", $workspace);
  193. if ($workspace[0] == '/') {
  194. return $workspace;
  195. } else {
  196. $workspaceArray = explode("/", $workspace);
  197. $currentDirArray = explode("/", $currentDir);
  198. $work = array();
  199. foreach ($workspaceArray AS $k) {
  200. if ($k == '..') {
  201. array_pop($currentDirArray);
  202. } elseif ($k == '.') {
  203. } else {
  204. $work[] = $k;
  205. }
  206. }
  207. if (!empty($currentDirArray)) {
  208. return join("/", $currentDirArray) . "/" . join("/", $work);
  209. } else {
  210. return false;
  211. }
  212. }
  213. }
  214. /**
  215. * 创建工作区目录
  216. *
  217. * @param String $dir
  218. * @return Bool
  219. */
  220. public function workspace($dir)
  221. {
  222. return true;
  223. if (!empty($dir)) {
  224. if (!is_dir($dir)) {
  225. return mkdir($dir, 0777, true);
  226. } else {
  227. return true;
  228. }
  229. }
  230. return false;
  231. }
  232. /**
  233. * 匹配命令行参数
  234. *
  235. * @param String $argvs
  236. * @return Array 返回参数对应的值
  237. */
  238. public function parseArgvs($argvs)
  239. {
  240. $keyValue = array();
  241. foreach ($argvs AS $value) {
  242. $valueArray = explode("=", $value);
  243. $k = $valueArray[0];
  244. $v = stripslashes($valueArray[1]);
  245. $keyValue[$k] = $v;
  246. }
  247. return $keyValue;
  248. }
  249. /**
  250. * 在windows cmd 情况下的中文输出乱码问题
  251. * @param string $string
  252. * @return bool|int
  253. */
  254. public function stdout($string)
  255. {
  256. $string = iconv('utf-8', 'gbk', $string);
  257. fwrite(\STDOUT, $string);
  258. }
  259. }
  260. array_shift($argv);
  261. new cmd($argv);
  262. ?>