Schema.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. $list = scandir(APP_PATH . $module . DS . 'model');
  44. $app = App::$namespace;
  45. foreach ($list as $file) {
  46. if (0 === strpos($file, '.')) {
  47. continue;
  48. }
  49. $class = '\\' . $app . '\\' . $module . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
  50. $this->buildModelSchema($class);
  51. }
  52. $output->writeln('<info>Succeed!</info>');
  53. return;
  54. } elseif ($input->hasOption('table')) {
  55. $table = $input->getOption('table');
  56. if (!strpos($table, '.')) {
  57. $dbName = Db::connect($config)->getConfig('database');
  58. }
  59. $tables[] = $table;
  60. } elseif ($input->hasOption('db')) {
  61. $dbName = $input->getOption('db');
  62. $tables = Db::connect($config)->getTables($dbName);
  63. } elseif (!\think\Config::get('app_multi_module')) {
  64. $app = App::$namespace;
  65. $list = scandir(APP_PATH . 'model');
  66. foreach ($list as $file) {
  67. if (0 === strpos($file, '.')) {
  68. continue;
  69. }
  70. $class = '\\' . $app . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
  71. $this->buildModelSchema($class);
  72. }
  73. $output->writeln('<info>Succeed!</info>');
  74. return;
  75. } else {
  76. $tables = Db::connect($config)->getTables();
  77. }
  78. $db = isset($dbName) ? $dbName . '.' : '';
  79. $this->buildDataBaseSchema($tables, $db, $config);
  80. $output->writeln('<info>Succeed!</info>');
  81. }
  82. protected function buildModelSchema($class)
  83. {
  84. $reflect = new \ReflectionClass($class);
  85. if (!$reflect->isAbstract() && $reflect->isSubclassOf('\think\Model')) {
  86. $table = $class::getTable();
  87. $dbName = $class::getConfig('database');
  88. $content = '<?php ' . PHP_EOL . 'return ';
  89. $info = $class::getConnection()->getFields($table);
  90. $content .= var_export($info, true) . ';';
  91. file_put_contents(RUNTIME_PATH . 'schema' . DS . $dbName . '.' . $table . EXT, $content);
  92. }
  93. }
  94. protected function buildDataBaseSchema($tables, $db, $config)
  95. {
  96. if ('' == $db) {
  97. $dbName = Db::connect($config)->getConfig('database') . '.';
  98. } else {
  99. $dbName = $db;
  100. }
  101. foreach ($tables as $table) {
  102. $content = '<?php ' . PHP_EOL . 'return ';
  103. $info = Db::connect($config)->getFields($db . $table);
  104. $content .= var_export($info, true) . ';';
  105. file_put_contents(RUNTIME_PATH . 'schema' . DS . $dbName . $table . EXT, $content);
  106. }
  107. }
  108. }