private function compiler(string &$content, string $cacheFile): void
{
// 判断是否启用布局
if ($this->config[‘layout_on‘]) {
if (false !== strpos($content, ‘{__NOLAYOUT__}‘)) {
// 可以单独定义不使用布局
$content = str_replace(‘{__NOLAYOUT__}‘, ‘‘, $content);
} else {
// 读取布局模板
$layoutFile = $this->parseTemplateFile($this->config[‘layout_name‘]);
if ($layoutFile) {
// 替换布局的主体内容
$content = str_replace($this->config[‘layout_item‘], $content, file_get_contents($layoutFile));
}
}
} else {
$content = str_replace(‘{__NOLAYOUT__}‘, ‘‘, $content);
}
//这里是替换模板内的tp语法例如 {$data} = echo $data
//{if (1==1))} == <?php if (1==1): ?>
//{/endif} == <?php endif;?>
$this->parse($content);
if ($this->config[‘strip_space‘]) {
/* 去除html空格与换行 */
$find = [‘~>\s+<~‘, ‘~>(\s+\n|\r)~‘];
$replace = [‘><‘, ‘>‘];
$content = preg_replace($find, $replace, $content);
}
// 优化生成的php代码
$content = preg_replace(‘/\?>\s*<\?php\s(?!echo\b|\bend)/s‘, ‘‘, $content);
// 模板过滤输出
$replace = $this->config[‘tpl_replace_string‘];
$content = str_replace(array_keys($replace), array_values($replace), $content);
// 添加安全代码及模板引用记录
$content = ‘<?php /*‘ . serialize($this->includeFile) . ‘*/ ?>‘ . "\n" . $content;
//写入缓存
$this->storage->write($cacheFile, $content);
$this->includeFile = [];
}