Config.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\Config as ThinkConfig;
  13. use think\console\Command;
  14. use think\console\Input;
  15. use think\console\input\Argument;
  16. use think\console\Output;
  17. class Config extends Command
  18. {
  19. /** @var Output */
  20. protected $output;
  21. protected function configure()
  22. {
  23. $this->setName('optimize:config')
  24. ->addArgument('module', Argument::OPTIONAL, 'Build module config cache .')
  25. ->setDescription('Build config and common file cache.');
  26. }
  27. protected function execute(Input $input, Output $output)
  28. {
  29. if ($input->getArgument('module')) {
  30. $module = $input->getArgument('module') . DS;
  31. } else {
  32. $module = '';
  33. }
  34. $content = '<?php ' . PHP_EOL . $this->buildCacheContent($module);
  35. if (!is_dir(RUNTIME_PATH . $module)) {
  36. @mkdir(RUNTIME_PATH . $module, 0755, true);
  37. }
  38. file_put_contents(RUNTIME_PATH . $module . 'init' . EXT, $content);
  39. $output->writeln('<info>Succeed!</info>');
  40. }
  41. protected function buildCacheContent($module)
  42. {
  43. $content = '';
  44. $path = realpath(APP_PATH . $module) . DS;
  45. if ($module) {
  46. // 加载模块配置
  47. $config = ThinkConfig::load(CONF_PATH . $module . 'config' . CONF_EXT);
  48. // 读取数据库配置文件
  49. $filename = CONF_PATH . $module . 'database' . CONF_EXT;
  50. ThinkConfig::load($filename, 'database');
  51. // 加载应用状态配置
  52. if ($config['app_status']) {
  53. $config = ThinkConfig::load(CONF_PATH . $module . $config['app_status'] . CONF_EXT);
  54. }
  55. // 读取扩展配置文件
  56. if (is_dir(CONF_PATH . $module . 'extra')) {
  57. $dir = CONF_PATH . $module . 'extra';
  58. $files = scandir($dir);
  59. foreach ($files as $file) {
  60. if (strpos($file, CONF_EXT)) {
  61. $filename = $dir . DS . $file;
  62. ThinkConfig::load($filename, pathinfo($file, PATHINFO_FILENAME));
  63. }
  64. }
  65. }
  66. }
  67. // 加载行为扩展文件
  68. if (is_file(CONF_PATH . $module . 'tags' . EXT)) {
  69. $content .= '\think\Hook::import(' . (var_export(include CONF_PATH . $module . 'tags' . EXT, true)) . ');' . PHP_EOL;
  70. }
  71. // 加载公共文件
  72. if (is_file($path . 'common' . EXT)) {
  73. $content .= substr(php_strip_whitespace($path . 'common' . EXT), 5) . PHP_EOL;
  74. }
  75. $content .= '\think\Config::set(' . var_export(ThinkConfig::get(), true) . ');';
  76. return $content;
  77. }
  78. }