Route.php
2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
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('<info>Succeed!</info>');
}
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 = '<?php ' . PHP_EOL . 'return ';
$content .= var_export($rules, true) . ';';
$content = str_replace(['\'[__start__', '__end__]\''], '', stripcslashes($content));
return $content;
}
protected function buildClosure(&$value)
{
if ($value instanceof \Closure) {
$reflection = new \ReflectionFunction($value);
$startLine = $reflection->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__]';
}
}
}