ValidateMake.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace app\crud\make\make;
  3. use app\crud\make\ToAutoMake;
  4. use Symfony\Component\VarExporter\VarExporter;
  5. use think\console\Output;
  6. use think\facade\App;
  7. use think\facade\Db;
  8. class ValidateMake implements ToAutoMake
  9. {
  10. public function check($table, $path)
  11. {
  12. !defined('DS') && define('DS', DIRECTORY_SEPARATOR);
  13. $validateName = ucfirst(camelize($table)) . 'Validate';
  14. $validateFilePath = base_path() . $path . DS . 'validate' . DS . $validateName . '.php';
  15. if (!is_dir(base_path() . $path . DS . 'validate')) {
  16. mkdir(base_path() . $path . DS . 'validate', 0755, true);
  17. }
  18. if (file_exists($validateFilePath)) {
  19. $output = new Output();
  20. $output->error("$validateName.php已经存在");
  21. exit;
  22. }
  23. }
  24. public function make($table, $path, $other)
  25. {
  26. $validateTpl = dirname(dirname(__DIR__)) . '/tpl/validate.tpl';
  27. $tplContent = file_get_contents($validateTpl);
  28. $model = ucfirst(camelize($table));
  29. $filePath = empty($path) ? '' : DS . $path;
  30. $namespace = empty($path) ? '\\' : '\\' . $path . '\\';
  31. //$prefix = config('database.connections.mysql.prefix');
  32. //$column = Db::query('SHOW FULL COLUMNS FROM `' . $prefix . $table . '`');
  33. $column = get_cache('crud_v_'.$table);
  34. $rule = [];
  35. $message = [];
  36. foreach ($column as $vo) {
  37. $rule[$vo['field']] = 'require';
  38. $message[$vo['field'].'.require'] = $vo['title'].'不能为空';
  39. }
  40. $ruleArr = VarExporter::export($rule);
  41. $messageArr = VarExporter::export($message);
  42. $tplContent = str_replace('<namespace>', $namespace, $tplContent);
  43. $tplContent = str_replace('<model>', $model, $tplContent);
  44. $tplContent = str_replace('<rule>', '' . $ruleArr, $tplContent);
  45. $tplContent = str_replace('<message>', $messageArr, $tplContent);
  46. file_put_contents(base_path() . $filePath . DS . 'validate' . DS . $model . 'Validate.php', $tplContent);
  47. }
  48. }