|
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 <[email protected]> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
namespace think\console\command\optimize; |
|
12
|
|
|
|
|
13
|
|
|
use think\console\Command; |
|
14
|
|
|
use think\console\Input; |
|
15
|
|
|
use think\console\input\Argument; |
|
16
|
|
|
use think\console\Output; |
|
17
|
|
|
|
|
18
|
|
|
class Route extends Command |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
protected function configure() |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
$this->setName('optimize:route') |
|
23
|
|
|
->addArgument('app', Argument::OPTIONAL, 'app name.') |
|
24
|
|
|
->setDescription('Build app route cache.'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
protected function execute(Input $input, Output $output) |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
$app = $input->getArgument('app'); |
|
30
|
|
|
|
|
31
|
|
|
if (empty($app) && !is_dir($this->app->getBasePath() . 'controller')) { |
|
32
|
|
|
$output->writeln('<error>Miss app name!</error>'); |
|
33
|
|
|
return false; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$path = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . ($app ? $app . DIRECTORY_SEPARATOR : ''); |
|
37
|
|
|
|
|
38
|
|
|
$filename = $path . 'route.php'; |
|
39
|
|
|
if (is_file($filename)) { |
|
40
|
|
|
unlink($filename); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
file_put_contents($filename, $this->buildRouteCache($app)); |
|
44
|
|
|
$output->writeln('<info>Succeed!</info>'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function buildRouteCache(string $app = null): string |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$this->app->route->clear(); |
|
50
|
|
|
$this->app->route->lazy(false); |
|
51
|
|
|
|
|
52
|
|
|
// 路由检测 |
|
53
|
|
|
$path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . ($app ? $app . DIRECTORY_SEPARATOR : ''); |
|
54
|
|
|
|
|
55
|
|
|
$files = is_dir($path) ? scandir($path) : []; |
|
56
|
|
|
|
|
57
|
|
|
foreach ($files as $file) { |
|
58
|
|
|
if (strpos($file, '.php')) { |
|
59
|
|
|
include $path . $file; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ($this->app->config->get('route.route_annotation')) { |
|
64
|
|
|
$this->app->console->call('route:build', [$app ?: '']); |
|
65
|
|
|
$filename = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . ($app ? $app . DIRECTORY_SEPARATOR : '') . 'build_route.php'; |
|
66
|
|
|
|
|
67
|
|
|
if (is_file($filename)) { |
|
68
|
|
|
include $filename; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$content = '<?php ' . PHP_EOL . 'return '; |
|
73
|
|
|
$content .= '\think\App::unserialize(\'' . \think\App::serialize($this->app->route->getName()) . '\');'; |
|
74
|
|
|
return $content; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|