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.
 
 
 
 
 
 

126 lines
4.2 KiB

  1. <?php
  2. /**
  3. * 生成接口代码
  4. *
  5. * - 此脚本可用于生成基本的接口代码,包括Api, Domain, Model
  6. * - 非必须的,项目可根据自己喜好使用
  7. *
  8. * @author dogstar <chanzonghuang@gmail.com> 2016-05-14
  9. */
  10. define('CUR_PATH', dirname(__FILE__));
  11. if ($argc < 3) {
  12. echo "\n";
  13. echo colorfulString("Usage:\n", 'WARNING');
  14. echo " $argv[0] <app_path> <api_path> [author] [overwride]\n";
  15. echo "\n";
  16. echo colorfulString("Options:\n", 'WARNING');
  17. echo colorfulString(' app_path', 'NOTE'), " Require. App relative path to PhalApi\n";
  18. echo colorfulString(' api_path', 'NOTE'), " Require. Api relative path to app_pathe\n";
  19. echo colorfulString(' author', 'NOTE'), " NOT require. Your great name here, default is empty\n";
  20. echo colorfulString(' overwride', 'NOTE'), " NOT require. Whether overwrite, default is false\n";
  21. echo "\n";
  22. echo colorfulString("Demo:\n", 'WARNING');
  23. echo " $argv[0] ./Demo ./User dogstar\n";
  24. echo "\n";
  25. echo colorfulString("Tips:\n", 'WARNING');
  26. echo " This will create Api, Domain, and Model files if successfully.\n";
  27. echo "\n";
  28. exit(1);
  29. }
  30. // 接收参数
  31. $appPath = CUR_PATH . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . trim($argv[1]);
  32. $apiPath = str_replace('.php', '', ltrim(ltrim($argv[2], '.'), '/'));
  33. $author = isset($argv[3]) ? trim($argv[3]) : '';
  34. $overwrite = !empty($argv[4]) ? TRUE : FALSE;
  35. $appPathRealPath = realpath($appPath);
  36. if (!file_exists($appPathRealPath)) {
  37. echo colorfulString("$appPath not exists!\n", 'FAILURE');
  38. exit(1);
  39. }
  40. // 待生成的代码文件
  41. $apiFilePath = $appPath . DIRECTORY_SEPARATOR . 'Api' . DIRECTORY_SEPARATOR . $apiPath . '.php';
  42. $domainFilePath = $appPath . DIRECTORY_SEPARATOR . 'Domain' . DIRECTORY_SEPARATOR . $apiPath . '.php';
  43. $modelFilePath = $appPath . DIRECTORY_SEPARATOR . 'Model' . DIRECTORY_SEPARATOR . $apiPath . '.php';
  44. // 检测是否重复生成
  45. foreach (array($apiFilePath, $domainFilePath, $modelFilePath) as $file) {
  46. if (file_exists($file) && !$overwrite) {
  47. echo colorfulString("$file exists! Stop to create again!\n", 'FAILURE');
  48. exit(1);
  49. }
  50. }
  51. // 创建需要的目录
  52. foreach (array($apiFilePath, $domainFilePath, $modelFilePath) as $file) {
  53. $toCreateFile = substr($file, 0, strrpos($file, DIRECTORY_SEPARATOR));
  54. if (!file_exists($toCreateFile)) {
  55. echo colorfulString("Start to create folder $toCreateFile ...\n");
  56. mkdir($toCreateFile, 0755, TRUE);
  57. }
  58. }
  59. // 准备模板变量
  60. $API_NAME = str_replace(DIRECTORY_SEPARATOR, '_', $apiPath);
  61. $AUTHOR_NAME = $author;
  62. $CREATE_TIME = date('Y-m-d H:i:s');
  63. $TABLE_NAME = strtolower($API_NAME);
  64. // 生成代码
  65. $helperFolder = CUR_PATH . DIRECTORY_SEPARATOR . 'PhalApi' . DIRECTORY_SEPARATOR . 'Helper' . DIRECTORY_SEPARATOR;
  66. foreach (array('Api', 'Domain', 'Model') as $type) {
  67. $apiCode = str_replace(
  68. array('{%API_NAME%}', '{%AUTHOR_NAME%}', '{%CREATE_TIME%}', '{%TABLE_NAME%}'),
  69. array($API_NAME, $AUTHOR_NAME, $CREATE_TIME, $TABLE_NAME),
  70. file_get_contents($helperFolder . "_{$type}.php.tpl")
  71. );
  72. $maps = array(
  73. 'Api' => $apiFilePath,
  74. 'Domain' => $domainFilePath,
  75. 'Model' => $modelFilePath,
  76. );
  77. $toSaveFilePath = $maps[$type];
  78. echo colorfulString("Start to create file $toSaveFilePath ...\n");
  79. file_put_contents($toSaveFilePath, $apiCode);
  80. }
  81. echo colorfulString("\nOK! ${apiPath} has been created successfully!\n\n", 'SUCCESS');
  82. function colorfulString($text, $type = NULL) {
  83. $colors = array(
  84. 'WARNING' => '1;33',
  85. 'NOTE' => '1;36',
  86. 'SUCCESS' => '1;32',
  87. 'FAILURE' => '1;35',
  88. );
  89. if (empty($type) || !isset($colors[$type])){
  90. return $text;
  91. }
  92. return "\033[" . $colors[$type] . "m" . $text . "\033[0m";
  93. }
  94. function genSql($tableName, $tableKey, $sqlContent, $engine, $charset) {
  95. return sprintf("
  96. CREATE TABLE `%s` (
  97. `%s` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  98. %s
  99. `ext_data` text COMMENT 'json data here',
  100. PRIMARY KEY (`%s`)
  101. ) ENGINE=%s DEFAULT CHARSET=%s;
  102. ", $tableName, $tableKey, $sqlContent, $tableKey, $engine, $charset);
  103. }