Schema.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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('db', null, Option::VALUE_REQUIRED, 'db name .')
  26. ->addOption('table', null, Option::VALUE_REQUIRED, 'table name .')
  27. ->addOption('module', null, Option::VALUE_REQUIRED, 'module name .')
  28. ->setDescription('Build database schema cache.');
  29. }
  30. protected function execute(Input $input, Output $output)
  31. {
  32. if (!is_dir(RUNTIME_PATH . 'schema')) {
  33. @mkdir(RUNTIME_PATH . 'schema', 0755, true);
  34. }
  35. if ($input->hasOption('module')) {
  36. $module = $input->getOption('module');
  37. // 读取模型
  38. $list = scandir(APP_PATH . $module . DS . 'model');
  39. $app = App::$namespace;
  40. foreach ($list as $file) {
  41. if ('.' == $file || '..' == $file) {
  42. continue;
  43. }
  44. $class = '\\' . $app . '\\' . $module . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
  45. $this->buildModelSchema($class);
  46. }
  47. $output->writeln('<info>Succeed!</info>');
  48. return;
  49. } elseif ($input->hasOption('table')) {
  50. $table = $input->getOption('table');
  51. if (!strpos($table, '.')) {
  52. $dbName = Db::getConfig('database');
  53. }
  54. $tables[] = $table;
  55. } elseif ($input->hasOption('db')) {
  56. $dbName = $input->getOption('db');
  57. $tables = Db::getTables($dbName);
  58. } elseif (!\think\Config::get('app_multi_module')) {
  59. $app = App::$namespace;
  60. $list = scandir(APP_PATH . 'model');
  61. foreach ($list as $file) {
  62. if ('.' == $file || '..' == $file) {
  63. continue;
  64. }
  65. $class = '\\' . $app . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
  66. $this->buildModelSchema($class);
  67. }
  68. $output->writeln('<info>Succeed!</info>');
  69. return;
  70. } else {
  71. $tables = Db::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(RUNTIME_PATH . 'schema' . DS . $dbName . '.' . $table . EXT, $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::getFields($db . $table);
  99. $content .= var_export($info, true) . ';';
  100. file_put_contents(RUNTIME_PATH . 'schema' . DS . $dbName . $table . EXT, $content);
  101. }
  102. }
  103. }