/**
* 執行應用例項 入口檔案使用的快捷方法
* @access public
* @return void
*/
static public function run() {
// 應用初始化標籤
Hook::listen('app_init');
App::init();
// 應用開始標籤
Hook::listen('app_begin');
// Session初始化
if(!IS_CLI){
session(C('SESSION_OPTIONS'));
}
// 記錄應用初始化時間
G('initTime');
App::exec();
// 應用結束標籤
Hook::listen('app_end');
return ;
}
複製程式碼
監聽app_init
'app_init' => array(
'Behavior\BuildLiteBehavior', // 生成執行Lite檔案
),
複製程式碼
app_init就是執行生成Lite檔案
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace Behavior;
// 建立Lite執行檔案
// 可以替換框架入口檔案執行
// 建議繫結位置app_init
class BuildLiteBehavior {
public function run(&$params) {
if(!defined('BUILD_LITE_FILE')) return ;
$litefile = C('RUNTIME_LITE_FILE',null,RUNTIME_PATH.'lite.php');
if(is_file($litefile)) return;
$defs = get_defined_constants(TRUE);
$content = 'namespace {$GLOBALS[\'_beginTime\'] = microtime(TRUE);';
if(MEMORY_LIMIT_ON) {
$content .= '$GLOBALS[\'_startUseMems\'] = memory_get_usage();';
}
// 生成陣列定義
unset($defs['user']['BUILD_LITE_FILE']);
$content .= $this->buildArrayDefine($defs['user']).'}';
// 讀取編譯列表檔案
$filelist = is_file(CONF_PATH.'lite.php')?
include CONF_PATH.'lite.php':
array(
THINK_PATH.'Common/functions.php',
COMMON_PATH.'Common/function.php',
CORE_PATH . 'Think'.EXT,
CORE_PATH . 'Hook'.EXT,
CORE_PATH . 'App'.EXT,
CORE_PATH . 'Dispatcher'.EXT,
CORE_PATH . 'Log'.EXT,
CORE_PATH . 'Log/Driver/File'.EXT,
CORE_PATH . 'Route'.EXT,
CORE_PATH . 'Controller'.EXT,
CORE_PATH . 'View'.EXT,
CORE_PATH . 'Storage'.EXT,
CORE_PATH . 'Storage/Driver/File'.EXT,
CORE_PATH . 'Exception'.EXT,
BEHAVIOR_PATH . 'ParseTemplateBehavior'.EXT,
BEHAVIOR_PATH . 'ContentReplaceBehavior'.EXT,
);
// 編譯檔案
foreach ($filelist as $file){
if(is_file($file)) {
$content .= compile($file);
}
}
// 處理Think類的start方法
$content = preg_replace('/\$runtimefile = RUNTIME_PATH(.+?)(if\(APP_STATUS)/','\2',$content,1);
$content .= "\nnamespace { Think\Think::addMap(".var_export(\Think\Think::getMap(),true).");";
$content .= "\nL(".var_export(L(),true).");\nC(".var_export(C(),true).');Think\Hook::import('.var_export(\Think\Hook::get(),true).');Think\Think::start();}';
// 生成執行Lite檔案
file_put_contents($litefile,strip_whitespace('<?php '.$content));
}
// 根據陣列生成常量定義
private function buildArrayDefine($array) {
$content = "\n";
foreach ($array as $key => $val) {
$key = strtoupper($key);
$content .= 'defined(\'' . $key . '\') or ';
if (is_int($val) || is_float($val)) {
$content .= "define('" . $key . "'," . $val . ');';
} elseif (is_bool($val)) {
$val = ($val) ? 'true' : 'false';
$content .= "define('" . $key . "'," . $val . ');';
} elseif (is_string($val)) {
$content .= "define('" . $key . "','" . addslashes($val) . "');";
}
$content .= "\n";
}
return $content;
}
}
複製程式碼
App::init();
/**
* 應用程式初始化
* @access public
* @return void
*/
static public function init() {
// 載入動態應用公共檔案和配置
load_ext_file(COMMON_PATH);
// 日誌目錄轉換為絕對路徑 預設情況下儲存到公共模組下面
C('LOG_PATH', realpath(LOG_PATH).'/Common/');
// 定義當前請求的系統常量
define('NOW_TIME', $_SERVER['REQUEST_TIME']);
define('REQUEST_METHOD',$_SERVER['REQUEST_METHOD']);
define('IS_GET', REQUEST_METHOD =='GET' ? true : false);
define('IS_POST', REQUEST_METHOD =='POST' ? true : false);
define('IS_PUT', REQUEST_METHOD =='PUT' ? true : false);
define('IS_DELETE', REQUEST_METHOD =='DELETE' ? true : false);
// URL排程
Dispatcher::dispatch();
if(C('REQUEST_VARS_FILTER')){
// 全域性安全過濾
array_walk_recursive($_GET, 'think_filter');
array_walk_recursive($_POST, 'think_filter');
array_walk_recursive($_REQUEST, 'think_filter');
}
// URL排程結束標籤
Hook::listen('url_dispatch');
define('IS_AJAX', ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') || !empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')])) ? true : false);
// TMPL_EXCEPTION_FILE 改為絕對地址
C('TMPL_EXCEPTION_FILE',realpath(C('TMPL_EXCEPTION_FILE')));
return ;
}
複製程式碼