_cli.php 13 KB

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