ValidateUtils.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 20228/10
  13. */
  14. class ValidateUtils
  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 . 'validate.stub';
  28. }
  29. /**
  30. * 获取表的所有字段信息
  31. * @return array
  32. */
  33. protected function getColumns()
  34. {
  35. $sql = "SELECT column_name,column_comment,data_type FROM information_schema.COLUMNS WHERE TABLE_NAME = '" . $this->tableName . "' AND table_schema = '" . $this->databaseName . "'";
  36. // echo $sql . "\n";
  37. $res = Db::query($sql);
  38. $columns = empty($res) ? [] : $res;
  39. return $columns;
  40. }
  41. /**
  42. * 构建Class文件
  43. * @return string
  44. */
  45. protected function buildClass()
  46. {
  47. $stub = file_get_contents($this->getStub());
  48. $schemas = '';
  49. $columns = $this->getColumns();
  50. foreach ($columns as $column) {
  51. $schemas .= "\n" . ' "' . $column['column_name'] . '" => "require",';
  52. }
  53. return str_replace(['{%namespace%}', '{%className%}', '{%schemas%}'], [
  54. $this->namespace,
  55. $this->class,
  56. $schemas
  57. ], $stub);
  58. }
  59. /**
  60. * 生成 Model 文件
  61. * @param string $name 类目,包含命名空间
  62. * @param string $connections 数据库配置, 默认 mysql
  63. */
  64. public function makeModel(string $name, string $connections = 'mysql')
  65. {
  66. $pathname = $this->getPathName($name);
  67. if (is_file($pathname)) {
  68. throw new \Exception("Valiate :' . $name . ' already exists!</error>", 1);
  69. }
  70. if (!is_dir(dirname($pathname))) {
  71. mkdir(dirname($pathname), 0755, true);
  72. }
  73. file_put_contents($pathname, $this->codeModel($name, $connections));
  74. }
  75. /**
  76. * 生成 Model 不生成文件
  77. * @param string $name 类目,包含命名空间
  78. * @param string $connections 数据库配置, 默认 mysql
  79. * @return string
  80. */
  81. public function codeModel(string $name, string $connections = 'mysql')
  82. {
  83. $this->namespace = trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
  84. $this->class = str_replace($this->namespace . '\\', '', $name);
  85. $this->tableName = Config::get('database.connections.'.$connections.'.prefix') . strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $this->class));
  86. $this->databaseName = Config::get('database.connections.'.$connections.'.database');
  87. return $this->buildClass();
  88. }
  89. }