Completed
Push — 6.0 ( caeff7...3b655b )
by liu
03:11
created

Route::buildDirRoute()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 10
nop 4
dl 0
loc 31
ccs 0
cts 16
cp 0
crap 30
rs 9.4555
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class Route
Loading history...
19
{
20
    protected function configure()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function configure()
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function execute()
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function buildRouteCache()
Loading history...
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