// +---------------------------------------------------------------------- namespace think\console\command\optimize; use think\console\command\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() { $config = include CONF_PATH . 'route' . CONF_EXT; \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__]'; } } }