public static function include_file($_filename, $_vars = null) { self::$current_include_file = 'saemc://' . $_SERVER['HTTP_APPVERSION'] . '/' . $_filename; $_content = isset(self::$contents[$_filename]) ? self::$contents[$_filename] : self::getValue($_filename, 'content'); if (!is_null($_vars)) { extract($_vars, EXTR_OVERWRITE); } if (!$_content) { exit('<br /><b>SAE_Parse_error</b>: failed to open stream: No such file ' . self::$current_include_file); } if (@eval(' ?>' . $_content) === false) { self::error(); } self::$current_include_file = null; unset(self::$contents[$_filename]); //释放内存 }
protected function checkCache($tmplTemplateFile) { if (!C('TMPL_CACHE_ON')) { // 优先对配置设定检测 return false; } //[sae] 不加模版目录,简化模版名称 $tmplCacheFile = md5($tmplTemplateFile) . C('TMPL_CACHFILE_SUFFIX'); if (!SaeMC::file_exists($tmplCacheFile)) { return false; //}elseif (filemtime($tmplTemplateFile) > filemtime($tmplCacheFile)) { } elseif (filemtime($tmplTemplateFile) > SaeMC::filemtime($tmplCacheFile)) { // 模板文件如果有更新则缓存需要更新 return false; } elseif (C('TMPL_CACHE_TIME') != 0 && time() > SaeMC::filemtime($tmplCacheFile) + C('TMPL_CACHE_TIME')) { // 缓存是否在有效期 return false; } // 开启布局模板 if (C('LAYOUT_ON')) { $layoutFile = THEME_PATH . C('LAYOUT_NAME') . C('TMPL_TEMPLATE_SUFFIX'); if (filemtime($layoutFile) > SaeMC::filemtime($tmplCacheFile)) { return false; } } // 缓存有效 return true; }
/** +---------------------------------------------------------- * 加载主模板并缓存 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $tmplTemplateFile 模板文件 +---------------------------------------------------------- * @return string +---------------------------------------------------------- * @throws ThinkExecption +---------------------------------------------------------- */ public function loadTemplate($tmplTemplateFile) { $this->templateFile = $tmplTemplateFile; //[sae] 根据模版文件名定位缓存文件 $tmplCacheFile = md5($tmplTemplateFile) . $this->config['cache_suffix']; // 读取模板文件内容 $tmplContent = file_get_contents($tmplTemplateFile); // 判断是否启用布局 if (C('LAYOUT_ON')) { if (false !== strpos($tmplContent, '{__NOLAYOUT__}')) { // 可以单独定义不使用布局 $tmplContent = str_replace('{__NOLAYOUT__}', '', $tmplContent); } else { // 替换布局的主体内容 $layoutFile = THEME_PATH . C('LAYOUT_NAME') . $this->config['template_suffix']; $tmplContent = str_replace($this->config['layout_item'], $tmplContent, file_get_contents($layoutFile)); } } //编译模板内容 $tmplContent = $this->compiler($tmplContent); //[sae]去掉检测分组目录 //[sae]重写Cache文件 SaeMC::set($tmplCacheFile, trim($tmplContent)); return $tmplCacheFile; }
defined('SAE_RUNTIME') or define('SAE_RUNTIME', false); require SAE_PATH . 'Lib/Core/SaeMC.class.php'; //记录开始运行时间 $GLOBALS['_beginTime'] = microtime(TRUE); // 记录内存初始使用 define('MEMORY_LIMIT_ON', function_exists('memory_get_usage')); if (MEMORY_LIMIT_ON) { $GLOBALS['_startUseMems'] = memory_get_usage(); } defined('APP_PATH') or define('APP_PATH', dirname($_SERVER['SCRIPT_FILENAME']) . '/'); //[sae] 判断是否手动建立项目目录 if (!is_dir(APP_PATH . '/Lib/')) { header('Content-Type:text/html; charset=utf-8'); exit('<div style=\'font-weight:bold;float:left;width:430px;text-align:center;border:1px solid silver;background:#E8EFFF;padding:8px;color:red;font-size:14px;font-family:Tahoma\'>sae环境下请手动生成项目目录~</div>'); } defined('RUNTIME_PATH') or define('RUNTIME_PATH', APP_PATH . 'Runtime/'); defined('APP_DEBUG') or define('APP_DEBUG', false); // 是否调试模式 $runtime = defined('MODE_NAME') ? '~' . strtolower(MODE_NAME) . '_sae_runtime.php' : '~sae_runtime.php'; defined('RUNTIME_FILE') or define('RUNTIME_FILE', RUNTIME_PATH . $runtime); //[sae] 载入核心编译缓存 if (!APP_DEBUG && SaeMC::file_exists(RUNTIME_FILE)) { // 部署模式直接载入allinone缓存 SaeMC::include_file(RUNTIME_FILE); } else { // ThinkPHP系统目录定义 defined('THINK_PATH') or define('THINK_PATH', dirname(dirname(dirname(__FILE__))) . '/'); //[sae] 加载运行时文件 require SAE_PATH . 'Common/runtime.php'; } }
function build_runtime_cache($append = '') { // 生成编译文件 $defs = get_defined_constants(TRUE); $content = '$GLOBALS[\'_beginTime\'] = microtime(TRUE);'; //[sae]编译SaeMC核心 $content .= compile(SAE_PATH . 'Lib/Core/SaeMC.class.php'); if (defined('RUNTIME_DEF_FILE')) { //[sae] 编译后的常量文件外部引入 SaeMC::set(RUNTIME_DEF_FILE, '<?php ' . array_define($defs['user'])); $content .= 'SaeMC::include_file(\'' . RUNTIME_DEF_FILE . '\');'; } else { $content .= array_define($defs['user']); } $content .= 'set_include_path(get_include_path() . PATH_SEPARATOR . VENDOR_PATH);'; //[sae] 读取核心编译文件列表 $list = array(SAE_PATH . 'Common/common.php', SAE_PATH . 'Lib/Core/Think.class.php', CORE_PATH . 'Core/ThinkException.class.php', CORE_PATH . 'Core/Behavior.class.php'); foreach ($list as $file) { $content .= compile($file); } // 系统行为扩展文件统一编译 if (C('APP_TAGS_ON')) { $content .= build_tags_cache(); } //[sae] 编译SAE的alias $alias = (include SAE_PATH . 'Conf/alias.php'); $content .= 'alias_import(' . var_export($alias, true) . ');'; // 编译框架默认语言包和配置参数 $content .= $append . "\nL(" . var_export(L(), true) . ");C(" . var_export(C(), true) . ');G(\'loadTime\');Think::Start();'; //[sae] 生成编译缓存文件 SaeMC::set(RUNTIME_FILE, strip_whitespace('<?php ' . $content)); }
function build_runtime_cache($append = '') { // 生成编译文件 $defs = get_defined_constants(TRUE); $content = '$GLOBALS[\'_beginTime\'] = microtime(TRUE);'; //[sae]编译SaeMC核心 $content .= compile(SAE_PATH . 'Lib/Core/SaeMC.class.php'); $defs['user']['APP_DEBUG'] = false; //[sae] 关闭调试 if (defined('RUNTIME_DEF_FILE')) { //[sae] 编译后的常量文件外部引入 SaeMC::set(RUNTIME_DEF_FILE, '<?php ' . array_define($defs['user'])); $content .= 'SaeMC::include_file(\'' . RUNTIME_DEF_FILE . '\');'; } else { $content .= array_define($defs['user']); } $content .= 'set_include_path(get_include_path() . PATH_SEPARATOR . VENDOR_PATH);'; //[sae] 读取核心编译文件列表 $list = array(SAE_PATH . 'Common/common.php', SAE_PATH . 'Common/sae_common.php', SAE_PATH . 'Lib/Core/Think.class.php', CORE_PATH . 'Core/ThinkException.class.php', CORE_PATH . 'Core/Behavior.class.php'); foreach ($list as $file) { $content .= compile($file); } // 系统行为扩展文件统一编译 $content .= build_tags_cache(); //[sae] 编译SAE的alias //$alias = include SAE_PATH.'Conf/alias.php'; //$content .= 'alias_import('.var_export($alias,true).');'; // 编译框架默认语言包和配置参数 // [sae_runtime] 对配置中的SAE常量进行处理。 配置项的值如果是 ~func() 的字符串 则会 编译为 执行func函数。主要是为了处理 sae_storage_root 函数在SAE_RUNTIME模式下的使用 $content .= $append . "\nL(" . var_export(L(), true) . ");C(" . preg_replace(array('/\'SAE_(.*?)\'/e', '/\'~([a-zA-Z_][a-zA-Z0-9_]*)\\((.*?)\\)\'/'), array('parse_sae_define("\\1")', '\\1(\\2)'), var_export(C(), true)) . ');G(\'loadTime\');Think::Start();'; //[sae] 生成编译缓存文件 SaeMC::set(RUNTIME_FILE, strip_whitespace('<?php ' . str_replace("defined('THINK_PATH') or exit();", ' ', $content))); }
/** * 检查缓存内容是否有效 * 如果无效则需要重新编译 * @access public * @param string $tmplContent 模板内容 * @return boolen */ protected function checkContentCache($tmplContent, $prefix = '') { return SaeMC::file_exists(C('CACHE_PATH') . $prefix . md5($tmplContent) . C('TMPL_CACHFILE_SUFFIX')); }