Completed
Push — 6.0 ( 54f609...174718 )
by yun
04:30
created

Route::execute()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 2
dl 0
loc 18
ccs 0
cts 11
cp 0
crap 30
rs 9.6111
c 0
b 0
f 0
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
use think\event\RouteLoaded;
18
19
class Route extends Command
20
{
21
    protected function configure()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function configure()
Loading history...
22
    {
23
        $this->setName('optimize:route')
24
            ->addArgument('app', Argument::OPTIONAL, 'app name.')
25
            ->setDescription('Build app route cache.');
26
    }
27
28
    protected function execute(Input $input, Output $output)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function execute()
Loading history...
29
    {
30
        $app = $input->getArgument('app');
31
32
        if (empty($app) && !is_dir($this->app->getBasePath() . 'controller')) {
33
            $output->writeln('<error>Miss app name!</error>');
34
            return false;
35
        }
36
37
        $path = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . ($app ? $app . DIRECTORY_SEPARATOR : '');
38
39
        $filename = $path . 'route.php';
40
        if (is_file($filename)) {
41
            unlink($filename);
42
        }
43
44
        file_put_contents($filename, $this->buildRouteCache($app));
45
        $output->writeln('<info>Succeed!</info>');
46
    }
47
48
    protected function buildRouteCache(string $app = null): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function buildRouteCache()
Loading history...
49
    {
50
        $this->app->route->clear();
51
        $this->app->route->lazy(false);
52
53
        // 路由检测
54
        $path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . ($app ? $app . DIRECTORY_SEPARATOR : '');
55
56
        $files = is_dir($path) ? scandir($path) : [];
57
58
        foreach ($files as $file) {
59
            if (strpos($file, '.php')) {
60
                include $path . $file;
61
            }
62
        }
63
64
        //触发路由载入完成事件
65
        $this->app->event->trigger(RouteLoaded::class);
66
67
        $content = '<?php ' . PHP_EOL . 'return ';
68
        $content .= '\think\App::unserialize(\'' . \think\App::serialize($this->app->route->getName()) . '\');';
69
        return $content;
70
    }
71
72
}
73