Template.php 47 KB

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