增加路由缓存功能

This commit is contained in:
thinkphp
2016-08-03 09:26:26 +08:00
parent c0d65a8eb2
commit eda33a2412
4 changed files with 78 additions and 4 deletions

View File

@@ -65,6 +65,7 @@ class App
protected static $routeMust;
protected static $dispatch;
protected static $file = [];
/**
* 执行应用程序
@@ -388,8 +389,9 @@ class App
if (!empty($config['extra_file_list'])) {
foreach ($config['extra_file_list'] as $file) {
$file = strpos($file, '.') ? $file : APP_PATH . $file . EXT;
if (is_file($file)) {
include_once $file;
if (is_file($file) && !isset(self::$file[$file])) {
include $file;
self::$file[$file] = true;
}
}
}
@@ -435,6 +437,9 @@ class App
if ($config['extra_config_list']) {
foreach ($config['extra_config_list'] as $name => $file) {
$filename = CONF_PATH . $module . $file . CONF_EXT;
if ('route' == $module . $file && is_file(RUNTIME_PATH . 'route.php')) {
continue;
}
Config::load($filename, is_string($name) ? $name : pathinfo($filename, PATHINFO_FILENAME));
}
}
@@ -479,7 +484,9 @@ class App
$check = !is_null(self::$routeCheck) ? self::$routeCheck : $config['url_route_on'];
if ($check) {
// 开启路由
if (!empty($config['route'])) {
if (is_file(RUNTIME_PATH . 'route.php')) {
Route::rules(include RUNTIME_PATH . 'route.php');
} elseif (!empty($config['route'])) {
// 导入路由配置
Route::import($config['route']);
}

View File

@@ -54,6 +54,7 @@ class Console
"think\\console\\command\\make\\Model",
"think\\console\\command\\optimize\\Autoload",
"think\\console\\command\\optimize\\Config",
"think\\console\\command\\optimize\\Route",
];
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')

View File

@@ -658,7 +658,7 @@ class Route
if (is_array($rules)) {
self::$rules = $rules;
} elseif ($rules) {
return self::$rules[$rules];
return true === $rules ? self::$rules : self::$rules[$rules];
} else {
$rules = self::$rules;
unset($rules['pattern'], $rules['alias'], $rules['domain']);

View File

@@ -0,0 +1,66 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\console\command\optimize;
use think\console\command\Command;
use think\console\Input;
use think\console\Output;
use think\Route;
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()
{
$config = include CONF_PATH . 'route' . CONF_EXT;
Route::import($config);
$rules = 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__]';
}
}
}