Schema.php 4.5 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\App;
  13. use think\console\Command;
  14. use think\console\Input;
  15. use think\console\input\Option;
  16. use think\console\Output;
  17. use think\Db;
  18. class Schema extends Command
  19. {
  20. /** @var Output */
  21. protected $output;
  22. protected function configure()
  23. {
  24. $this->setName('optimize:schema')
  25. ->addOption('config', null, Option::VALUE_REQUIRED, 'db config .')
  26. ->addOption('db', null, Option::VALUE_REQUIRED, 'db name .')
  27. ->addOption('table', null, Option::VALUE_REQUIRED, 'table name .')
  28. ->addOption('module', null, Option::VALUE_REQUIRED, 'module name .')
  29. ->setDescription('Build database schema cache.');
  30. }
  31. protected function execute(Input $input, Output $output)
  32. {
  33. if (!is_dir(RUNTIME_PATH . 'schema')) {
  34. @mkdir(RUNTIME_PATH . 'schema', 0755, true);
  35. }
  36. $config = [];
  37. if ($input->hasOption('config')) {
  38. $config = $input->getOption('config');
  39. }
  40. if ($input->hasOption('module')) {
  41. $module = $input->getOption('module');
  42. // 读取模型
  43. $path = APP_PATH . $module . DS . 'model';
  44. $list = is_dir($path) ? scandir($path) : [];
  45. $app = App::$namespace;
  46. foreach ($list as $file) {
  47. if (0 === strpos($file, '.')) {
  48. continue;
  49. }
  50. $class = '\\' . $app . '\\' . $module . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
  51. $this->buildModelSchema($class);
  52. }
  53. $output->writeln('<info>Succeed!</info>');
  54. return;
  55. } elseif ($input->hasOption('table')) {
  56. $table = $input->getOption('table');
  57. if (!strpos($table, '.')) {
  58. $dbName = Db::connect($config)->getConfig('database');
  59. }
  60. $tables[] = $table;
  61. } elseif ($input->hasOption('db')) {
  62. $dbName = $input->getOption('db');
  63. $tables = Db::connect($config)->getTables($dbName);
  64. } elseif (!\think\Config::get('app_multi_module')) {
  65. $app = App::$namespace;
  66. $path = APP_PATH . 'model';
  67. $list = is_dir($path) ? scandir($path) : [];
  68. foreach ($list as $file) {
  69. if (0 === strpos($file, '.')) {
  70. continue;
  71. }
  72. $class = '\\' . $app . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
  73. $this->buildModelSchema($class);
  74. }
  75. $output->writeln('<info>Succeed!</info>');
  76. return;
  77. } else {
  78. $tables = Db::connect($config)->getTables();
  79. }
  80. $db = isset($dbName) ? $dbName . '.' : '';
  81. $this->buildDataBaseSchema($tables, $db, $config);
  82. $output->writeln('<info>Succeed!</info>');
  83. }
  84. protected function buildModelSchema($class)
  85. {
  86. $reflect = new \ReflectionClass($class);
  87. if (!$reflect->isAbstract() && $reflect->isSubclassOf('\think\Model')) {
  88. $table = $class::getTable();
  89. $dbName = $class::getConfig('database');
  90. $content = '<?php ' . PHP_EOL . 'return ';
  91. $info = $class::getConnection()->getFields($table);
  92. $content .= var_export($info, true) . ';';
  93. file_put_contents(RUNTIME_PATH . 'schema' . DS . $dbName . '.' . $table . EXT, $content);
  94. }
  95. }
  96. protected function buildDataBaseSchema($tables, $db, $config)
  97. {
  98. if ('' == $db) {
  99. $dbName = Db::connect($config)->getConfig('database') . '.';
  100. } else {
  101. $dbName = $db;
  102. }
  103. foreach ($tables as $table) {
  104. $content = '<?php ' . PHP_EOL . 'return ';
  105. $info = Db::connect($config)->getFields($db . $table);
  106. $content .= var_export($info, true) . ';';
  107. file_put_contents(RUNTIME_PATH . 'schema' . DS . $dbName . $table . EXT, $content);
  108. }
  109. }
  110. }