_cli.php 11 KB

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