Config.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2017 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\Argument;
  15. use think\console\Output;
  16. use think\Container;
  17. use think\facade\App;
  18. class Config extends Command
  19. {
  20. protected function configure()
  21. {
  22. $this->setName('optimize:config')
  23. ->addArgument('module', Argument::OPTIONAL, 'Build module config cache .')
  24. ->setDescription('Build config and common file cache.');
  25. }
  26. protected function execute(Input $input, Output $output)
  27. {
  28. if ($input->getArgument('module')) {
  29. $module = $input->getArgument('module') . DIRECTORY_SEPARATOR;
  30. } else {
  31. $module = '';
  32. }
  33. $content = '<?php ' . PHP_EOL . $this->buildCacheContent($module);
  34. $runtimePath = App::getRuntimePath();
  35. if (!is_dir($runtimePath . $module)) {
  36. @mkdir($runtimePath . $module, 0755, true);
  37. }
  38. file_put_contents($runtimePath . $module . 'init.php', $content);
  39. $output->writeln('<info>Succeed!</info>');
  40. }
  41. protected function buildCacheContent($module)
  42. {
  43. $content = '// This cache file is automatically generated at:' . date('Y-m-d H:i:s') . PHP_EOL;
  44. $path = realpath(App::getAppPath() . $module) . DIRECTORY_SEPARATOR;
  45. if ($module) {
  46. $configPath = is_dir($path . 'config') ? $path . 'config' : App::getConfigPath() . $module;
  47. } else {
  48. $configPath = App::getConfigPath();
  49. }
  50. $ext = App::getConfigExt();
  51. $config = Container::get('config');
  52. $files = is_dir($configPath) ? scandir($configPath) : [];
  53. foreach ($files as $file) {
  54. if ('.' . pathinfo($file, PATHINFO_EXTENSION) === $ext) {
  55. $filename = $configPath . DIRECTORY_SEPARATOR . $file;
  56. $config->load($filename, pathinfo($file, PATHINFO_FILENAME));
  57. }
  58. }
  59. // 加载行为扩展文件
  60. if (is_file($path . 'tags.php')) {
  61. $tags = include $path . 'tags.php';
  62. if (is_array($tags)) {
  63. $content .= PHP_EOL . '\think\facade\Hook::import(' . (var_export($tags, true)) . ');' . PHP_EOL;
  64. }
  65. }
  66. // 加载公共文件
  67. if (is_file($path . 'common.php')) {
  68. $common = substr(php_strip_whitespace($path . 'common.php'), 6);
  69. if ($common) {
  70. $content .= PHP_EOL . $common . PHP_EOL;
  71. }
  72. }
  73. if ('' == $module) {
  74. $content .= PHP_EOL . substr(php_strip_whitespace(App::getThinkPath() . 'helper.php'), 6) . PHP_EOL;
  75. if (is_file($path . 'middleware.php')) {
  76. $middleware = include $path . 'middleware.php';
  77. if (is_array($middleware)) {
  78. $content .= PHP_EOL . '\think\Container::get("middleware")->import(' . var_export($middleware, true) . ');' . PHP_EOL;
  79. }
  80. }
  81. }
  82. if (is_file($path . 'provider.php')) {
  83. $provider = include $path . 'provider.php';
  84. if (is_array($provider)) {
  85. $content .= PHP_EOL . '\think\Container::getInstance()->bindTo(' . var_export($provider, true) . ');' . PHP_EOL;
  86. }
  87. }
  88. $content .= PHP_EOL . '\think\facade\Config::set(' . var_export($config->get(), true) . ');' . PHP_EOL;
  89. return $content;
  90. }
  91. }