Schema.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\console\command\optimize;
  12. use think\console\Command;
  13. use think\console\Input;
  14. use think\console\input\Option;
  15. use think\console\Output;
  16. use think\Db;
  17. use think\facade\App;
  18. class Schema extends Command
  19. {
  20. protected function configure()
  21. {
  22. $this->setName('optimize:schema')
  23. ->addOption('db', null, Option::VALUE_REQUIRED, 'db name .')
  24. ->addOption('table', null, Option::VALUE_REQUIRED, 'table name .')
  25. ->addOption('module', null, Option::VALUE_REQUIRED, 'module name .')
  26. ->setDescription('Build database schema cache.');
  27. }
  28. protected function execute(Input $input, Output $output)
  29. {
  30. if (!is_dir(App::getRuntimePath() . 'schema')) {
  31. @mkdir(App::getRuntimePath() . 'schema', 0755, true);
  32. }
  33. if ($input->hasOption('module')) {
  34. $module = $input->getOption('module');
  35. // 读取模型
  36. $path = App::getAppPath() . $module . DIRECTORY_SEPARATOR . 'model';
  37. $list = is_dir($path) ? scandir($path) : [];
  38. $namespace = App::getNamespace();
  39. foreach ($list as $file) {
  40. if (0 === strpos($file, '.')) {
  41. continue;
  42. }
  43. $class = '\\' . $namespace . '\\' . $module . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
  44. $this->buildModelSchema($class);
  45. }
  46. $output->writeln('<info>Succeed!</info>');
  47. return;
  48. } elseif ($input->hasOption('table')) {
  49. $table = $input->getOption('table');
  50. if (false === strpos($table, '.')) {
  51. $dbName = Db::getConfig('database');
  52. }
  53. $tables[] = $table;
  54. } elseif ($input->hasOption('db')) {
  55. $dbName = $input->getOption('db');
  56. $tables = Db::getConnection()->getTables($dbName);
  57. } elseif (!\think\facade\Config::get('app_multi_module')) {
  58. $namespace = App::getNamespace();
  59. $path = App::getAppPath() . 'model';
  60. $list = is_dir($path) ? scandir($path) : [];
  61. foreach ($list as $file) {
  62. if (0 === strpos($file, '.')) {
  63. continue;
  64. }
  65. $class = '\\' . $namespace . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
  66. $this->buildModelSchema($class);
  67. }
  68. $output->writeln('<info>Succeed!</info>');
  69. return;
  70. } else {
  71. $tables = Db::getConnection()->getTables();
  72. }
  73. $db = isset($dbName) ? $dbName . '.' : '';
  74. $this->buildDataBaseSchema($tables, $db);
  75. $output->writeln('<info>Succeed!</info>');
  76. }
  77. protected function buildModelSchema($class)
  78. {
  79. $reflect = new \ReflectionClass($class);
  80. if (!$reflect->isAbstract() && $reflect->isSubclassOf('\think\Model')) {
  81. $table = $class::getTable();
  82. $dbName = $class::getConfig('database');
  83. $content = '<?php ' . PHP_EOL . 'return ';
  84. $info = $class::getConnection()->getFields($table);
  85. $content .= var_export($info, true) . ';';
  86. file_put_contents(App::getRuntimePath() . 'schema' . DIRECTORY_SEPARATOR . $dbName . '.' . $table . '.php', $content);
  87. }
  88. }
  89. protected function buildDataBaseSchema($tables, $db)
  90. {
  91. if ('' == $db) {
  92. $dbName = Db::getConfig('database') . '.';
  93. } else {
  94. $dbName = $db;
  95. }
  96. foreach ($tables as $table) {
  97. $content = '<?php ' . PHP_EOL . 'return ';
  98. $info = Db::getConnection()->getFields($db . $table);
  99. $content .= var_export($info, true) . ';';
  100. file_put_contents(App::getRuntimePath() . 'schema' . DIRECTORY_SEPARATOR . $dbName . $table . '.php', $content);
  101. }
  102. }
  103. }