// +---------------------------------------------------------------------- namespace think\console\command\optimize; use think\console\Command; use think\console\Input; use think\console\Output; 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() { $files = \think\Config::get('route_config_file'); foreach ($files as $file) { if (is_file(CONF_PATH . $file . CONF_EXT)) { $config = include CONF_PATH . $file . CONF_EXT; if (is_array($config)) { \think\Route::import($config); } } } $rules = \think\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__]'; } } }