You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

226 lines
8.2 KiB

  1. <?php
  2. define('PHALAPI_INSTALL', TRUE);
  3. define('D_S', DIRECTORY_SEPARATOR);
  4. $step = isset($_GET['step']) ? intval($_GET['step']) : 0;
  5. switch ($step) {
  6. //第一步:环境检测
  7. case 1:
  8. if (file_exists('_install.lock')) {
  9. $error = '项目已安装,请不要重复安装,并建议手动删除 ./install 此目录以及目录下的全部文件';
  10. include dirname(__FILE__) . D_S . '_error.php';
  11. exit(0);
  12. }
  13. //-1:必须但不支持 0:可选但不支持 1:完美支持
  14. $checkList = array(
  15. 'php' => array('name' => 'PHP 版本', 'status' => -1, 'tip' => '建议使用PHP 5.3.3及以上版本,否则DI无法支持匿名函数'),
  16. 'pdo' => array('name' => '数据库模块', 'status' => -1, 'tip' => '建议使用PDO扩展,否则NotORM无法使用PDO进行数据库操作'),
  17. 'memcache' => array('name' => 'Memcache扩展', 'status' => 0, 'tip' => '无此扩展时,不能使用Memcache缓存'),
  18. 'mcrypt' => array('name' => 'Mcrypt扩展', 'status' => 0, 'tip' => '无此扩展时,不能使用mcrypt进行加密处理'),
  19. 'runtime' => array('name' => '目录权限', 'status' => -1, 'tip' => '日志或配置文件目录若缺少写入权限,则不能写入日记和进行文件缓存以及接下配置无法生效'),
  20. );
  21. if (version_compare(PHP_VERSION, '5.3.3', '>=')) {
  22. $checkList['php']['status'] = 1;
  23. }
  24. if (class_exists('PDO', false) && extension_loaded('PDO')) {
  25. $checkList['pdo']['status'] = 1;
  26. }
  27. if (class_exists('Memcache', false) && extension_loaded('memcache')) {
  28. $checkList['memcache']['status'] = 1;
  29. }
  30. if (extension_loaded('mcrypt')) {
  31. $checkList['mcrypt']['status'] = 1;
  32. }
  33. $runtimePath = dirname(__FILE__) . implode(D_S, array('', '..', '..', 'Runtime'));
  34. $runtimePath = file_exists($runtimePath) ? realpath($runtimePath) : $runtimePath;
  35. $checkList['runtime']['tip'] = $runtimePath . '<br>' . $checkList['runtime']['tip'];
  36. $configPath = dirname(__FILE__) . implode(D_S, array('', '..', '..', 'Config'));
  37. $configPath = file_exists($configPath) ? realpath($configPath) : $configPath;
  38. $checkList['runtime']['tip'] = $configPath . '<br>' . $checkList['runtime']['tip'];
  39. $publicPath = dirname(__FILE__) . implode(D_S, array('', '..', '..', 'Public'));
  40. $publicPath = file_exists($publicPath) ? realpath($publicPath) : $publicPath;
  41. $checkList['runtime']['tip'] = $publicPath . '<br>' . $checkList['runtime']['tip'];
  42. if (is_writeable($runtimePath) && is_writeable($configPath)) {
  43. $checkList['runtime']['status'] = 1;
  44. }
  45. include dirname(__FILE__) . D_S . '_step1.php';
  46. break;
  47. //第2步:系统配置
  48. case 2:
  49. include dirname(__FILE__) . D_S . '_step2.php';
  50. break;
  51. //第3步:接口请求
  52. case 3:
  53. if (empty($_POST['doSubmit']) || empty($_POST)) {
  54. header('Location: ./?step=1');
  55. exit(0);
  56. }
  57. //数据库配置文件
  58. $search = array(
  59. '{project}',
  60. '{host}',
  61. '{name}',
  62. '{user}',
  63. '{password}',
  64. '{port}',
  65. '{charset}',
  66. '{prefix}',
  67. );
  68. $replace = array(
  69. strtolower($_POST['project']),
  70. $_POST['host'],
  71. $_POST['name'],
  72. $_POST['user'],
  73. $_POST['password'],
  74. $_POST['port'],
  75. $_POST['charset'],
  76. $_POST['prefix'],
  77. );
  78. $configDbsContent = str_replace($search, $replace, getConfigDbsTpl());
  79. file_put_contents(
  80. dirname(__FILE__) . implode(D_S, array('', '..', '..', 'Config', 'dbs.php')),
  81. $configDbsContent
  82. );
  83. //Project
  84. $project = ucwords($_POST['project']);
  85. $appPath = dirname(__FILE__) . implode(D_S, array('', '..', '..', $project,));
  86. $demoPath = dirname(__FILE__) . implode(D_S, array('', '..', '..', 'Demo',));
  87. if (!file_exists($appPath)) {
  88. //项目目录
  89. mkdir($appPath . D_S);
  90. mkdir($appPath . D_S . 'Api');
  91. mkdir($appPath . D_S . 'Domain');
  92. mkdir($appPath . D_S . 'Model');
  93. mkdir($appPath . D_S . 'Common');
  94. copy(
  95. $demoPath . D_S . 'Api' . D_S . 'Default.php',
  96. $appPath . D_S . 'Api' . D_S . 'Default.php'
  97. );
  98. mkdir($appPath . D_S . 'Tests');
  99. mkdir($appPath . D_S . 'Tests' . D_S . 'Api');
  100. mkdir($appPath . D_S . 'Tests' . D_S . 'Domain');
  101. mkdir($appPath . D_S . 'Tests' . D_S . 'Model');
  102. mkdir($appPath . D_S . 'Tests' . D_S . 'Common');
  103. //单元测试
  104. copy(
  105. $demoPath . D_S . 'Tests' . D_S . 'test_env.php',
  106. $appPath . D_S . 'Tests' . D_S . 'test_env.php'
  107. );
  108. file_put_contents(
  109. $appPath . D_S . 'Tests' . D_S . 'test_env.php',
  110. str_replace('Demo', $project, file_get_contents($appPath . D_S . 'Tests' . D_S . 'test_env.php'))
  111. );
  112. copy(
  113. $demoPath . D_S . 'Tests' . D_S . 'Api' . D_S . 'Api_Default_Test.php',
  114. $appPath . D_S . 'Tests' . D_S . 'Api' . D_S . 'Api_Default_Test.php'
  115. );
  116. copy(
  117. $demoPath . D_S . 'Tests' . D_S . 'phpunit.xml',
  118. $appPath . D_S . 'Tests' . D_S . 'phpunit.xml'
  119. );
  120. //访问入口
  121. $appPublicPath = dirname(__FILE__) . implode(D_S, array('', '..', strtolower($project), ));
  122. $demoPublicPath = dirname(__FILE__) . implode(D_S, array('', '..', 'demo',));
  123. mkdir($appPublicPath);
  124. copy(
  125. $demoPublicPath . D_S . 'checkApiParams.php',
  126. $appPublicPath . D_S . 'checkApiParams.php'
  127. );
  128. copy(
  129. $demoPublicPath . D_S . 'listAllApis.php',
  130. $appPublicPath . D_S . 'listAllApis.php'
  131. );
  132. copy(
  133. $demoPublicPath . D_S . 'index.php',
  134. $appPublicPath . D_S . 'index.php'
  135. );
  136. // 挂载项目
  137. foreach (array('checkApiParams.php', 'listAllApis.php', 'index.php') as $publicFile) {
  138. file_put_contents(
  139. $appPublicPath . D_S . $publicFile,
  140. str_replace('Demo', $project, file_get_contents($demoPublicPath . D_S . $publicFile))
  141. );
  142. }
  143. }
  144. @touch('_install.lock');
  145. //请求链接
  146. $relatePath = substr($_SERVER['REQUEST_URI'], 0, stripos($_SERVER['REQUEST_URI'], '/install/'));
  147. $apiUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $relatePath . '/' . strtolower($project);
  148. include dirname(__FILE__) . D_S . '_step3.php';
  149. break;
  150. default:
  151. include dirname(__FILE__) . D_S . '_start.php';
  152. }
  153. function getConfigDbsTpl() {
  154. $configDbs = <<<EOT
  155. <?php
  156. /**
  157. * 分库分表的自定义数据库路由配置
  158. */
  159. return array(
  160. /**
  161. * DB数据库服务器集群
  162. */
  163. 'servers' => array(
  164. 'db_{project}' => array( //服务器标记
  165. 'host' => '{host}', //数据库域名
  166. 'name' => '{name}', //数据库名字
  167. 'user' => '{user}', //数据库用户名
  168. 'password' => '{password}', //数据库密码
  169. 'port' => '{port}', //数据库端口
  170. 'charset' => '{charset}', //数据库字符集
  171. ),
  172. ),
  173. /**
  174. * 自定义路由表
  175. */
  176. 'tables' => array(
  177. //通用路由
  178. '__default__' => array(
  179. 'prefix' => '{prefix}',
  180. 'key' => 'id',
  181. 'map' => array(
  182. array('db' => 'db_{project}'),
  183. ),
  184. ),
  185. /**
  186. 'demo' => array( //表名
  187. 'prefix' => '{prefix}', //表名前缀
  188. 'key' => 'id', //表主键名
  189. 'map' => array( //表路由配置
  190. array('db' => 'db_{project}'), //单表配置:array('db' => 服务器标记)
  191. array('start' => 0, 'end' => 2, 'db' => 'db_{project}'), //分表配置:array('start' => 开始下标, 'end' => 结束下标, 'db' => 服务器标记)
  192. ),
  193. ),
  194. */
  195. ),
  196. );
  197. EOT;
  198. return $configDbs;
  199. }