Route.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\console\Command;
  13. use think\console\Input;
  14. use think\console\Output;
  15. use think\Container;
  16. class Route extends Command
  17. {
  18. /** @var Output */
  19. protected $output;
  20. protected function configure()
  21. {
  22. $this->setName('optimize:route')
  23. ->setDescription('Build route cache.');
  24. }
  25. protected function execute(Input $input, Output $output)
  26. {
  27. file_put_contents(Container::get('app')->getRuntimePath() . 'route.php', $this->buildRouteCache());
  28. $output->writeln('<info>Succeed!</info>');
  29. }
  30. protected function buildRouteCache()
  31. {
  32. Container::get('route')->setName([]);
  33. Container::get('config')->set('url_lazy_route', false);
  34. // 路由检测
  35. $path = Container::get('app')->getRoutePath();
  36. $files = is_dir($path) ? scandir($path) : [];
  37. foreach ($files as $file) {
  38. if (strpos($file, '.php')) {
  39. $filename = $path . DIRECTORY_SEPARATOR . $file;
  40. // 导入路由配置
  41. $rules = include $filename;
  42. if (is_array($rules)) {
  43. Container::get('route')->import($rules);
  44. }
  45. }
  46. }
  47. if (Container::get('config')->get('route_annotation')) {
  48. include Container::get('build')->buildRoute();
  49. }
  50. $content = '<?php ' . PHP_EOL . 'return ';
  51. $content .= var_export(Container::get('route')->getName(), true) . ';';
  52. return $content;
  53. }
  54. }