Template.php 45 KB

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