diff --git a/library/think/App.php b/library/think/App.php index 6daf7300..c1388b51 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -65,6 +65,7 @@ class App protected static $routeMust; protected static $dispatch; + protected static $file = []; /** * 执行应用程序 @@ -388,8 +389,9 @@ class App if (!empty($config['extra_file_list'])) { foreach ($config['extra_file_list'] as $file) { $file = strpos($file, '.') ? $file : APP_PATH . $file . EXT; - if (is_file($file)) { - include_once $file; + if (is_file($file) && !isset(self::$file[$file])) { + include $file; + self::$file[$file] = true; } } } @@ -435,6 +437,9 @@ class App if ($config['extra_config_list']) { foreach ($config['extra_config_list'] as $name => $file) { $filename = CONF_PATH . $module . $file . CONF_EXT; + if ('route' == $module . $file && is_file(RUNTIME_PATH . 'route.php')) { + continue; + } Config::load($filename, is_string($name) ? $name : pathinfo($filename, PATHINFO_FILENAME)); } } @@ -479,7 +484,9 @@ class App $check = !is_null(self::$routeCheck) ? self::$routeCheck : $config['url_route_on']; if ($check) { // 开启路由 - if (!empty($config['route'])) { + if (is_file(RUNTIME_PATH . 'route.php')) { + Route::rules(include RUNTIME_PATH . 'route.php'); + } elseif (!empty($config['route'])) { // 导入路由配置 Route::import($config['route']); } diff --git a/library/think/Console.php b/library/think/Console.php index f65fe024..7859974f 100644 --- a/library/think/Console.php +++ b/library/think/Console.php @@ -54,6 +54,7 @@ class Console "think\\console\\command\\make\\Model", "think\\console\\command\\optimize\\Autoload", "think\\console\\command\\optimize\\Config", + "think\\console\\command\\optimize\\Route", ]; public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN') diff --git a/library/think/Route.php b/library/think/Route.php index 9ac74d33..2a05a716 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -658,7 +658,7 @@ class Route if (is_array($rules)) { self::$rules = $rules; } elseif ($rules) { - return self::$rules[$rules]; + return true === $rules ? self::$rules : self::$rules[$rules]; } else { $rules = self::$rules; unset($rules['pattern'], $rules['alias'], $rules['domain']); diff --git a/library/think/console/command/optimize/Route.php b/library/think/console/command/optimize/Route.php new file mode 100644 index 00000000..c2954452 --- /dev/null +++ b/library/think/console/command/optimize/Route.php @@ -0,0 +1,66 @@ + +// +---------------------------------------------------------------------- +namespace think\console\command\optimize; + +use think\console\command\Command; +use think\console\Input; +use think\console\Output; +use think\Route; + +class Route extends Command +{ + /** @var Output */ + protected $output; + + protected function configure() + { + $this->setName('optimize:route') + ->setDescription('Build route cache.'); + } + + protected function execute(Input $input, Output $output) + { + + file_put_contents(RUNTIME_PATH . 'route.php', $this->buildRouteCache()); + + $output->writeln('Succeed!'); + } + + protected function buildRouteCache() + { + $config = include CONF_PATH . 'route' . CONF_EXT; + Route::import($config); + $rules = Route::rules(true); + array_walk_recursive($rules, [$this, 'buildClosure']); + $content = 'getStartLine(); + $endLine = $reflection->getEndLine(); + $file = $reflection->getFileName(); + $item = file($file); + $content = ''; + for ($i = $startLine - 1; $i <= $endLine - 1; $i++) { + $content .= $item[$i]; + } + $start = strpos($content, 'function'); + $end = strrpos($content, '}'); + $value = '[__start__' . substr($content, $start, $end - $start + 1) . '__end__]'; + } + } +}