ModelUtils.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\utils;
  4. // 引入框架内置类
  5. use think\facade\Db;
  6. use think\facade\Config;
  7. /**
  8. * 模型 utils
  9. *
  10. * @version 0.0.1
  11. * @author by huwhois
  12. * @time 2022/08/10
  13. */
  14. class ModelUtils
  15. {
  16. protected $namespace;
  17. protected $class;
  18. protected $tableName;
  19. protected $databaseName;
  20. protected function getPathName(string $name): string
  21. {
  22. $name = substr($name, 4);
  23. return app()->getBasePath() . ltrim(str_replace('\\', '/', $name), '/') . '.php';
  24. }
  25. protected function getStub(): string
  26. {
  27. return __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'model.stub';
  28. }
  29. /**
  30. * 获取表的主键
  31. * @return string
  32. */
  33. protected function getPrimarykey()
  34. {
  35. $sql = "SELECT COLUMN_NAME AS pk FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_NAME = '" . $this->tableName . "' AND table_schema = '.$this->databaseName.' AND CONSTRAINT_NAME = 'PRIMARY'";
  36. $res = Db::query($sql);
  37. $pk = empty($res) ? '' : $res[0]['pk'];
  38. return $pk;
  39. }
  40. /**
  41. * 获取表的所有字段信息
  42. * @return array
  43. */
  44. protected function getColumns(string $tableName = '', string $database = '')
  45. {
  46. $sql = "SELECT column_name,column_comment,data_type FROM information_schema.COLUMNS WHERE TABLE_NAME = '" . $this->tableName . "' AND table_schema = '" . $this->databaseName . "'";
  47. // echo $sql . "\n";
  48. $res = Db::query($sql);
  49. $columns = empty($res) ? [] : $res;
  50. return $columns;
  51. }
  52. /**
  53. * 构建Class文件
  54. * @return string
  55. */
  56. protected function buildClass()
  57. {
  58. $stub = file_get_contents($this->getStub());
  59. $primarykey = "";
  60. $pk = $this->getPrimarykey();
  61. if ($pk != "" && $pk != "id") {
  62. $primarykey = 'protected $pk = "' . $pk . '";';
  63. }
  64. $schemas = '';
  65. $autoWriteTimestamp = '';
  66. $create_time = false;
  67. $update_time = false;
  68. $columns = $this->getColumns();
  69. foreach ($columns as $column) {
  70. $schemas .= "\n" . ' "' . $column['column_name'] . '" => "' . $column['data_type'] . '", // ' . $column['column_comment'];
  71. if ($column['column_name'] == 'create_time') {
  72. $create_time = true;
  73. }
  74. if ($column['column_name'] == 'update_time') {
  75. $update_time = true;
  76. }
  77. }
  78. if (!$create_time || !$update_time) {
  79. $autoWriteTimestamp = 'protected $autoWriteTimestamp = false;';
  80. }
  81. return str_replace(['{%namespace%}', '{%className%}', '{%primarykey%}', '{%schemas%}', '{%autoWriteTimestamp%}'], [
  82. $this->namespace,
  83. $this->class,
  84. $primarykey,
  85. $schemas,
  86. $autoWriteTimestamp
  87. ], $stub);
  88. }
  89. /**
  90. * 生成 Model 文件
  91. * @param string $name 类目,包含命名空间
  92. * @param string $connections 数据库配置, 默认 mysql
  93. */
  94. public function makeModel(string $name, string $connections = 'mysql')
  95. {
  96. $pathname = $this->getPathName($name);
  97. if (is_file($pathname)) {
  98. throw new \Exception("Model :' . $name . ' already exists!</error>", 1);
  99. }
  100. if (!is_dir(dirname($pathname))) {
  101. mkdir(dirname($pathname), 0755, true);
  102. }
  103. file_put_contents($pathname, $this->codeModel($name, $connections));
  104. }
  105. /**
  106. * 生成 Model 不生成文件
  107. * @param string $name 类目,包含命名空间
  108. * @param string $connections 数据库配置, 默认 mysql
  109. * @return string
  110. */
  111. public function codeModel(string $name, string $connections = 'mysql')
  112. {
  113. $this->namespace = trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
  114. $this->class = str_replace($this->namespace . '\\', '', $name);
  115. $this->tableName = Config::get('database.connections.'.$connections.'.prefix') . strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $this->class));
  116. $this->databaseName = Config::get('database.connections.'.$connections.'.database');
  117. return $this->buildClass();
  118. }
  119. }