_cli.php 12 KB

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