config['view_path'] = root_path().'app'.DS.$modelname.DS.'view'.DS; $this->config['cache_path'] = root_path().'runtime'.DS.'temp'.DS; $template= substr($template,strpos($template,'@')+1); if(str_starts_with($template,'/')){ $template= substr($template,1); } }else{ $modelname= app('http')->getName(); $this->config['view_path'] = root_path().'app'.DS.$modelname.DS.'view'.DS; $this->config['cache_path'] = root_path().'runtime'.DS.$modelname.DS.'temp'.DS; } if ($vars) { $this->data = array_merge($this->data, $vars); } if ($this->isCache($this->config['cache_id'])) { // 读取渲染缓存 echo $this->cache->get($this->config['cache_id']); return; } $template = $this->parseTemplateFile($template); if ($template) { $cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($this->config['layout_on'] . $this->config['layout_name'] . $template) . '.' . ltrim($this->config['cache_suffix'], '.'); if (!$this->checkCache($cacheFile)) { // 缓存无效 重新模板编译 $content = file_get_contents($template); //处理vue包含标签 $preg='/\{include\s+vue=[\'"](.*)[\'"]\s+\/\}/'; $vuetemp=''; preg_replace_callback($preg,function ($match) use (&$vuetemp){ $vuetemp=$match[1]; },$content); if($vuetemp){ $this->fetch($vuetemp,$vars); return; }else{ $this->compiler($content, $cacheFile); } } // 页面缓存 ob_start(); ob_implicit_flush(false); // 读取编译存储 $this->storage->read($cacheFile, $this->data); // 获取并清空缓存 $content = ob_get_clean(); if (!empty($this->config['cache_id']) && $this->config['display_cache'] && null !== $this->cache) { // 缓存页面输出 $this->cache->set($this->config['cache_id'], $content, $this->config['cache_time']); } echo $content; } } /** * 编译模板文件内容 * @access private * @param string $content 模板内容 * @param string $cacheFile 缓存文件名 * @return void */ private function compiler(string &$content, string $cacheFile): void { // 判断是否启用布局 if ($this->config['layout_on']) { if (str_contains($content, '{__NOLAYOUT__}')) { // 可以单独定义不使用布局 $content = str_replace('{__NOLAYOUT__}', '', $content); } else { // 读取布局模板 $layoutFile = $this->parseTemplateFile($this->config['layout_name']); if ($layoutFile) { $layoutContent=file_get_contents($layoutFile); if($this->config['layout_name']=='layout'.DS.'vue'){ [$content, $jsfile, $cssfile] = $this->parseVue($content,$cacheFile); $layoutContent = str_replace('{__CSS__}', $cssfile, $layoutContent); $layoutContent = str_replace('{__JS__}', $jsfile, $layoutContent); } // 替换布局的主体内容 $content = str_replace($this->config['layout_item'], $content,$layoutContent); } } } else { $content = str_replace('{__NOLAYOUT__}', '', $content); } $this->compilerFileToPhp($content, $cacheFile); $this->includeFile = []; } private function parseVue(string $content,string $cacheFile): array { //删除注释 $content=preg_replace('//Uis','',$content); $content=trim($content); if(!str_starts_with($content,'