Template.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think;
  13. use Exception;
  14. use Psr\SimpleCache\CacheInterface;
  15. /**
  16. * ThinkPHP分离出来的模板引擎
  17. * 支持XML标签和普通标签的模板解析
  18. * 编译型模板引擎 支持动态缓存
  19. */
  20. class Template
  21. {
  22. /**
  23. * 模板变量
  24. * @var array
  25. */
  26. protected $data = [];
  27. /**
  28. * 模板配置参数
  29. * @var array
  30. */
  31. protected $config = [
  32. 'view_path' => '', // 模板路径
  33. 'view_suffix' => 'html', // 默认模板文件后缀
  34. 'view_depr' => DIRECTORY_SEPARATOR,
  35. 'cache_path' => '',
  36. 'cache_suffix' => 'php', // 默认模板缓存后缀
  37. 'tpl_deny_func_list' => 'echo,exit', // 模板引擎禁用函数
  38. 'tpl_deny_php' => false, // 默认模板引擎是否禁用PHP原生代码
  39. 'tpl_begin' => '{', // 模板引擎普通标签开始标记
  40. 'tpl_end' => '}', // 模板引擎普通标签结束标记
  41. 'strip_space' => false, // 是否去除模板文件里面的html空格与换行
  42. 'tpl_cache' => true, // 是否开启模板编译缓存,设为false则每次都会重新编译
  43. 'compile_type' => 'file', // 模板编译类型
  44. 'cache_prefix' => '', // 模板缓存前缀标识,可以动态改变
  45. 'cache_time' => 0, // 模板缓存有效期 0 为永久,(以数字为值,单位:秒)
  46. 'layout_on' => false, // 布局模板开关
  47. 'layout_name' => 'layout', // 布局模板入口文件
  48. 'layout_item' => '{__CONTENT__}', // 布局模板的内容替换标识
  49. 'taglib_begin' => '{', // 标签库标签开始标记
  50. 'taglib_end' => '}', // 标签库标签结束标记
  51. 'taglib_load' => true, // 是否使用内置标签库之外的其它标签库,默认自动检测
  52. 'taglib_build_in' => 'cx', // 内置标签库名称(标签使用不必指定标签库名称),以逗号分隔 注意解析顺序
  53. 'taglib_pre_load' => '', // 需要额外加载的标签库(须指定标签库名称),多个以逗号分隔
  54. 'display_cache' => false, // 模板渲染缓存
  55. 'cache_id' => '', // 模板缓存ID
  56. 'tpl_replace_string' => [],
  57. 'tpl_var_identify' => 'array', // .语法变量识别,array|object|'', 为空时自动识别
  58. 'default_filter' => 'htmlentities', // 默认过滤方法 用于普通标签输出
  59. ];
  60. /**
  61. * 保留内容信息
  62. * @var array
  63. */
  64. private $literal = [];
  65. /**
  66. * 扩展解析规则
  67. * @var array
  68. */
  69. private $extend = [];
  70. /**
  71. * 模板包含信息
  72. * @var array
  73. */
  74. private $includeFile = [];
  75. /**
  76. * 模板存储对象
  77. * @var object
  78. */
  79. protected $storage;
  80. /**
  81. * 查询缓存对象
  82. * @var CacheInterface
  83. */
  84. protected $cache;
  85. /**
  86. * 架构函数
  87. * @access public
  88. * @param array $config
  89. */
  90. public function __construct(array $config = [])
  91. {
  92. $this->config = array_merge($this->config, $config);
  93. $this->config['taglib_begin_origin'] = $this->config['taglib_begin'];
  94. $this->config['taglib_end_origin'] = $this->config['taglib_end'];
  95. $this->config['taglib_begin'] = preg_quote($this->config['taglib_begin'], '/');
  96. $this->config['taglib_end'] = preg_quote($this->config['taglib_end'], '/');
  97. $this->config['tpl_begin'] = preg_quote($this->config['tpl_begin'], '/');
  98. $this->config['tpl_end'] = preg_quote($this->config['tpl_end'], '/');
  99. // 初始化模板编译存储器
  100. $type = $this->config['compile_type'] ? $this->config['compile_type'] : 'File';
  101. $class = false !== strpos($type, '\\') ? $type : '\\think\\template\\driver\\' . ucwords($type);
  102. $this->storage = new $class();
  103. }
  104. /**
  105. * 模板变量赋值
  106. * @access public
  107. * @param array $vars 模板变量
  108. * @return $this
  109. */
  110. public function assign(array $vars = [])
  111. {
  112. $this->data = array_merge($this->data, $vars);
  113. return $this;
  114. }
  115. /**
  116. * 模板引擎参数赋值
  117. * @access public
  118. * @param string $name
  119. * @param mixed $value
  120. */
  121. public function __set($name, $value)
  122. {
  123. $this->config[$name] = $value;
  124. }
  125. /**
  126. * 设置缓存对象
  127. * @access public
  128. * @param CacheInterface $cache 缓存对象
  129. * @return void
  130. */
  131. public function setCache(CacheInterface $cache): void
  132. {
  133. $this->cache = $cache;
  134. }
  135. /**
  136. * 模板引擎配置
  137. * @access public
  138. * @param array $config
  139. * @return $this
  140. */
  141. public function config(array $config)
  142. {
  143. $this->config = array_merge($this->config, $config);
  144. return $this;
  145. }
  146. /**
  147. * 获取模板引擎配置项
  148. * @access public
  149. * @param string $name
  150. * @return mixed
  151. */
  152. public function getConfig(string $name)
  153. {
  154. return $this->config[$name] ?? null;
  155. }
  156. /**
  157. * 模板变量获取
  158. * @access public
  159. * @param string $name 变量名
  160. * @return mixed
  161. */
  162. public function get(string $name = '')
  163. {
  164. if ('' == $name) {
  165. return $this->data;
  166. }
  167. $data = $this->data;
  168. foreach (explode('.', $name) as $key => $val) {
  169. if (isset($data[$val])) {
  170. $data = $data[$val];
  171. } else {
  172. $data = null;
  173. break;
  174. }
  175. }
  176. return $data;
  177. }
  178. /**
  179. * 扩展模板解析规则
  180. * @access public
  181. * @param string $rule 解析规则
  182. * @param callable $callback 解析规则
  183. * @return void
  184. */
  185. public function extend(string $rule, callable $callback = null): void
  186. {
  187. $this->extend[$rule] = $callback;
  188. }
  189. /**
  190. * 渲染模板文件
  191. * @access public
  192. * @param string $template 模板文件
  193. * @param array $vars 模板变量
  194. * @return void
  195. */
  196. public function fetch(string $template, array $vars = []): void
  197. {
  198. if ($vars) {
  199. $this->data = array_merge($this->data, $vars);
  200. }
  201. if (!empty($this->config['cache_id']) && $this->config['display_cache'] && $this->cache) {
  202. // 读取渲染缓存
  203. if ($this->cache->has($this->config['cache_id'])) {
  204. echo $this->cache->get($this->config['cache_id']);
  205. return;
  206. }
  207. }
  208. $template = $this->parseTemplateFile($template);
  209. if ($template) {
  210. $cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($this->config['layout_on'] . $this->config['layout_name'] . $template) . '.' . ltrim($this->config['cache_suffix'], '.');
  211. if (!$this->checkCache($cacheFile)) {
  212. // 缓存无效 重新模板编译
  213. $content = file_get_contents($template);
  214. $this->compiler($content, $cacheFile);
  215. }
  216. // 页面缓存
  217. ob_start();
  218. if (PHP_VERSION > 8.0) {
  219. ob_implicit_flush(false);
  220. } else {
  221. ob_implicit_flush(0);
  222. }
  223. // 读取编译存储
  224. $this->storage->read($cacheFile, $this->data);
  225. // 获取并清空缓存
  226. $content = ob_get_clean();
  227. if (!empty($this->config['cache_id']) && $this->config['display_cache'] && $this->cache) {
  228. // 缓存页面输出
  229. $this->cache->set($this->config['cache_id'], $content, $this->config['cache_time']);
  230. }
  231. echo $content;
  232. }
  233. }
  234. /**
  235. * 检查编译缓存是否存在
  236. * @access public
  237. * @param string $cacheId 缓存的id
  238. * @return boolean
  239. */
  240. public function isCache(string $cacheId): bool
  241. {
  242. if ($cacheId && $this->cache && $this->config['display_cache']) {
  243. // 缓存页面输出
  244. return $this->cache->has($cacheId);
  245. }
  246. return false;
  247. }
  248. /**
  249. * 渲染模板内容
  250. * @access public
  251. * @param string $content 模板内容
  252. * @param array $vars 模板变量
  253. * @return void
  254. */
  255. public function display(string $content, array $vars = []): void
  256. {
  257. if ($vars) {
  258. $this->data = array_merge($this->data, $vars);
  259. }
  260. $cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($content) . '.' . ltrim($this->config['cache_suffix'], '.');
  261. if (!$this->checkCache($cacheFile)) {
  262. // 缓存无效 模板编译
  263. $this->compiler($content, $cacheFile);
  264. }
  265. // 读取编译存储
  266. $this->storage->read($cacheFile, $this->data);
  267. }
  268. /**
  269. * 设置布局
  270. * @access public
  271. * @param mixed $name 布局模板名称 false 则关闭布局
  272. * @param string $replace 布局模板内容替换标识
  273. * @return $this
  274. */
  275. public function layout($name, string $replace = '')
  276. {
  277. if (false === $name) {
  278. // 关闭布局
  279. $this->config['layout_on'] = false;
  280. } else {
  281. // 开启布局
  282. $this->config['layout_on'] = true;
  283. // 名称必须为字符串
  284. if (is_string($name)) {
  285. $this->config['layout_name'] = $name;
  286. }
  287. if (!empty($replace)) {
  288. $this->config['layout_item'] = $replace;
  289. }
  290. }
  291. return $this;
  292. }
  293. /**
  294. * 检查编译缓存是否有效
  295. * 如果无效则需要重新编译
  296. * @access private
  297. * @param string $cacheFile 缓存文件名
  298. * @return bool
  299. */
  300. private function checkCache(string $cacheFile): bool
  301. {
  302. if (!$this->config['tpl_cache'] || !is_file($cacheFile) || !$handle = @fopen($cacheFile, "r")) {
  303. return false;
  304. }
  305. // 读取第一行
  306. $line = fgets($handle);
  307. if (false === $line) {
  308. return false;
  309. }
  310. preg_match('/\/\*(.+?)\*\//', $line, $matches);
  311. if (!isset($matches[1])) {
  312. return false;
  313. }
  314. $includeFile = unserialize($matches[1]);
  315. if (!is_array($includeFile)) {
  316. return false;
  317. }
  318. // 检查模板文件是否有更新
  319. foreach ($includeFile as $path => $time) {
  320. if (is_file($path) && filemtime($path) > $time) {
  321. // 模板文件如果有更新则缓存需要更新
  322. return false;
  323. }
  324. }
  325. // 检查编译存储是否有效
  326. return $this->storage->check($cacheFile, $this->config['cache_time']);
  327. }
  328. /**
  329. * 编译模板文件内容
  330. * @access private
  331. * @param string $content 模板内容
  332. * @param string $cacheFile 缓存文件名
  333. * @return void
  334. */
  335. private function compiler(string &$content, string $cacheFile): void
  336. {
  337. // 判断是否启用布局
  338. if ($this->config['layout_on']) {
  339. if (false !== strpos($content, '{__NOLAYOUT__}')) {
  340. // 可以单独定义不使用布局
  341. $content = str_replace('{__NOLAYOUT__}', '', $content);
  342. } else {
  343. // 读取布局模板
  344. $layoutFile = $this->parseTemplateFile($this->config['layout_name']);
  345. if ($layoutFile) {
  346. // 替换布局的主体内容
  347. $content = str_replace($this->config['layout_item'], $content, file_get_contents($layoutFile));
  348. }
  349. }
  350. } else {
  351. $content = str_replace('{__NOLAYOUT__}', '', $content);
  352. }
  353. // 模板解析
  354. $this->parse($content);
  355. if ($this->config['strip_space']) {
  356. /* 去除html空格与换行 */
  357. $find = ['~>\s+<~', '~>(\s+\n|\r)~'];
  358. $replace = ['><', '>'];
  359. $content = preg_replace($find, $replace, $content);
  360. }
  361. // 优化生成的php代码
  362. $content = preg_replace('/\?>\s*<\?php\s(?!echo\b|\bend)/s', '', $content);
  363. // 模板过滤输出
  364. $replace = $this->config['tpl_replace_string'];
  365. $content = str_replace(array_keys($replace), array_values($replace), $content);
  366. // 添加安全代码及模板引用记录
  367. $content = '<?php /*' . serialize($this->includeFile) . '*/ ?>' . "\n" . $content;
  368. // 编译存储
  369. $this->storage->write($cacheFile, $content);
  370. $this->includeFile = [];
  371. }
  372. /**
  373. * 模板解析入口
  374. * 支持普通标签和TagLib解析 支持自定义标签库
  375. * @access public
  376. * @param string $content 要解析的模板内容
  377. * @return void
  378. */
  379. public function parse(string &$content): void
  380. {
  381. // 内容为空不解析
  382. if (empty($content)) {
  383. return;
  384. }
  385. // 替换literal标签内容
  386. $this->parseLiteral($content);
  387. // 解析继承
  388. $this->parseExtend($content);
  389. // 解析布局
  390. $this->parseLayout($content);
  391. // 检查include语法
  392. $this->parseInclude($content);
  393. // 替换包含文件中literal标签内容
  394. $this->parseLiteral($content);
  395. // 检查PHP语法
  396. $this->parsePhp($content);
  397. // 获取需要引入的标签库列表
  398. // 标签库只需要定义一次,允许引入多个一次
  399. // 一般放在文件的最前面
  400. // 格式:<taglib name="html,mytag..." />
  401. // 当TAGLIB_LOAD配置为true时才会进行检测
  402. if ($this->config['taglib_load']) {
  403. $tagLibs = $this->getIncludeTagLib($content);
  404. if (!empty($tagLibs)) {
  405. // 对导入的TagLib进行解析
  406. foreach ($tagLibs as $tagLibName) {
  407. $this->parseTagLib($tagLibName, $content);
  408. }
  409. }
  410. }
  411. // 预先加载的标签库 无需在每个模板中使用taglib标签加载 但必须使用标签库XML前缀
  412. if ($this->config['taglib_pre_load']) {
  413. $tagLibs = explode(',', $this->config['taglib_pre_load']);
  414. foreach ($tagLibs as $tag) {
  415. $this->parseTagLib($tag, $content);
  416. }
  417. }
  418. // 内置标签库 无需使用taglib标签导入就可以使用 并且不需使用标签库XML前缀
  419. $tagLibs = explode(',', $this->config['taglib_build_in']);
  420. foreach ($tagLibs as $tag) {
  421. $this->parseTagLib($tag, $content, true);
  422. }
  423. // 解析普通模板标签 {$tagName}
  424. $this->parseTag($content);
  425. // 还原被替换的Literal标签
  426. $this->parseLiteral($content, true);
  427. }
  428. /**
  429. * 检查PHP语法
  430. * @access private
  431. * @param string $content 要解析的模板内容
  432. * @return void
  433. * @throws Exception
  434. */
  435. private function parsePhp(string &$content): void
  436. {
  437. // 短标签的情况要将<?标签用echo方式输出 否则无法正常输出xml标识
  438. $content = preg_replace('/(<\?(?!php|=|$))/i', '<?php echo \'\\1\'; ?>' . "\n", $content);
  439. // PHP语法检查
  440. if ($this->config['tpl_deny_php'] && false !== strpos($content, '<?php')) {
  441. throw new Exception('not allow php tag');
  442. }
  443. }
  444. /**
  445. * 解析模板中的布局标签
  446. * @access private
  447. * @param string $content 要解析的模板内容
  448. * @return void
  449. */
  450. private function parseLayout(string &$content): void
  451. {
  452. // 读取模板中的布局标签
  453. if (preg_match($this->getRegex('layout'), $content, $matches)) {
  454. // 替换Layout标签
  455. $content = str_replace($matches[0], '', $content);
  456. // 解析Layout标签
  457. $array = $this->parseAttr($matches[0]);
  458. if (!$this->config['layout_on'] || $this->config['layout_name'] != $array['name']) {
  459. // 读取布局模板
  460. $layoutFile = $this->parseTemplateFile($array['name']);
  461. if ($layoutFile) {
  462. $replace = isset($array['replace']) ? $array['replace'] : $this->config['layout_item'];
  463. // 替换布局的主体内容
  464. $content = str_replace($replace, $content, file_get_contents($layoutFile));
  465. }
  466. }
  467. } else {
  468. $content = str_replace('{__NOLAYOUT__}', '', $content);
  469. }
  470. }
  471. /**
  472. * 解析模板中的include标签
  473. * @access private
  474. * @param string $content 要解析的模板内容
  475. * @return void
  476. */
  477. private function parseInclude(string &$content): void
  478. {
  479. $regex = $this->getRegex('include');
  480. $func = function ($template) use (&$func, &$regex, &$content) {
  481. if (preg_match_all($regex, $template, $matches, PREG_SET_ORDER)) {
  482. foreach ($matches as $match) {
  483. $array = $this->parseAttr($match[0]);
  484. $file = $array['file'];
  485. unset($array['file']);
  486. // 分析模板文件名并读取内容
  487. $parseStr = $this->parseTemplateName($file);
  488. foreach ($array as $k => $v) {
  489. // 以$开头字符串转换成模板变量
  490. if (0 === strpos($v, '$')) {
  491. $v = $this->get(substr($v, 1));
  492. }
  493. $parseStr = str_replace('[' . $k . ']', $v, $parseStr);
  494. }
  495. $content = str_replace($match[0], $parseStr, $content);
  496. // 再次对包含文件进行模板分析
  497. $func($parseStr);
  498. }
  499. unset($matches);
  500. }
  501. };
  502. // 替换模板中的include标签
  503. $func($content);
  504. }
  505. /**
  506. * 解析模板中的extend标签
  507. * @access private
  508. * @param string $content 要解析的模板内容
  509. * @return void
  510. */
  511. private function parseExtend(string &$content): void
  512. {
  513. $regex = $this->getRegex('extend');
  514. $array = $blocks = $baseBlocks = [];
  515. $extend = '';
  516. $func = function ($template) use (&$func, &$regex, &$array, &$extend, &$blocks, &$baseBlocks) {
  517. if (preg_match($regex, $template, $matches)) {
  518. if (!isset($array[$matches['name']])) {
  519. $array[$matches['name']] = 1;
  520. // 读取继承模板
  521. $extend = $this->parseTemplateName($matches['name']);
  522. // 递归检查继承
  523. $func($extend);
  524. // 取得block标签内容
  525. $blocks = array_merge($blocks, $this->parseBlock($template));
  526. return;
  527. }
  528. } else {
  529. // 取得顶层模板block标签内容
  530. $baseBlocks = $this->parseBlock($template, true);
  531. if (empty($extend)) {
  532. // 无extend标签但有block标签的情况
  533. $extend = $template;
  534. }
  535. }
  536. };
  537. $func($content);
  538. if (!empty($extend)) {
  539. if ($baseBlocks) {
  540. $children = [];
  541. foreach ($baseBlocks as $name => $val) {
  542. $replace = $val['content'];
  543. if (!empty($children[$name])) {
  544. // 如果包含有子block标签
  545. foreach ($children[$name] as $key) {
  546. $replace = str_replace($baseBlocks[$key]['begin'] . $baseBlocks[$key]['content'] . $baseBlocks[$key]['end'], $blocks[$key]['content'], $replace);
  547. }
  548. }
  549. if (isset($blocks[$name])) {
  550. // 带有{__block__}表示与所继承模板的相应标签合并,而不是覆盖
  551. $replace = str_replace(['{__BLOCK__}', '{__block__}'], $replace, $blocks[$name]['content']);
  552. if (!empty($val['parent'])) {
  553. // 如果不是最顶层的block标签
  554. $parent = $val['parent'];
  555. if (isset($blocks[$parent])) {
  556. $blocks[$parent]['content'] = str_replace($blocks[$name]['begin'] . $blocks[$name]['content'] . $blocks[$name]['end'], $replace, $blocks[$parent]['content']);
  557. }
  558. $blocks[$name]['content'] = $replace;
  559. $children[$parent][] = $name;
  560. continue;
  561. }
  562. } elseif (!empty($val['parent'])) {
  563. // 如果子标签没有被继承则用原值
  564. $children[$val['parent']][] = $name;
  565. $blocks[$name] = $val;
  566. }
  567. if (!$val['parent']) {
  568. // 替换模板中的顶级block标签
  569. $extend = str_replace($val['begin'] . $val['content'] . $val['end'], $replace, $extend);
  570. }
  571. }
  572. }
  573. $content = $extend;
  574. unset($blocks, $baseBlocks);
  575. }
  576. }
  577. /**
  578. * 替换页面中的literal标签
  579. * @access private
  580. * @param string $content 模板内容
  581. * @param boolean $restore 是否为还原
  582. * @return void
  583. */
  584. private function parseLiteral(string &$content, bool $restore = false): void
  585. {
  586. $regex = $this->getRegex($restore ? 'restoreliteral' : 'literal');
  587. if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) {
  588. if (!$restore) {
  589. $count = count($this->literal);
  590. // 替换literal标签
  591. foreach ($matches as $match) {
  592. $this->literal[] = substr($match[0], strlen($match[1]), -strlen($match[2]));
  593. $content = str_replace($match[0], "<!--###literal{$count}###-->", $content);
  594. $count++;
  595. }
  596. } else {
  597. // 还原literal标签
  598. foreach ($matches as $match) {
  599. $content = str_replace($match[0], $this->literal[$match[1]], $content);
  600. }
  601. // 清空literal记录
  602. $this->literal = [];
  603. }
  604. unset($matches);
  605. }
  606. }
  607. /**
  608. * 获取模板中的block标签
  609. * @access private
  610. * @param string $content 模板内容
  611. * @param boolean $sort 是否排序
  612. * @return array
  613. */
  614. private function parseBlock(string &$content, bool $sort = false): array
  615. {
  616. $regex = $this->getRegex('block');
  617. $result = [];
  618. if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
  619. $right = $keys = [];
  620. foreach ($matches as $match) {
  621. if (empty($match['name'][0])) {
  622. if (count($right) > 0) {
  623. $tag = array_pop($right);
  624. $start = $tag['offset'] + strlen($tag['tag']);
  625. $length = $match[0][1] - $start;
  626. $result[$tag['name']] = [
  627. 'begin' => $tag['tag'],
  628. 'content' => substr($content, $start, $length),
  629. 'end' => $match[0][0],
  630. 'parent' => count($right) ? end($right)['name'] : '',
  631. ];
  632. $keys[$tag['name']] = $match[0][1];
  633. }
  634. } else {
  635. // 标签头压入栈
  636. $right[] = [
  637. 'name' => $match[2][0],
  638. 'offset' => $match[0][1],
  639. 'tag' => $match[0][0],
  640. ];
  641. }
  642. }
  643. unset($right, $matches);
  644. if ($sort) {
  645. // 按block标签结束符在模板中的位置排序
  646. array_multisort($keys, $result);
  647. }
  648. }
  649. return $result;
  650. }
  651. /**
  652. * 搜索模板页面中包含的TagLib库
  653. * 并返回列表
  654. * @access private
  655. * @param string $content 模板内容
  656. * @return array|null
  657. */
  658. private function getIncludeTagLib(string &$content)
  659. {
  660. // 搜索是否有TagLib标签
  661. if (preg_match($this->getRegex('taglib'), $content, $matches)) {
  662. // 替换TagLib标签
  663. $content = str_replace($matches[0], '', $content);
  664. return explode(',', $matches['name']);
  665. }
  666. }
  667. /**
  668. * TagLib库解析
  669. * @access public
  670. * @param string $tagLib 要解析的标签库
  671. * @param string $content 要解析的模板内容
  672. * @param boolean $hide 是否隐藏标签库前缀
  673. * @return void
  674. */
  675. public function parseTagLib(string $tagLib, string &$content, bool $hide = false): void
  676. {
  677. if (false !== strpos($tagLib, '\\')) {
  678. // 支持指定标签库的命名空间
  679. $className = $tagLib;
  680. $tagLib = substr($tagLib, strrpos($tagLib, '\\') + 1);
  681. } else {
  682. $className = '\\think\\template\\taglib\\' . ucwords($tagLib);
  683. }
  684. $tLib = new $className($this);
  685. $tLib->parseTag($content, $hide ? '' : $tagLib);
  686. }
  687. /**
  688. * 分析标签属性
  689. * @access public
  690. * @param string $str 属性字符串
  691. * @param string $name 不为空时返回指定的属性名
  692. * @return array
  693. */
  694. public function parseAttr(string $str, string $name = null): array
  695. {
  696. $regex = '/\s+(?>(?P<name>[\w-]+)\s*)=(?>\s*)([\"\'])(?P<value>(?:(?!\\2).)*)\\2/is';
  697. $array = [];
  698. if (preg_match_all($regex, $str, $matches, PREG_SET_ORDER)) {
  699. foreach ($matches as $match) {
  700. $array[$match['name']] = $match['value'];
  701. }
  702. unset($matches);
  703. }
  704. if (!empty($name) && isset($array[$name])) {
  705. return $array[$name];
  706. }
  707. return $array;
  708. }
  709. /**
  710. * 模板标签解析
  711. * 格式: {TagName:args [|content] }
  712. * @access private
  713. * @param string $content 要解析的模板内容
  714. * @return void
  715. */
  716. private function parseTag(string &$content): void
  717. {
  718. $regex = $this->getRegex('tag');
  719. if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) {
  720. foreach ($matches as $match) {
  721. $str = stripslashes($match[1]);
  722. $flag = substr($str, 0, 1);
  723. switch ($flag) {
  724. case '$':
  725. // 解析模板变量 格式 {$varName}
  726. // 是否带有?号
  727. if (false !== $pos = strpos($str, '?')) {
  728. $array = preg_split('/([!=]={1,2}|(?<!-)[><]={0,1})/', substr($str, 0, $pos), 2, PREG_SPLIT_DELIM_CAPTURE);
  729. $name = $array[0];
  730. $this->parseVar($name);
  731. //$this->parseVarFunction($name);
  732. $str = trim(substr($str, $pos + 1));
  733. $this->parseVar($str);
  734. $first = substr($str, 0, 1);
  735. if (strpos($name, ')')) {
  736. // $name为对象或是自动识别,或者含有函数
  737. if (isset($array[1])) {
  738. $this->parseVar($array[2]);
  739. $name .= $array[1] . $array[2];
  740. }
  741. switch ($first) {
  742. case '?':
  743. $this->parseVarFunction($name);
  744. $str = '<?php echo (' . $name . ') ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
  745. break;
  746. case '=':
  747. $str = '<?php if(' . $name . ') echo ' . substr($str, 1) . '; ?>';
  748. break;
  749. default:
  750. $str = '<?php echo ' . $name . '?' . $str . '; ?>';
  751. }
  752. } else {
  753. if (isset($array[1])) {
  754. $express = true;
  755. $this->parseVar($array[2]);
  756. $express = $name . $array[1] . $array[2];
  757. } else {
  758. $express = false;
  759. }
  760. if (in_array($first, ['?', '=', ':'])) {
  761. $str = trim(substr($str, 1));
  762. if ('$' == substr($str, 0, 1)) {
  763. $str = $this->parseVarFunction($str);
  764. }
  765. }
  766. // $name为数组
  767. switch ($first) {
  768. case '?':
  769. // {$varname??'xxx'} $varname有定义则输出$varname,否则输出xxx
  770. $str = '<?php echo ' . ($express ?: 'isset(' . $name . ')') . ' ? ' . $this->parseVarFunction($name) . ' : ' . $str . '; ?>';
  771. break;
  772. case '=':
  773. // {$varname?='xxx'} $varname为真时才输出xxx
  774. $str = '<?php if(' . ($express ?: '!empty(' . $name . ')') . ') echo ' . $str . '; ?>';
  775. break;
  776. case ':':
  777. // {$varname?:'xxx'} $varname为真时输出$varname,否则输出xxx
  778. $str = '<?php echo ' . ($express ?: '!empty(' . $name . ')') . ' ? ' . $this->parseVarFunction($name) . ' : ' . $str . '; ?>';
  779. break;
  780. default:
  781. if (strpos($str, ':')) {
  782. // {$varname ? 'a' : 'b'} $varname为真时输出a,否则输出b
  783. $array = explode(':', $str, 2);
  784. $array[0] = '$' == substr(trim($array[0]), 0, 1) ? $this->parseVarFunction($array[0]) : $array[0];
  785. $array[1] = '$' == substr(trim($array[1]), 0, 1) ? $this->parseVarFunction($array[1]) : $array[1];
  786. $str = implode(' : ', $array);
  787. }
  788. $str = '<?php echo ' . ($express ?: '!empty(' . $name . ')') . ' ? ' . $str . '; ?>';
  789. }
  790. }
  791. } else {
  792. $this->parseVar($str);
  793. $this->parseVarFunction($str);
  794. $str = '<?php echo ' . $str . '; ?>';
  795. }
  796. break;
  797. case ':':
  798. // 输出某个函数的结果
  799. $str = substr($str, 1);
  800. $this->parseVar($str);
  801. $str = '<?php echo ' . $str . '; ?>';
  802. break;
  803. case '~':
  804. // 执行某个函数
  805. $str = substr($str, 1);
  806. $this->parseVar($str);
  807. $str = '<?php ' . $str . '; ?>';
  808. break;
  809. case '-':
  810. case '+':
  811. // 输出计算
  812. $this->parseVar($str);
  813. $str = '<?php echo ' . $str . '; ?>';
  814. break;
  815. case '/':
  816. // 注释标签
  817. $flag2 = substr($str, 1, 1);
  818. if ('/' == $flag2 || ('*' == $flag2 && substr(rtrim($str), -2) == '*/')) {
  819. $str = '';
  820. }
  821. break;
  822. default:
  823. // 未识别的标签直接返回
  824. $str = $this->config['tpl_begin'] . $str . $this->config['tpl_end'];
  825. break;
  826. }
  827. $content = str_replace($match[0], $str, $content);
  828. }
  829. unset($matches);
  830. }
  831. }
  832. /**
  833. * 模板变量解析,支持使用函数
  834. * 格式: {$varname|function1|function2=arg1,arg2}
  835. * @access public
  836. * @param string $varStr 变量数据
  837. * @return void
  838. */
  839. public function parseVar(string &$varStr): void
  840. {
  841. $varStr = trim($varStr);
  842. if (preg_match_all('/\$[a-zA-Z_](?>\w*)(?:[:\.][0-9a-zA-Z_](?>\w*))+/', $varStr, $matches, PREG_OFFSET_CAPTURE)) {
  843. static $_varParseList = [];
  844. while ($matches[0]) {
  845. $match = array_pop($matches[0]);
  846. //如果已经解析过该变量字串,则直接返回变量值
  847. if (isset($_varParseList[$match[0]])) {
  848. $parseStr = $_varParseList[$match[0]];
  849. } else {
  850. if (strpos($match[0], '.')) {
  851. $vars = explode('.', $match[0]);
  852. $first = array_shift($vars);
  853. if (isset($this->extend[$first])) {
  854. $callback = $this->extend[$first];
  855. $parseStr = $callback($vars);
  856. } elseif ('$Request' == $first) {
  857. // 输出请求变量
  858. $parseStr = $this->parseRequestVar($vars);
  859. } elseif ('$Think' == $first) {
  860. // 所有以Think.打头的以特殊变量对待 无需模板赋值就可以输出
  861. $parseStr = $this->parseThinkVar($vars);
  862. } else {
  863. switch ($this->config['tpl_var_identify']) {
  864. case 'array': // 识别为数组
  865. $parseStr = $first . '[\'' . implode('\'][\'', $vars) . '\']';
  866. break;
  867. case 'obj': // 识别为对象
  868. $parseStr = $first . '->' . implode('->', $vars);
  869. break;
  870. default: // 自动判断数组或对象
  871. $parseStr = '(is_array(' . $first . ')?' . $first . '[\'' . implode('\'][\'', $vars) . '\']:' . $first . '->' . implode('->', $vars) . ')';
  872. }
  873. }
  874. } else {
  875. $parseStr = str_replace(':', '->', $match[0]);
  876. }
  877. $_varParseList[$match[0]] = $parseStr;
  878. }
  879. $varStr = substr_replace($varStr, $parseStr, $match[1], strlen($match[0]));
  880. }
  881. unset($matches);
  882. }
  883. }
  884. /**
  885. * 对模板中使用了函数的变量进行解析
  886. * 格式 {$varname|function1|function2=arg1,arg2}
  887. * @access public
  888. * @param string $varStr 变量字符串
  889. * @param bool $autoescape 自动转义
  890. * @return string
  891. */
  892. public function parseVarFunction(string &$varStr, bool $autoescape = true): string
  893. {
  894. if (!$autoescape && false === strpos($varStr, '|')) {
  895. return $varStr;
  896. } elseif ($autoescape && !preg_match('/\|(\s)?raw(\||\s)?/i', $varStr)) {
  897. $varStr .= '|' . $this->config['default_filter'];
  898. }
  899. static $_varFunctionList = [];
  900. $_key = md5($varStr);
  901. //如果已经解析过该变量字串,则直接返回变量值
  902. if (isset($_varFunctionList[$_key])) {
  903. $varStr = $_varFunctionList[$_key];
  904. } else {
  905. $varArray = explode('|', $varStr);
  906. // 取得变量名称
  907. $name = trim(array_shift($varArray));
  908. // 对变量使用函数
  909. $length = count($varArray);
  910. // 取得模板禁止使用函数列表
  911. $template_deny_funs = explode(',', $this->config['tpl_deny_func_list']);
  912. for ($i = 0; $i < $length; $i++) {
  913. $args = explode('=', $varArray[$i], 2);
  914. // 模板函数过滤
  915. $fun = trim($args[0]);
  916. if (in_array($fun, $template_deny_funs)) {
  917. continue;
  918. }
  919. switch (strtolower($fun)) {
  920. case 'raw':
  921. break;
  922. case 'date':
  923. $name = 'date(' . $args[1] . ',!is_numeric(' . $name . ')? strtotime(' . $name . ') : ' . $name . ')';
  924. break;
  925. case 'first':
  926. $name = 'current(' . $name . ')';
  927. break;
  928. case 'last':
  929. $name = 'end(' . $name . ')';
  930. break;
  931. case 'upper':
  932. $name = 'strtoupper(' . $name . ')';
  933. break;
  934. case 'lower':
  935. $name = 'strtolower(' . $name . ')';
  936. break;
  937. case 'format':
  938. $name = 'sprintf(' . $args[1] . ',' . $name . ')';
  939. break;
  940. case 'default': // 特殊模板函数
  941. if (false === strpos($name, '(')) {
  942. $name = '(isset(' . $name . ') && (' . $name . ' !== \'\')?' . $name . ':' . $args[1] . ')';
  943. } else {
  944. $name = '(' . $name . ' ?: ' . $args[1] . ')';
  945. }
  946. break;
  947. default: // 通用模板函数
  948. if (isset($args[1])) {
  949. if (strstr($args[1], '###')) {
  950. $args[1] = str_replace('###', $name, $args[1]);
  951. $name = "$fun($args[1])";
  952. } else {
  953. $name = "$fun($name,$args[1])";
  954. }
  955. } else {
  956. if (!empty($args[0])) {
  957. $name = "$fun($name)";
  958. }
  959. }
  960. }
  961. }
  962. $_varFunctionList[$_key] = $name;
  963. $varStr = $name;
  964. }
  965. return $varStr;
  966. }
  967. /**
  968. * 请求变量解析
  969. * 格式 以 $Request. 打头的变量属于请求变量
  970. * @access public
  971. * @param array $vars 变量数组
  972. * @return string
  973. */
  974. public function parseRequestVar(array $vars): string
  975. {
  976. $type = strtoupper(trim(array_shift($vars)));
  977. $param = implode('.', $vars);
  978. switch ($type) {
  979. case 'SERVER':
  980. $parseStr = '$_SERVER[\'' . $param . '\']';
  981. break;
  982. case 'GET':
  983. $parseStr = '$_GET[\'' . $param . '\']';
  984. break;
  985. case 'POST':
  986. $parseStr = '$_POST[\'' . $param . '\']';
  987. break;
  988. case 'COOKIE':
  989. $parseStr = '$_COOKIE[\'' . $param . '\']';
  990. break;
  991. case 'SESSION':
  992. $parseStr = '$_SESSION[\'' . $param . '\']';
  993. break;
  994. case 'ENV':
  995. $parseStr = '$_ENV[\'' . $param . '\']';
  996. break;
  997. case 'REQUEST':
  998. $parseStr = '$_REQUEST[\'' . $param . '\']';
  999. break;
  1000. default:
  1001. $parseStr = '\'\'';
  1002. }
  1003. return $parseStr;
  1004. }
  1005. /**
  1006. * 特殊模板变量解析
  1007. * 格式 以 $Think. 打头的变量属于特殊模板变量
  1008. * @access public
  1009. * @param array $vars 变量数组
  1010. * @return string
  1011. */
  1012. public function parseThinkVar(array $vars): string
  1013. {
  1014. $type = strtoupper(trim(array_shift($vars)));
  1015. $param = implode('.', $vars);
  1016. switch ($type) {
  1017. case 'CONST':
  1018. $parseStr = strtoupper($param);
  1019. break;
  1020. case 'NOW':
  1021. $parseStr = "date('Y-m-d g:i a',time())";
  1022. break;
  1023. case 'LDELIM':
  1024. $parseStr = '\'' . ltrim($this->config['tpl_begin'], '\\') . '\'';
  1025. break;
  1026. case 'RDELIM':
  1027. $parseStr = '\'' . ltrim($this->config['tpl_end'], '\\') . '\'';
  1028. break;
  1029. default:
  1030. $parseStr = defined($type) ? $type : '\'\'';
  1031. }
  1032. return $parseStr;
  1033. }
  1034. /**
  1035. * 分析加载的模板文件并读取内容 支持多个模板文件读取
  1036. * @access private
  1037. * @param string $templateName 模板文件名
  1038. * @return string
  1039. */
  1040. private function parseTemplateName(string $templateName): string
  1041. {
  1042. $array = explode(',', $templateName);
  1043. $parseStr = '';
  1044. foreach ($array as $templateName) {
  1045. if (empty($templateName)) {
  1046. continue;
  1047. }
  1048. if (0 === strpos($templateName, '$')) {
  1049. //支持加载变量文件名
  1050. $templateName = $this->get(substr($templateName, 1));
  1051. }
  1052. $template = $this->parseTemplateFile($templateName);
  1053. if ($template) {
  1054. // 获取模板文件内容
  1055. $parseStr .= file_get_contents($template);
  1056. }
  1057. }
  1058. return $parseStr;
  1059. }
  1060. /**
  1061. * 解析模板文件名
  1062. * @access private
  1063. * @param string $template 文件名
  1064. * @return string
  1065. */
  1066. private function parseTemplateFile(string $template): string
  1067. {
  1068. if ('' == pathinfo($template, PATHINFO_EXTENSION)) {
  1069. if (0 !== strpos($template, '/')) {
  1070. $template = str_replace(['/', ':'], $this->config['view_depr'], $template);
  1071. } else {
  1072. $template = str_replace(['/', ':'], $this->config['view_depr'], substr($template, 1));
  1073. }
  1074. $template = $this->config['view_path'] . $template . '.' . ltrim($this->config['view_suffix'], '.');
  1075. }
  1076. if (is_file($template)) {
  1077. // 记录模板文件的更新时间
  1078. $this->includeFile[$template] = filemtime($template);
  1079. return $template;
  1080. }
  1081. throw new Exception('template not exists:' . $template);
  1082. }
  1083. /**
  1084. * 按标签生成正则
  1085. * @access private
  1086. * @param string $tagName 标签名
  1087. * @return string
  1088. */
  1089. private function getRegex(string $tagName): string
  1090. {
  1091. $regex = '';
  1092. if ('tag' == $tagName) {
  1093. $begin = $this->config['tpl_begin'];
  1094. $end = $this->config['tpl_end'];
  1095. if (strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1) {
  1096. $regex = $begin . '((?:[\$]{1,2}[a-wA-w_]|[\:\~][\$a-wA-w_]|[+]{2}[\$][a-wA-w_]|[-]{2}[\$][a-wA-w_]|\/[\*\/])(?>[^' . $end . ']*))' . $end;
  1097. } else {
  1098. $regex = $begin . '((?:[\$]{1,2}[a-wA-w_]|[\:\~][\$a-wA-w_]|[+]{2}[\$][a-wA-w_]|[-]{2}[\$][a-wA-w_]|\/[\*\/])(?>(?:(?!' . $end . ').)*))' . $end;
  1099. }
  1100. } else {
  1101. $begin = $this->config['taglib_begin'];
  1102. $end = $this->config['taglib_end'];
  1103. $single = strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1 ? true : false;
  1104. switch ($tagName) {
  1105. case 'block':
  1106. if ($single) {
  1107. $regex = $begin . '(?:' . $tagName . '\b\s+(?>(?:(?!name=).)*)\bname=([\'\"])(?P<name>[\$\w\-\/\.]+)\\1(?>[^' . $end . ']*)|\/' . $tagName . ')' . $end;
  1108. } else {
  1109. $regex = $begin . '(?:' . $tagName . '\b\s+(?>(?:(?!name=).)*)\bname=([\'\"])(?P<name>[\$\w\-\/\.]+)\\1(?>(?:(?!' . $end . ').)*)|\/' . $tagName . ')' . $end;
  1110. }
  1111. break;
  1112. case 'literal':
  1113. if ($single) {
  1114. $regex = '(' . $begin . $tagName . '\b(?>[^' . $end . ']*)' . $end . ')';
  1115. $regex .= '(?:(?>[^' . $begin . ']*)(?>(?!' . $begin . '(?>' . $tagName . '\b[^' . $end . ']*|\/' . $tagName . ')' . $end . ')' . $begin . '[^' . $begin . ']*)*)';
  1116. $regex .= '(' . $begin . '\/' . $tagName . $end . ')';
  1117. } else {
  1118. $regex = '(' . $begin . $tagName . '\b(?>(?:(?!' . $end . ').)*)' . $end . ')';
  1119. $regex .= '(?:(?>(?:(?!' . $begin . ').)*)(?>(?!' . $begin . '(?>' . $tagName . '\b(?>(?:(?!' . $end . ').)*)|\/' . $tagName . ')' . $end . ')' . $begin . '(?>(?:(?!' . $begin . ').)*))*)';
  1120. $regex .= '(' . $begin . '\/' . $tagName . $end . ')';
  1121. }
  1122. break;
  1123. case 'restoreliteral':
  1124. $regex = '<!--###literal(\d+)###-->';
  1125. break;
  1126. case 'include':
  1127. $name = 'file';
  1128. case 'taglib':
  1129. case 'layout':
  1130. case 'extend':
  1131. if (empty($name)) {
  1132. $name = 'name';
  1133. }
  1134. if ($single) {
  1135. $regex = $begin . $tagName . '\b\s+(?>(?:(?!' . $name . '=).)*)\b' . $name . '=([\'\"])(?P<name>[\$\w\-\/\.\:@,\\\\]+)\\1(?>[^' . $end . ']*)' . $end;
  1136. } else {
  1137. $regex = $begin . $tagName . '\b\s+(?>(?:(?!' . $name . '=).)*)\b' . $name . '=([\'\"])(?P<name>[\$\w\-\/\.\:@,\\\\]+)\\1(?>(?:(?!' . $end . ').)*)' . $end;
  1138. }
  1139. break;
  1140. }
  1141. }
  1142. return '/' . $regex . '/is';
  1143. }
  1144. public function __debugInfo()
  1145. {
  1146. $data = get_object_vars($this);
  1147. unset($data['storage']);
  1148. return $data;
  1149. }
  1150. }